Written by thoughtbot

Integrating Vim Into Your Life

More tips from thoughtbot about using vim, but this time with an emphasis on fitting it into your life.

Copy and paste

To copy and paste from your PRIMARY (on OS X, your clipboard) you use the * register. For example, to paste from something you've copied elsewhere use "*p and to copy the current line into your system-wide buffer use "*yy .

For those of us with both a PRIMARY and a CLIPBOARD, the * register is the PRIMARY and the + register is the CLIPBOARD.

Reducing distractions

You can fullscreen MacVim to block out other distractions with :set fuoptions=maxvert,maxhorz and then :set fullscreen. You can get back with :set nofullscreen

File management

Converts from IDEs such as Visual Studio like Nick might miss a file explorer for your project. Luckily, NERDTree comes to the rescue. Packed with plenty of good shortcuts, this helpful plugin saves Nick a lot of time when a simple :e or using the :R macros in rails.vim just won't cut it.

Search results from the command line

While Jason blogged about integrating Ack into vim before, here's a handy shell script to open a new vim with search results from the command line.

if [ "$#" = "1" ]; then
  $editor -c "/$1" $(grep -l $1 **/*)
elif [ "$#" = "2" ]; then
  $editor -c "/$1" $(grep -l $1 $2)
  $editor $(grep -l $@)

Save that as vg then, to open all files that mention current_user from the command line run: vg current_user

The full blame

You see that weird piece of code? Who checked that in?! Why is it even there?!

Add these to your .vimrc to get the quick blame for any highlighted lines (\b for svn, \g for git, and \h for Mercurial):

vmap <Leader>b :<C-U>!svn blame <C-R>=expand("%:p") <CR> \| sed -n <C-R>=line("'<") <CR>,<C-R>=line("'>") <CR>p <CR>
vmap <Leader>g :<C-U>!git blame <C-R>=expand("%:p") <CR> \| sed -n <C-R>=line("'<") <CR>,<C-R>=line("'>") <CR>p <CR>
vmap <Leader>h :<C-U>!hg blame -fu <C-R>=expand("%:p") <CR> \| sed -n <C-R>=line("'<") <CR>,<C-R>=line("'>") <CR>p <CR>


Exuberant ctags is a program that scans source files for keywords and supports many languages, including Ruby. Jumping to a defined tag is much faster and easier than searching for it in your project using Ack or Grep, and Vim integrates with ctags nicely.

You can create a tags file using the ctags command (run ctags --help for options), but if you're using the excellent rails.vim plugin by Tim Pope, you can run the :Rtags command from Vim. Running this command only takes a moment, and will generate a tags file containing all the keywords and locations in your project. Note that you'll have to regenerate your tags file using the same command for it to pick up new keywords.

Once you have a tags file, you can jump to a tag by using the :tag command:

:tag ensure_user_is_admin

Or by pressing Ctrl+] when the cursor is over a keyword. If there is more than one match for a tag, you can use :tn (next tag), :tp (previous tag), and :ts (select from a list) to navigate through matches. Again, this is much faster than searching.

Another benefit of using ctags is that you can use it for tab completion. I find that tab completion becomes unusably slow in a large project if you're finding keywords from open buffers, but you can tell Vim to only use the current file and ctags when finding keywords:

:set complete=.,t

Completion results with this setting are instantaneous.

Contributions by Jon Yurek, Nick Quaranto, and Joe Ferris.