A Reluctant Guide to Vim

Face it, it's better than nano and easier than emacs. You're going to edit some file, somewhere, sometime on some server. You won't be able to click to move your cursor or scroll to the bottom of a 2000 line long configuration file. As it would for any developer, holding down the arrow keys will get old, fast.

This is the reluctant guide to Vim. I'm not going to try to convince you that Vim is God's gift to man. Here's the low-hanging fruit that got me hooked, maybe it will hook you too.

Too often Vim power-users will try to explain some super-powerful thing you can do and how easy it is. Honestly, it is easy for us. The thing is, it's not really all that simple. The truth is that the power of Vim comes from knowing a few basics, and then composing those simple, one character keystrokes into powerful one-liners. Of course, all those things presuppose that you know the fundamentals. Which, as a reluctant Vim user, you probably don't.

These are the fundamentals:

modes what they are
normal default when you open vim, all the commands below assume you're in this mode.
insert "insert" mode, where you can type like normal. i, a, c, o will put you into this mode.
key what they do
hjkl "arrow keys". left, down, up, right. Protip: Try to use b and e before using these.
i start 'inserting' text before the cursor
a start 'inserting' text after the cursor
ESC Escape insert mode and go back to normal mode
d delete 1
c change (same as d but puts you into insert mode) 1
o open a new line below your current line
e jump to the ending of the next 'word' 2
b jump to the beginning of the next 'word' 2
0 start of line
$ end of line
u undo
ctrl+r redo
gg go to start of file
G go to end of file Protip: you can put a line number in front of G to go straight to it.
/ find whatever you type after /. Use n to jump to the next occurence. shift+n to go backwards.
shift simple modifier, often makes a key "linewise" or opposite (e.g. shift+c becomes 'change the whole line.' shift+o becomes 'open a new line above your current line')

1 These should be combined with a 'movement', like e. So, ce is 'change everything until the ending of the next word'. d$ is 'delete eveverything until the end of your line'.

2 What's a word when you're writing code? Vim will do its best to guess. Sometimes it will fall short. shift+w will move you to the next word after a space. This is often closer to what you expect.

Don't try to remember these all at once, just remember that they're here. For example, next time you need to jump to the end of a file and add a new line (sudo vim /etc/hosts anyone?) try G and o. Need to add something to the very end of a line? shift+a.

Once you have a few of these keys committed to memory, 'compose' your keystrokes together by typing them one after another. For example, if you type c$ you've told Vim that you want to change everything from your cursor to the end of the line. Vim obeys your commmand and will delete all text from your cursor to the last character on the line and then let you start typing something with which to replace that text.

Compose, combine, mix and match. That's what Vim's all about.

Let's mix the last example up a little. Instead of changing everything up until the end of the your current line, let's try to delete everything from the current line to last line of a file. What two keys could compose together to describe that action to Vim? Using the table above, d and G might compose together to do just that, and indeed they do! One final example... say you see ggcG, what does it do? Decompose it with the table above. You'll hopefuly see that it says, 'go to the beginning of the file, gg, start changing it, c, and go to the end of the file, G. Effectively, we've emptied out the whole text file and put ourselves into insert mode at its start.

Welcome to Vim.


Read This Next