learn / vim / v3 · 1 of 1 · 8 min
Vim commands are tiny sentences. Learn a handful of verbs and you already know hundreds of edits.
## Commands are sentences
Here's the idea that makes Vim click. You don't memorize hundreds of shortcuts. You learn a few verbs (operators) and combine them with the nouns (motions) you already know from the last lesson.
[count] operator motion
3 d w
"three" "delete" "words"The verbs:
d— delete (it's really "cut": what you delete is saved so you can paste it)c— change: delete, then drop into Insert modey— yank, which is Vim's word for copy>and<— indent and un-indent
Now combine them with motions:
dw— delete to the start of the next wordd$— delete to the end of the linedG— delete from here to the end of the filec$— change everything to the end of the liney0— copy from here back to the start of the line
Every new motion you learn works with every verb you know, and every new verb works with every motion. Learn } (next blank line, meaning the end of the paragraph) and you instantly get d}, c}, y} and >} for free. That multiplication is why Vim users get faster over years instead of hitting a ceiling.
Read this Vim sentence: d2w. What does it do?
## Double it for the whole line
Press a verb twice and it acts on the whole line:
dd— delete the linecc— change (retype) the lineyy— copy the line>>— indent the line
And a few capitals do "to the end of the line":
Dis the same asd$Cis the same asc$
Counts still work: 3dd deletes three lines, and 5>> indents five.
An attacker added a backdoor user to /etc/passwd. Your cursor is on that line. Delete the whole line.
sync:x:4:65534:sync:/bin:/bin/sync backup:x:0:0::/tmp/.x:/bin/sh ^ cursor (user id 0 = root!)
Someone pasted a word too many times. Delete the three extra words, using a count.
allow allow allow allow from 10.0.0.0/8 ^ cursor (three too many "allow"s)
The cursor is on "yes". Replace everything from there to the end of the line, then start typing "no".
PermitRootLogin yes # temporary!!
^ cursor## Paste, undo and the magic dot
p— paste after the cursor (or below, if you copied whole lines)P— paste before the cursor (or above)x— delete the single character under the cursoru— undoCtrl-r— redo.— repeat the last change
The dot is the most underrated key in Vim. Make a change once, move somewhere else and press . to make it again.
A real example: you're hardening a config and need to comment out four lines. On the first line press I, type #, then press Esc. Now just press j. three times. Four lines commented out with a handful of keys.
Copy the current line and paste a duplicate right below it.
Comment out four config lines in a row using the dot command. Put the steps in order.
- 1.Press . to repeat the change (then j. j. for the rest)
- 2.On the first line, press I to insert at the start
- 3.Press j to move down a line
- 4.Type # and press Esc
## Write it down
When you change a config file on a shared or production system, note the file, the line and what you changed and why, before you :wq. Better still, copy the file first with cp sshd_config sshd_config.bak. Future you, and the incident report, will thank you.