Strong opinions, weakly held

The grammar of Vim

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 and t. 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 F and 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, d, and c. 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 — ya(.

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.


  1. Haralan Dobrev

    May 12, 2012 at 3:42 am

    Great read!

  2. I wouldn’t call vim “retro.” Sure. it’s lineage has been around for a long time, but it’s also been maintained and constantly state-of-the-art, and it’s never fallen out of use.

  3. Great article. This is the best summary of the vi’s logic I’ve read yet. Despite that, I don’t think I don’t think I’ll be adopting vi as my full time editor anytime soon. Most of my programming time is spent thinking, and the time I spend typing is a very small fraction of my total time spent. Learning another language to potentially increase my input speed doesn’t feel like a good investment, personally. I can’t deny how cool it is to be able to do all your programming without touching a mouse, but I think you need to have a certain type of brain for it to make sense.

  4. This was a good summary, thanks for putting it together!. Climbing the VIM learning curve takes a while, but is definitely worth it. I combine it with GNU screen for a completely terminal-based development environment that makes me way happier than when I tried to code using other IDEs.

  5. This is a really great post. I’m relatively new to vim and was just starting to use the language of vim, on my own, to help learn the seemingly random letters assigned to commands. On itss own, things like ‘y’ for yank, the difference between ‘p’ and ‘P’ and so many other commands make a little more sense. Then, after reading your post, I realized you hit the nail on the head. It’s truly a language. After all, the little voice in my head says ‘Yank’ not ‘y’.

    To clarify the confusion behind up, down, left and right, there’s a really great blog post that explains why, at http://www.catonmat.net/blog/why-vim-uses-hjkl-as-arrow-keys/ – not really the language of vim, but the reason.

  6. This is a great read. I’ve always tried to interpret vim this way but didn’t realize it. It seems a lot more clear in my head now.

  7. The only commands I ever use are: i (input mode), :w (save and quit), and :q! (quit without saving). It’s nice to learn a few more useful tools.

  8. Reminds me strongly of this rather famous answer on StackOverflow: http://stackoverflow.com/a/1220118 “Your problem with Vim is that you don’t grok vi.”

  9. That Stack Overflow post is great. Hadn’t seen it before.

  10. When it comes to editors I hate vi and love vim. It’s possible because those are two very different editors! That “Improved” in vim’s name really make huge difference. So please don’t confuse vi and vim.

  11. Hah, T actually brings up an excellent point there– I usually express their relationship as: vi : Vim :: Netscape Navigator : Mozilla Firefox They’re directly related, but the latter is so far advanced from the former that they’re really no longer comparable.

    …That said, a better understanding of vi will help immensely with using Vim to its fullest. 🙂

  12. Sander Takens

    May 14, 2012 at 1:32 am

    Somehow it sounds to me like a T9 job on mobile phones. It usually does not what I want and drives me insane. I do not like stuff that does things for me if I am an idiot (Apple style)

    But I do see the + point here and do grand everybody their favorite editor. But gonna stick with MC which does what I want, where I want it and how I want it. Does not what I don’t want it to do.

  13. george trencat

    May 14, 2012 at 4:55 am

    NO Discussion for me. Every language that uses “goto” fails! Dont use this, its Spaghetti. But was interesting to see, what OTHER people think, thanks.

  14. Reminded me of Esperanto, both expressive. Till now never realized why obscureness and love (of vi) lay side-by-side.

  15. Amazing! If teachers in HS/college could explain math, science and English with that clarity, simplicity and completeness, we’d be living in a very different world! Thanks for a GREAT article!!

  16. Anirudh Gargi

    May 24, 2012 at 4:10 am

    The best article i have read on VIM yet. The whole idea of learning it as a grammar will help in better using and framing the commands. ! Will change my idea of looking at task to command mapping. ! And VIM for programming is also a great guide. Could extend this by explaining the plug-ins, marks,set commands and ~/.vimrc file concepts.

  17. I’ve been using Vim for a long time, but this expanded my understanding. Thanks!

Leave a Reply

Your email address will not be published.


© 2022 rc3.org

Theme by Anders NorenUp ↑