Vim Survival Guide

julian.a

Posted by julian.a

Let's start with a joke:

Q: How do you generate a random string?

A: Put a Windows user in front of vi, and tell them to exit

Vim has a (unearned!) reputation for having a steep learning curve. It's true that Vim's vast array of keybindings in 6 basic modes (and 6 additional modes) makes mastery a challenge, but getting up and running doesn't have to be hard. Despite Vim's reputation for a steep learning curve, if you ask a Vim user he'll usually tell you that after a few weeks he was already faster than with his old editor or IDE. The mountain of Vim mastery is tall, but the path up it doesn't have to be steep.

Part of the problem is that resources for beginners try to do too much. While there are a lot of good resources out there for learning Vim, none is really ideal for a total beginner. The venerable vimtutor is more or less the go-to, but in 30 minutes it introduces dozens of commands without making an effort to communicate which ones are essential. No one goes through vimtutor just once and remembers more than a small handful of commands, and those probably won't be the most useful ones. Most of the other introductions I've found suffer from a similar issue - they try to show off Vim's powerful functionality at the cost of failing to really teach a total beginner what he needs.

So, without further ado, here's a survival guide for total beginners. I've intentionally limited the guide to a small handful of commands (5 vital commands and 5 useful navigation commands) that you should be able to learn and remember in a few minutes. This guide won't make you an expert, but it should have you confidently editing files in Vim!

Vim is a Modal Editor

Before jumping into commands, there's one concept that's specific to Vim that you'll need to understand: Vim is a 'modal' editor.

Vim has 6 basic modes and in each mode the editor behaves completely differently. For instance, Insert mode behaves more or less like a standard editor, with text you type being added to the buffer, while in Normal mode, most of the keys on the keyboard have some functionality useful for efficient navigation or editing. Modal editing is what makes Vim so powerful. Instead of reserving the majority of the keys on your keyboard for entering text, and relegating commands to the relatively awkward chords, modes allow you to use the whole keyboard. If you want to paste text you don't need to type Ctrl+v you can just hit p. This might not seem like much, but once you get used to it, you'll find the old approach slow and painful. The modes are, of course, also a large part of the reason for Vim's learning curve: each mode has its own ins and outs you need to learn to really master Vim. For the purposes of this guide though, we'll be limiting ourselves to a small handful of commands in Normal mode, Insert mode, and Cmdline mode.

Five Vital Commands

There are really only five commands you absolutely need to know to use Vim:

  • <ESC>,
  • i,
  • u,
  • :w, and
  • :q

First <ESC>: from most modes <ESC> will bring you back to Normal mode. This is the natural resting mode for Vim. This command is vital because if you somehow end up in a mode other than Normal mode, the other commands in this section probably won't work. When in doubt, hit <ESC>.

Next on our list is i. From Normal mode i will take you to Insert mode. Insert mode lets you write text and behaves a lot like your standard text editor. If you're new to Vim, this will feel like the natural mode, but if you really want to learn to use Vim efficiently you're going to want to get used to returning to Normal mode once you're done editing. This will take some getting used to, but it will pay off in the end.

With access to Insert mode you can do basic file editing. Before long though, you'll try to type a bunch of text in Normal mode expecting to see it appear on the screen. This will issue a sequence of essentially random commands with bewildering effects. At the least a bunch of new text will probably have been inserted in one or more random places in your file and some of your text will likely be gone or changed. Vim Normal mode is powerful; a small handful of key presses can accomplish a lot. Eventually you'll learn to use that to your advantage, but for now, you just need to know how to undo what you did. First, tap <ESC> to make sure you're in Normal mode, then tap u until you've undone all the changes you made. Problem solved!

Finally, once you've edited a file, you need to save the file and quit Vim. For this we'll need to introduce Cmdline mode. : in Normal mode takes you to Cmdline mode. Once there, text you type will appear in a command line at the bottom of the screen. Hitting <CR> (carriage return, or enter) will issue the command and usually return you to Normal mode. Vim provides an impressive array of commands (including a complete scripting language), but for now we'll just introduce two: w writes the file you're editing to disk, and q quits Vim. So to save your file, type :w<CR> in Normal mode, and to quit type :q<CR>. Ok, I can't resist adding one more command here: :wq<CR> will write and quit in a single command.

Congratulations! Now you can edit text in Vim without fear!

Navigation

The "Vital Commands" above should be enough to change your Vim editing experience from "slow and painful" to just "slow", but I figure I should at least provide a small handful of navigation commands to help get you started on the path towards "not slow" or even "fast". These commands provide the basics of paging and searching through a file. As a bonus, these commands also work in less! Here they are:

  • <C-u> and <C-d>,
  • /, and
  • n and N,

<C-u> and <C-d> let you jump a half page up or down. These are useful for reading through an unfamiliar file.

/ should be your go-to command for navigation in Vim. / will open up a search prompt at the bottom of the screen. Type the text you want to search for there and then hit <CR> and Vim will jump your cursor to the first match. n will jump you to the next match, and N will go back one. This should allow you to quickly navigate your file. Once you get used to using search to navigate all other methods will start to feel painfully slow.

What Next?

This guide isn't intended to teach you to use Vim: we haven't even touched basic editing commands, or motions which are Vim's true strength, and I've glossed over a lot of details (for instance / is actually a regex search!). If you want to learn more, vimtutor is a good starting point. If you're looking for more advanced resources, Drew Neil's Practical Vim is fantastic.

Good luck!

Return to Articles & Guides