Published in · 4 min read · Jan 2, 2015
At Reverb we use MacVim to standardize the developer environment, make pairing easier, and increase efficiency. When I started at Reverb, I had never used Vim before. It took a few weeks for me to start feeling comfortable, but without tips like these, it would have taken months. Here are ten tips to help make you more efficient as you learn to use Vim.
1. Navigate more quickly by increasing your key repeat rate
Go to System Preferences -> Keyboard, increase your Key Repeat, and shorten your Delay Until Repeat. This will let you navigate more quickly with hkjl (don’t use those arrow keys!)
2. Navigate vertically with gg/G and ctrl-d/ctrl-u
The easiest way to navigate vertically is by jumping straight to the line number you need using :<line number>. Sometimes you don’t know that line number, so you can ‘page’ up and down quickly with ctrl-u and ctrl-d. Need to traverse the whole document? Jump quickly to the top of the document with gg or to the bottom with G.
3. Navigate horizontally with 0 and $
There are many ways to navigate horizontally, but the most helpful to me were always 0, which takes you to the beginning of the line, and $, which takes you to the end.
4. Escape quicker by re-binding to caps lock (or using ctrl-c)
If Vim is all about keeping your fingers as close to the keyboard’s “home row” as possible, why is escape mapped to such an important function? Use a key binding program (Reverb is partial to Seil) to map escape to caps lock or start using ctrl-c, the default alternative to escape.
5. Delete and switch to insert mode simultaneously with c
In the same way d is the Vim “verb” for delete, c is the verb for change. c allows you to delete and immediately enter insert mode. For instance, ‘ct ‘ deletes everything until the next space and enters insert mode. ‘c$’ does the same thing until the end of the line.
6. Search with / and navigate with n and N
The Vim equivalent of cmd-f is / followed by the term you seek. Once you’ve searched for a term, go to the next instance with n and the previous instance with N.
7. Search and replace with :%s
If you need to replace a term, you can use the somewhat more complicated :%s command which has the following structure:
:%s/<term to replace>/<replacement>/<flags>
For instance, if you’d like to find and replace ‘grey’ with ‘gray’, you’d use :%s/grey/gray. Unfortunately, however, by default this only replaces the first instance of the term in each line. Use the g flag to replace all instances of the first term, no matter how many are in each line.
You can also use the c flag to have Vim confirm each instance before replacing it. So :%s/grey/gray/gc will find each instance of ‘grey’ and ask you to confirm you’d like it replaced with ‘gray’.
8. Jump to between previously-visited locations using ctrl-o and ctrl-i
Vim keeps track of the locations you’ve been in your files, including across files. Every time you make a change to a file or open a new file, that location is stored. You can jump to previously-visited locations with ctrl-o. Similarly, you can jump forward with ctrl-i. This means that if you are working in two files in the same tab or window (without screen splitting), you can navigate easily between them by using ctrl-o and ctrl-i.
9. Go to other files with gf
When you have your cursor anywhere in the name of another file, you can press gf to jump into that file. This is especially useful due to the common practice of naming files the same as the class they define. For increased effectiveness, see the next tip.
10. Use plugins
Standard Vim is incredibly powerful, but a large part of its power lies in its customizability. File navigation plugins like NerdTree and CtrlP are probably some of the most accessible, work-flow altering plugins around, but do some research! There’s a ton out there. At Reverb, we use YADR, a dotfile repo that greatly enhances the capabilities of both MacVim and terminal (especially for Rails developers).
Hopefully you learned something new today about how to most efficiently use Vim. With Vim, the more you streamline your workflow, the more comfortable you’ll be. If you have any tips of your own, please leave us a comment!
— Joe Levering, Software Engineer, Reverb.com