The previous lesson described text abbreviations, which are used in text mode to expand
an abbreviation into a full sequence of text. This lesson describes keyboard macros, which are used in command mode to devise editing shortcuts.
:map command lets you create shortcuts for editing tasks that are repetitive or hard to remember. With the
:map command, you
assign a sequence of vi commands to a single character. The result is a keyboard macro, also called a keyboard map or simply a map. The map acts like a new vi command but performs several things at a time. Conceptually, vi maps are similar to shell aliases. Both provide a shorthand for issuing more complex commands. The table below shows the general form of the related map commands:
Keyboard macro: A keyboard macro lets you perform a sequence of vi commands as a single keystroke. Keyboard macros also are called keyboard maps because they are created using the :map command.
1) In a file named text, there are two lines where you want to transpose words
2) The dw command deletes the word upon, and the cursor ends up on the o in the word once.
3) The w command moves the cursor one word forward. The cursor is now on the word a.
4) The P command puts the deleted word, upon, before the cursor location.
5) Use the :map command, you can assign the sequence dwwP to the letter V.
6) Here, the cursor is move to another line where you want to transpose words.
7) This time, you can simply press the V key to switch the two words, upon and once.