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 calledVIM 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
foryank
: Copy the object to the register (VIM’s clipboard).d
fordelete
: Remove the object, and at the same time send it to the register (likecut
in other applications)c
forchange
: Liked
, but the cursor immediately change to INSERT mode in place of the deleted object(s), so that you can enter new stuff there.p
forpaste
: 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 be0
(If you recall, when you press0
, 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
forword
: You may get a bit confusion at first with this object, because though it abbreviatesword
, it’s actually mean from the cursor position to the end of current word. For e.g., if you have the wordword
with cursor atw
and pressdw
, the whole word will be gone, but the same action would result inw
if your cursor was ato
instead. To make sure you get the whole word in any case, you can useiw
(inner word), for all the letters of the word, andaw
(which is officially “a word”, but I often remember it as “all word”) for both the word and its surrounding spaces.b
forbackward
: Likew
but instead of going forward, you go backward.ib
andab
don’t work, though.ip
forinner paragraph
andap
fora 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 ofi
anda
are similar to their meanings iniw
andaw
.- If in the place of
<object>
, you just repeat<verb>
(e.g.y2y
ordd
), then theobject
type will beline
, 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, andG
(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.