Home » Posts tagged "Vim"

Prevent indentation when pasting from an external source in Vim

Occasionally when I copy and paste something from an external source into Vim every line gets indented, which is probably not something you want when pasting a code snippet.

The solution to this problem is an easy one. Before pasting, first execute :set paste, then paste your content and to return the setting to its previous value execute :set nopaste.

Problem solved!

Vim essentials: Ack

Ack is a tool for searching your (code)files, much like grep already does, but better. Ack is written in Perl and works on all major platforms, including Windows. To use Ack with Vim there’s this nice plugin called ack.vim. Install it together with App::Ack and you’re set to go.

With a single command (:Ack [options] {pattern} [{directory}]) you can recursively search for files matching {pattern}. The results returned from Ack will be visible in a newly opened pane in which you can navigate and open the file to start working with it. Every result line contains the file path, position of the matched search and the contents of that line.

ackvim

Vim essentials: NERD Commenter

Another great Vim plugin I use is NERD Commenter. Using this plugin makes it easy to comment out lines of code with just a few keystrokes. By pressing <leader>cc or <leader>c<space> you can comment out your selection using a single comment character per line. With <leader>cm you can comment your selection with one set of multipart delimiters, though your programming language has to support those. Repeat the commands to uncomment your code again. These commands work on single lines as well as on multiple lines which you’ve selected in visual mode (e.g. ranges).

In some cases it can happen that NERD Commenter doesn’t know which comment characters to use which causes it to use comment characters your language doesn’t or may not support. So far this only seems to happen when the filetype can’t be determined by Vim. This can be annoying at times, but I can live with it.

All NERD Commenter commands

For completeness I’ve copied the full command set from its README below. Personally I only use the ones I mentioned earlier. Though I think I’ll go and try to memorize the NERDComYankComment function since that particular case can pop up quite a lot when programming.

[count]<leader>cc |NERDComComment|
Comment out the current line or text selected in visual mode.

[count]<leader>cn |NERDComNestedComment|
Same as <leader>cc but forces nesting.

[count]<leader>c |NERDComToggleComment|
Toggles the comment state of the selected line(s). If the topmost selected line is commented, all selected lines are uncommented and vice versa.

[count]<leader>cm |NERDComMinimalComment|
Comments the given lines using only one set of multipart delimiters.

[count]<leader>ci |NERDComInvertComment|
Toggles the comment state of the selected line(s) individually.

[count]<leader>cs |NERDComSexyComment|
Comments out the selected lines “sexily”

[count]<leader>cy |NERDComYankComment|
Same as <leader>cc except that the commented line(s) are yanked first.

<leader>c$ |NERDComEOLComment|
Comments the current line from the cursor to the end of line.

<leader>cA |NERDComAppendComment|
Adds comment delimiters to the end of line and goes into insert mode between them.

|NERDComInsertComment|
Adds comment delimiters at the current cursor position and inserts between. Disabled by default.

<leader>ca |NERDComAltDelim|
Switches to the alternative set of delimiters.

[count]<leader>cl
[count]<leader>cb |NERDComAlignedComment|
Same as |NERDComComment| except that the delimiters are aligned down the left side (<leader>cl) or both sides (<leader>cb).

[count]<leader>cu |NERDComUncommentLine|
Uncomments the selected line(s).

Vim essentials: NERD tree

nerdtreeSo you started using Vim and were a bit disappointed with the lack of a proper tree-style directory explorer? Meet NERD tree (also on GitHub). With NERD tree you can navigate through your folders and files.

You can choose to always display the sidebar (put let NERDTreeShowBookmarks=1 in your .gvimrc) or use its toggle function NERDTreeToggle, which I’ve got mapped at \\ with map \\ :NERDTreeToggle. If you don’t like to have the tree on the left side you can change it from left to right with let g:NERDTreeWinPos = "right".

NERD tree has lots of shortcuts to improve and speed up navigation. You can always request an overview of these shortcuts with shift + ? when in the tree window. Some useful shortcuts are:

  • shift + i to list hidden files.
  • s to vsplit the current window and open the selected file.
  • i to split the current window and open the selected file.
  • t to open file in new tab.
  • m to modify the file/directory to rename, move, delete or create a file/directory.
  • shift + c to move into the selected directory.
  • u to move back one level in the directory tree.
  • x to close the opened directory tree in which you’re in.
  • return to open a directory or file.
  • j and k or the cursor keys to move up and down in the list.

There are a lot more shortcuts, all explained by opening the help with shift + ? and it might seem a bit much to memorize all these shortcuts but it’s not that bad really. Using just the return and cursor keys is enough to navigate and open files, but it really pays of to get to learn the other shortcuts.

Another neat thing of NERD tree is that it supports Bookmarks. To be able to store your bookmarks you need to specify a location where the bookmarks are stored. Generally you do this in your .gvimrc file.

" Store the bookmarks file
let NERDTreeBookmarksFile=expand("$HOME/.vim-NERDTreeBookmarks")
" Show the bookmarks table on startup
let NERDTreeShowBookmarks=1

To create a bookmark navigate your cursor to the directory you want to bookmark and use the command Bookmark <name> to create a named bookmark. You can later use this bookmark with NERDTreeFromBookmark <name>.

Favorite programming font on Windows: Consolas 10pt bold

For years I’ve used Courier New in my programming editors and terminal (PuTTY). But there’s a much better font available for that kind of stuff. For the last couple of years I’ve been using Consolas, which is shipped with Windows. I’ve configured my editors and PuTTY to use Consolas 10pt bold as I find it by far the best readable font to use for programming. This font is just the right size at 10pt and setting it to bold gives it a very smooth look.

Here’s how Consolas 10pt bold looks in GVim on Windows 8:

consolas

Recently I’ve given some other fonts a try as well because in the showed examples it appears they make a nice programming font. Not too long ago the Adobe Source Code Pro font was released and looking at the screenshot at that page it looks really nice. So I decided to download and install it. But trying it in GVim didn’t gave a satisfying result. It doesn’t look too bad, but it isn’t as compact as Consolas is:

source code pro

When using Linux (Ubuntu) I always use Droid Sans Mono. But for some reason it looks really bad on Windows:

droid sans mono

As you can see Droid Sans Mono is even bigger and just doesn’t look right. I wonder why this is though I suspect it has to do with the font rendering on Windows. I’ve found font rendering on Linux a lot better and for what I’ve seen on Mac OSX as well, though I’ve recently heard a Mac user saying he found the font rendering on Linux even better.

And OK, the fonts don’t look that bad but imagine having to look at it all day when programming. All these examples have been set at 10pt bold and the extra space taken by both Adobe Source Code Pro and Droid Sans Mono is just bad in my opinion.

So far I haven’t found anything better than Consolas when it comes to a programming font on Windows. I’m not looking for a replacement of it but I’m always willing to try something new or different. My main disappointment I think comes with how bad Windows renders these fonts, because I know Droid Sans Mono looks good on Linux. If there’s any programming font that renders really well on Windows I’d really like to hear about it.