Like a lot of people, I have gone retro when it comes to editors, using Vim for most of my day to day work. I’ve been doing most of my development these days in a terminal window, logged directly into a VM where I test my code. For more, see this article.
I have been a somewhat casual vi user for at least 15 years, but using it for all my development work has made it more important to vastly improve my Vim skills in a short time. The best article on Vim I read was Yan Pritzker’s Learn to speak vim – verbs, nouns, and modifiers! It totally revolutionized the way I thought about Vim.
For my first 15 years with Vim I tried to learn it the same way you might learn a foreign language from a phrasebook. I had learned everything in a disconnected way. Use
j to move down a line. Use
cw to change a word. Use
yy to copy the current line into a buffer. You can get by this way, but it’s a very simplistic approach.
Advanced Vim users understand that Vim commands are a language unto themselves, and that the key to being productive is understanding Vim’s grammar. Pritzker accurately identifies that grammar as consisting of the following parts of speech: verbs, nouns, and modifiers.
The nouns usually represent positions within the document. For example,
0 represents the beginning of the current line.
$ represents the end of the current line.
j represents the next line in the file. When you use most nouns by themselves in command mode, the implied verb is “go to”. So if you just type
j you move to the next line. There are some exceptions. For example, in command mode,
s means substitute a character. In other contexts, it’s a noun representing a sentence.
You can usually supply a quantity along with a noun. So
5j refers to the five following lines, and typing it will move your cursor down 5 lines if you don’t supply a verb. Likewise, the
w noun represents a word, and moves to the next word to the right if you use it by itself.
3w moves three words to the right. The biggest problem with the way most people learn Vim is that people learn these things as “movement commands” rather than as nouns.
There are a couple of special nouns that I don’t see a lot of people using —
f means “find the next occurrence” and
t means “til the next occurrence” and both of them are applied to the next character you type. So typing
f_ means “find the next underscore” (on the current line).
t_ means “til the next underscore,” the cursor will stop on the character before the next underscore. (You can use
T to search backward rather than forward.)
These are helpful for navigation, but they’re even more helpful when you’re constructing editing commands, because they can be used as a noun.
Now let’s talk verbs. The big three are
y is yank (copy).
d is delete.
c is change. Combining a noun and a verb applies the verb to the noun. That’s the essential grammar of a Vim command.
cw changes the current word.
c0 changes the text between the cursor and the beginning of the line.
d$ deletes to the end of the line.
Let’s look at
f a bit more deeply. Say you have a variable named
my_integer_value and you want to change the name to
my_string_value. If you move to the beginning of the name and type
cw you’ll change the entire variable name. Instead you can type
fi to move to the “i” in “integer”. Then type
ct_ (change up to the next underscore) and the word “integer” will be deleted. You can then type in “string” instead.
Lastly there are modifiers. Modifiers change how nouns are understood. The two big ones are
i (inside) and
a (around). Here’s a simple example. If your cursor is in the middle of a word and you use the command
cw it will change all the characters from the cursor position to the end of the word. If you type
ciw it will change the entire word. You can also use paired characters as nouns with
i. So to change the contents of quotation marks, you can type
ci". Or to copy whatever is inside a pair of parentheses, you can type
yi(. If you want to yank the parentheses themselves, you can use
a instead —
This is just the tip of the iceberg, there’s a lot more. But once you know how to put these commands together, you’ll have a much easier time understanding the Vim tips you see online. Suddenly you’re expanding your vocabulary rather than learning a new language.
For example, once you understand the Vim as a language, you can get a lot out of Andrei Zmievski’s Vim for Programmers presentation, which is a whirlwind tour through PHP commands.
Vim is weird enough that it can seem like learning it is probably not worth the effort, but it’s an incredibly powerful tool in the right hands. The problem is that to even get started, you have to learn the difference between the various modes and how to switch between them. You also need to learn the basic methods for navigating through a document. If you’re used to a normal GUI editor, that can be really frustrating. Once you’ve mastered the basics, though, learning Vim’s grammar will really accelerate your efforts to make the most of it.