UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 31.7 Maps for Repeated Edits Chapter 31
Creating Custom Commands in vi
Next: 31.9 Good Stuff for Your .exrc File
 

31.8 More Examples of Mapping Keys in vi

The examples that follow will give you an idea of the clever shortcuts possible when defining keyboard maps.

  1. Add text whenever you move to the end of a word:

    map e ea

    Most of the time, the only reason you want to move to the end of a word is to add text. This map sequence puts you in text-input mode automatically. Note that the mapped key, e, has meaning in vi. You're allowed to map a key that is already used by vi, but the key's normal function will be unavailable as long as the map is in effect. This isn't so bad in this case, since the E command is often identical to e.

    In the remaining examples, we assume that e has been mapped to ea.

  2. Save a file and edit the next one in a series (30.4):

    map q :w^M:n^M

    Notice that you can map keys to ex commands, but be sure to finish each ex command with a RETURN. This sequence makes it easy to move from one file to the next and is useful when you've opened many short files with one vi command. Mapping the letter q helps you remember that the sequence is similar to a "quit."

  3. Put troff emboldening codes (\fB and \fP) around a word:

    map v i\fB^[e\fP^[

    This sequence assumes that the cursor is at the beginning of the word. First, you enter text-input mode, then you type the code for bold font. (In map commands, you don't need to type two backslashes to produce one backslash.) Next, you return to command mode by typing a "quoted" (31.6) ESC. Finally, you append the closing troff code at the end of the word, and you return to command mode. Of course, the map is not limited to troff font codes. You can use it to enclose a word in parentheses or C comment characters, to name just a few applications.

    This example shows you that map sequences are allowed to contain other map commands (the e is already mapped to ea). The ability to use nested map sequences is controlled by vi's remap option (31.14), which is normally enabled.

  4. Put troff emboldening codes around a word, even when the cursor is not at the beginning of the word:

    map V lbi\fB^[e\fP^[

    This sequence is the same as the previous one, except that it uses lb to handle the additional task of positioning the cursor at the beginning of the word. The cursor might be in the middle of the word, so you want to move to the beginning with the b command.

    But if the cursor were already at the beginning of the word, the b command would move the cursor to the previous word instead. To guard against that case, type an l before moving back with b, so that the cursor never starts on the first letter of the word. You can define variations of this sequence by replacing the b with B and the e with Ea. In all cases though, the l command prevents this sequence from working if the cursor is at the end of a line. (To get around this, you could add a space to the end of the word before typing the keymap.)

- DG from O'Reilly & Associates' Learning the vi Editor, Chapter 7


Previous: 31.7 Maps for Repeated Edits UNIX Power ToolsNext: 31.9 Good Stuff for Your .exrc File
31.7 Maps for Repeated Edits Book Index31.9 Good Stuff for Your .exrc File

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System