Small Collection of Useful Vim Tricks

I’ve been using vim for the past couple of years. Vim is a cross-platform programmer’s editor that is highly customizable and has a ton of well known and not-so well known features. As time goes by, I always come across new or better ways of doing things that increase productivity. What’s most important is to force yourself to start using them so that they become part of your workflow, becoming muscle memory.

Here’s a small collection of tips that I’ve picked up along the way:

Moving around files

  • I never bought into NERDTree or any of the other explorer plugins. I usually jump around using gf, using Rails.vim’s :R, :A, etc, or you can open up vim’s built in explorer using :Ex. :Sex opens it up in a split window.

  • Use vim marks to jump around any number of files. Here’s a quick reference

  • gf will open the filename under the cursor. Rails.vim extends this by being smart about view partials. You can also use gf to open the file in a new tab.

  • Use ctags. C-] on any method name or attribute to jump to the declaration point (or -] to open on a new window. Use to come back. The tag stack is maintained. Then, :ta to jump to the tag. I use F5 to rebuild my ctags database: map :!ctags -R --exclude=.svn --exclude=.git --exclude=log *

  • Learn how to use windows and tabs: it can really improve your work flow. I usually have multiple tabs open with different areas of the project. On each tab, I may have up to four windows open. I use tabs when it’s a different context, for example, a CSS and a view file on one tab, and a controller, cucumber feature and step definitions on another tab, each on its own windows. When working with many windows, it’s useful to quickly resize them: _ maximizes the viewing area of the active window (for vertical windows, | does the same). You can come back to equally sized windows with =. The easiest way for me to move around tabs is using gt and gT. To move around windows, I typically use along with one of h, j, k, l.

More efficient editing

  • Automate simple tasks with macros. q{reg}q. Then invoke it n times with n@{reg}. Learn about recursive macros to run it until EOF.

  • Use your registers. Yanking (and putting) and recording macros to different registers is most useful. Use :reg to view contents of your registers.

  • Quickly change text surrounded by something using ci. For example, to change a string in double quotes use ci", or to change parenthesized parameters, use ci(.

  • When working with large number of files, you can run the same command on all buffers/windows/tabs by using :bufdo/:windo/:tabdo. Such a time saver.

  • Learn how to use vim’s substitute command with regular expressions and the grouping operators \( and \). You can morph a file at will using the matched part of the pattern and \1, \2. Throw i at the end of the command for interactive substitution. Here’s a good vim regex guide

  • Vim’s built in autocompletion rocks. Use or in insert mode to activate and move through the options.

  • When moving within a file, f moves the cursor forward until the first . ; repeats the action. You can move to the second occurrence of by using 2f. F does the same, but backwards.

  • Learn the different ways to enter insert mode. The ones I use the most, other than i are

    • a to enter insert mode at the next character (as opposed to i, which enters insert mode before the current character).
    • A to enter insert mode at the end of the line.
    • I to enter insert mode at the beginning of the line.
    • O to create a new line above the current line and enter insert mode.
    • o to create a new line below the current line and enter insert mode.
  • Block selection is magical. Use it when you want to select a vertical block or column to yank it, delete it, etc. To enter visual block select, do . You can also insert arbitrary text before the block with Esc. For example, this is very useful for inserting the same text on a set of aligned

  • s(like adding a class=”foo” attribute) or after hash rockets or equal signs on your ruby code - yet another reason to align these properly.

  • C-e and C-y, scrolls the buffer without moving the cursor. Make it scroll three lines at a time with: nnoremap 3 nnoremap 3

  • Use % to jump to the matching open or closing bracket/parenthesis. Use the matchit plugin to have it match more than single characters, for example def matches end in Ruby code, or

    in HTML.

Other miscellanea

  • For bash style command tab completion, put set wildmode=list:longest on your vimrc. Then tab away when running commands on ex mode.

  • View unprintable characters with :set list. Cancel out with :set nolist

  • Analyzing log files with vim is also a win. For example, you may be in a rails app and used Rails.vim’s :Rlog command. Now you can use the global command to surface up the specific pattern you’re looking for using :g/{pattern}/p, or you may remove any useless lines with :g/{useless}/d.

  • When pasting something, vim will autoindent which is usually not what you want. Use :set paste before the paste to avoid that.

  • Use gg=G to apply proper indentation to a messy source file. I define 2 space “tabs” for all file types by using set tabstop=2 shiftwidth=2 expandtab.

  • Saving and reopening a vim session is easy using :mksession! and :source. I have the following two maps in my vimrc to make this even easier with F2 and F3: map :mksession! ~/vim_session " Quick write session with F2 map :source ~/vim_session " And load session with F3

  • Ctrl-L to redraw the screen (to fix broken syntax highlighting). The following mapping will also remove highlighting after a search: nnoremap :nohls.

  • About those darn backup files infecting your project: Keep them, but on a different location - you may come to a crash, and you’ll need them. I created a directory called ~/.vim_backups, and vim puts them there. The relevant lines are: silent execute '!mkdir -p ~/.vim_backups' set backupdir=~/.vim_backups// set directory=~/.vim_backups//

  • I usually write blog posts in Vim using markdown. I use vim’s integrated spell checking by setting :set spellang=en_us on my vimrc. To invoke it, use :set spell which will underline misspelled words, and z= brings up suggestions for the misspelled words under the cursor.

  • Word wrapping is sluggish by default. I added these lines to my .vimrc which do a few of things: w toggles between wrap and no wrap. It adds a > character to new lines, making it obvious that you’re on a wrapping line. It tries to wrap on word boundaries, and finally, it remaps k and j so that you’re moving up/down on visual lines, not actual lines.

  • Use set showmatch and set mat=5 on your .vimrc to blink matching parenthesis or brackets. Unobstrusive and useful.

I am sure I’m missing many, but this is what I use most often. Got any tips worth sharing or even better, improvements to the above?


The following are books, tutorials or articles that I’ve found useful in learning and improving my vim workflow.