Thursday, April 3, 2008

Using Vim Effectively

This is going to be sort of a common sense post aimed at newer Vim users. One thing I notice is that a lot of people start using Vim in hopes of improving their efficiency and speeding up their editing tasks. This makes sense because Vim can easily allow you to edit text an order of magnitude faster than an ordinary text editor; however, getting these kinds of gains out of Vim requires you to adopt the philosophy that it was built on. Probably the most basic and fundamental reason for using a modal editor (such as Vim) is that your fingers rarely have to leave the home-row of keys. A lot of new Vim users learn the basics of how to save and open a file and then navigate around Vim using the arrow keys to get where they want to go. This is probably the single worst habit new Vim users get into, and it destroys productivity. If you're one of those Vim users, please read the following tips as they will help you get into good habits that will ensure you get the most out of using Vim.

1) Start with vimtutor:

vimtutor starts of with the most fundamental and basic skills. To use Vim effectively, you need to have these skills committed to memory and "automatic".

2) Never use the arrow keys:

You simply NEVER need to use the arrow keys in Vim. If you are using them, go back to vimtutor and learn to navigate using hjlk. You'll thank yourself for taking the time to learn the "right way" later.

3) Avoid the Esc key:

The Esc was a mis-step in Vi's initial design. It requires you to lose focus on the home row. Use Ctrl-[ instead, or better yet, remap CAPS Lock to either Esc or Ctrl. I'll add a tip for how to do this in the future.


My friend Skyler tells me I'm wrong here. Apparently, moving the Esc key to the top-left corner of keyboards was a mis-step in keyboard evolution.

4) Use word motions:

When scrolling left and right on a line, don't go a character at a time. At minimum, you should jump a word at a time using "w" (forward one word) or "b" (backward one word). Vim has the ability to jump to the next word, sentence, paragraph, etc. Learn how to get around using the word motions that make the most sense for your workflow (see :help word-motions). If you see a word way down the screen, and you want to get to it quickly, consider using a search followed by the enter key (/someword).

5) Insert smarter:

Pressing "i" to go into insert mode will get you there, but is it what you really want? Use the following insert and delete-insert commands when you need them.

I = insert at the beginning of the line
A = insert at the end of the line
o = insert on the line below current
O = insert on the line above current
C = delete all text to end of line and enter insert mode
D = delete all text to end of line

6) Join lines:

Too many times when someone wants to put a subsequent line on the current line, I've seen them go into normal mode, jump down to the next line, go into insert mode and press backspace. You NEVER need to do this. Just press "J" in normal mode.

7) Use H, M, and L:

Don't bother scrolling to the top, middle or bottom of the screen. Just use H, M, and L. I use the following mnemonics:

H = high (top of screen)
M = mid (medium of screen)
L = low (bottom of screen)

8) Use gg and G:

You never need to scroll to the top or bottom of the file. Simply hit "gg" to go to the top or "G" to go to the bottom. A number followed by "G" takes you to that line number.

9) Avoid the mouse:

I see a lot of people copy and paste with the mouse. While this works, it forces you to leave the home row. Anything that can be done in Vim with the mouse is faster with the keyboard. If you want to copy and paste, just yank (from visual mode if necessary) and put. The same goes for navigating text on the screen. Find the normal mode navigation sequences that work for you.

10) Find what works:

The list of commands available in Vim is so expansive that it's questionable if anybody could really "learn" them all. The best bet for using Vim effectively is finding the subset of commands that work for you and making them "automatic". Incorporate them into your daily programming activities and if something seems inefficient, find a better way. The help system in Vim is amazingly in-depth and provides a good avenue for finding answers quickly (see :help to get started).


Christopher Suter said...

Great summary, Travis. I look forward to a post on mapping CAPS to ESC. I read something about it once long ago but i seem to recall it involved xmodmap, or something like that, to modify the behavior of the ESC and CTRL keys themselves at the input level. My gut tells me there's something inelegant about this solution, but i'm not sure there would be another way.

Niel said...

Nice write-up. couple of ones that helped me:

There's also '.', which I tend to use quite often to repeat the last operation.

'#' or '*' to search the word under the cursor.

if coming from windows, you may want to try the mswin.vim startup script, as a bridge between win-land and vim-land( :so $VIMRUNTIM/mswin.vim )

Will said...

Skyler is right.

Vi was developed on a system that had Esc where now we have Tab and Control where CapsLock.

And avoid the mswin.vim script at all costs. It cripples Vim and makes it harder to learn the right/Vim way to do things.