home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 31.6 Protecting Keys from Interpretation by ex Chapter 31
Creating Custom Commands in vi
Next: 31.8 More Examples of Mapping Keys in vi
 

31.7 Maps for Repeated Edits

[Another way to do this is with @ -functions ( 31.4 ) . -JP]

Not every keymap is something you want to save in your .exrc file. Some maps are handy just because you have to do a repetitive series of edits. Developing a complex map to repeat your edits can save more time than it takes. For example, assume that you have a glossary with entries like this:

map - an ex command which allows you to associate 
a complex command sequence with a single key.

You want to convert this glossary list to nroff ( 43.13 ) format, so that it looks like:

.IP "map" 10n
An ex command which allows you to associate
a complex command sequence with a single key.

The best way to define a complex map is to do the edit once manually, writing down each keystroke that you must type. Then re-create these keystrokes as a map. You want to:

  1. Insert the ms macro for an indented paragraph ( .IP ) at the beginning of the line. Insert the first quotation mark as well ( I.IP <"> ).

  2. Press ESC to terminate text-input mode.

  3. Move to the end of the first word ( e ) and add a second quotation mark, followed by a space and the size of the indent ( a<"> 10n ).

  4. Press RETURN to insert a new line.

  5. Press ESC to terminate text-input mode.

  6. Remove the hyphen and two surrounding spaces ( 3x ) and capitalize the next word ( ~ ).

That's quite an editing chore if you have to repeat it more than a few times. With map you can save the entire sequence so that it can be re-executed with a single keystroke:

map g I.IP "^[ea" 10n^M^[3x~

(To set that option during a vi session, type a colon ( : ) first.) Note that you have to "quote" both the ESC and RETURN characters with CTRL-v ( 31.6 ) . ^[ is the sequence that appears when you type [CTRL-v] followed by [ESC] ^M is the sequence shown when you type [CTRL-v] [RETURN] .

Now, simply typing g will perform the entire series of edits. At a slow data rate you can actually see the edits happening individually. At a fast data rate it will seem to happen by magic.

Don't be discouraged if your first attempt at keymapping fails. A small error in defining the map can give very different results from the ones you expect. Type u to undo the edit, and try again.

[I like to write a keymap in a temporary file and edit it there until I get it right. For instance, I write my buffer and type :e temp.so to open the temporary file temp.so . I make the keymaps, one per line - without a colon ( : ) first. I write this map file ( :w ), then read it in to the editor ( :so  % ( 30.4 ) ). If I don't get an error, I switch to the original file ( :e  # ( 30.4 ) ) and try the map. Then I usually go back to the map file ( :e# ) again, fix the mistake :-) , and repeat the process until I get what I wanted. -JP  ]

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


Previous: 31.6 Protecting Keys from Interpretation by ex UNIX Power Tools Next: 31.8 More Examples of Mapping Keys in vi
31.6 Protecting Keys from Interpretation by ex Book Index 31.8 More Examples of Mapping Keys in vi

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