We’ve learned to describe, navigate, and manipulate text with simple combinations of verbs, nouns, and quantifiers. Now let’s take it further. In normal languages we compose individual words (“ball”) to form simple phrases (for instance, “throw the ball” or “catch the ball”), much as we have been doing in Vim. But we also compose phrases together to form other phrases and sentences (for instance, “throw the ball upwards and then catch the ball”). Many of these composite notions are still simple and intuitive, and we express them with the same ease that we do the component notions.
It’s no different in Vim. While many common ideas can be expressed with the commands we’ve already seen, just like with any language, there are composite ideas that are also common and intuitive, and which are expressed as combinations of elementary commands. For instance, you may sometimes want to swap two words. “Swap” isn’t a word in the Vim language, but this operation could be expressed in Vim as “delete this word and then paste it after the next one.” Knowing this pattern can help you express the higher-level concept fluently without dwelling on the lower-level mechanics. Here are some other common phrases:
xp
— swap character forwards (very common, e.g. to fix simple typos)Xp
— swap character backwardsdbwP
— swap word backwards (sometimes also dBWP
or daWBP
)dwwP
— swap word forwards (sometimes also dWWP
or daWWP
)ddp
— swap line forwardsddkP
— swap line backwards
The pattern for swapping is the same for any noun, so paragraphs may be swapped in the same manner as words or letters. More phrases:
yyp
— duplicate lineywP
or ywwP
or yaWP
etc. — duplicate wordyapP
— duplicate paragraph (and so on to duplicate anything)0d^
— delete leading whitespace, i.e. at the start of the line (or ^d0
)g_lD
— delete trailing whitespace, i.e. at the end of the line (that’s an “l” as in London)0d^g_lD
— delete surrounding whitespace (within a line) — note how this composes the preceding two phrasesdd{p
/ dd}P
— move line to top / bottom of paragraph or block (useful when working with lists)dd{p''
/ dd}P''
— move line to top / bottom of paragraph or block and returnVr=
— overwrite entire line with =
‘syypVr=
— underline heading (e.g. useful in reStructuredText documents)
The important thing is not to memorize the keystrokes for such phrases but to understand the ideas behind them, so that when the time comes you will know how to say them.
See if you notice any other high-level operations that you do often, and what corresponding phrases might be worth remembering without thinking of them from scratch each time, and you’ll be well on your way to Vim mastery! 🕊