The Language of Vim

Posted in editors minimalist -

After the short introduction of VIM, I hope I did persuade you to start using this awesome editor to replace any bloated software you have been using. However, I understand that it can be difficult to get started, and the vimtutor might not work for everyone.

Back when I first started using VIM, I was in the same position, and the only thing that kept me from giving up on it was simply because I couldn’t run VSCode or Atom on the remote super computer at work. Long after that, I realized that there is a simple secret that, if I had known from that beginning, would have dramatically eased the path into VIM. That secret is indeed very important, but for some reason, vimtutor seems to expect us to figure it out on our own. The secret is:

You can talk to VIM if you know its language.

If you are a programmer, you can think of it as a programming language; but it can also be considered human language. Almost everything you need to know to get started in VIM lies in some grammar rules (or syntax). Of course, just like a real language, there will be some edge cases where the rules don’t apply, but you don’t have to care about those too much until you have more VIM experience.

Editing text in VIM

To make changes to your document in VIM, here is almostly the only “grammar rule” you need to remember:

<verb> <count>? <object>*

in which:

  • <verb> is basically an one-letter abbreviation of the English verb that you would like to apply to your object(s). These “verbs” are officially called VIM motions (you can see the full list of them here), but as we refer to VIM as a language, remembering them as “verbs” makes more senses. Here are the ones I find mostly used and you may want to remember first:
    • y for yank: Copy the object to the register (VIM’s clipboard).
    • d for delete: Remove the object, and at the same time send it to the register (like cut in other applications)
    • c for change: Like d, but the cursor immediately change to INSERT mode in place of the deleted object(s), so that you can enter new stuff there.
    • p for paste: Paste whatever it is from the register to the current position.
  • <count> means the number of sequential objects you want to apply this action on. It should be a number, and shouldn’t be 0 (If you recall, when you press 0, you immediately jump to the start of the line). The ? after <count> implies optionality: When you don’t provide a value, <count> is assigned a value of 1.
  • <object> is also the kind mnemonic abbreviation, similar to <verb>, but not it refers to the type of objects you would like to apply the action on, and may be one of these types:
    • w for word: You may get a bit confusion at first with this object, because though it abbreviates word, it’s actually mean from the cursor position to the end of current word. For e.g., if you have the word word with cursor at w and press dw, the whole word will be gone, but the same action would result in w if your cursor was at o instead. To make sure you get the whole word in any case, you can use iw (inner word), for all the letters of the word, and aw (which is officially “a word”, but I often remember it as “all word”) for both the word and its surrounding spaces.
    • b for backward: Like w but instead of going forward, you go backward. ib and ab don’t work, though.
    • ip for inner paragraph and ap for a paragraph: A paragraph is normally everything between two line breaks (i.e. when you press Enter), from the beginning to first line break, or from the last line break to the end. The meanings of i and a are similar to their meanings in iw and aw.
    • If in the place of <object>, you just repeat <verb> (e.g. y2y or dd), then the object type will be line, which means, eh, a line.

For some reasons, the actions performed on characters object don’t seem to follow the rules, though you can apply <count> to them:

  • To delete a letter, just press x, which deletes the letter and also sends it to the register. 10x deletes 10 characters.
  • Press r, following by another character will replace the character under the cursor with that new character. <count>r<character> will replace <count> characters with the new character. r<count><character> won’t work as you expected, it just replaces the current character with the first character in <count>.

Optionally, you can also install some plugin that provides additional object types. That is not in the scope of this article, though.

Note: <count> doesn’t only apply to <object>, it can also apply to <verb>, so technically, you can use 10dw instead of d10w to delete 10 next words. The result will be the same, but instead of deleting 10 words and send them to the register, you have deleted the next word repeatedly 10 times, which is a bit slower (though we can’t see it) and the register will only have the last of the 10 words, instead of all of them.

Note2: In reality, since 10dw are not normally used with intentions, some VIM distributions treat 10dw as if it is d10w. You have to do some experiments to see if your VIM does that. Generally, unless you’ve already attached to 10dw, it is better to run d10w.

What about moving around in VIM?

Moving around easily is an important factor contributing to your speed, but unfortunately, the “grammar rules” are not that simple as in editing things. Good news is you don’t have to know everything at once, and if you could remember the rules regarding Editing, then some useful movements may have made senses naturally:

  • <count><direction>: Move the cursor in that direction <count> times. E.g. 100j will move down 100 lines below the current line.
  • <count>w/b: Move forward/backward <count> words.

There are some other rules that worth remembering:

  • gg to go to the fist line, and G (Shift+g) to go to the last line of the document.
  • 0 to go to line beginning, $ to go to the end.
  • % to go to the matching parenthesis of the parenthesis under the cursor.
  • H, M, L (High, Middle, Low): To jump to top, middle and bottom of the screen.

Once you master those movements, you may want to learn some more from here.

I hope those are enough for getting started. Next time, let’s talk about another important element of VIM: the plugins.

Written by Huy Mai