7.3. Saving CommandsOften you type the same long phrases over and over in a file. vi and ex have a number of different ways of saving long sequences of commands, both in command mode and in insert mode. When you call up one of these saved sequences to execute it, all you do is type a few characters (or even only one), and the entire sequence is executed as if you had entered the whole sequence of commands one by one. 7.3.1. Word AbbreviationYou can define abbreviations that vi will automatically expand into the full text whenever you type the abbreviation in insert mode. To define an abbreviation, use the ex command: :ab abbr phrase abbr is an abbreviation for the specified phrase. The sequence of characters that make up the abbreviation will be expanded in insert mode only if you type it as a full word; abbr will not be expanded within a word. Suppose in the file practice you want to enter text that contains a frequently recurring phrase such as a difficult product or company name. The command: abbreviates International Materials Research Center to the initials imrc. Now whenever you type imrc in insert mode, imrc expands to the full text.:ab imrc International Materials Research Center
Abbreviations expand as soon as you press a non-alphanumeric character (e.g., punctuation), a space, a carriage return, or ESC (returning to command mode). When you are choosing abbreviations, choose combinations of characters that don't ordinarily occur while you are typing text. If you create an abbreviation that ends up expanding in places where you don't want it to, you can disable the abbreviation by typing: :unab abbr To list your currently defined abbreviations, type: :ab The characters that compose your abbreviation cannot also appear at the end of your phrase. For example, if you issue the command: :ab PG This movie is rated PG you'll get the message "No tail recursion," and the abbreviation won't be set. The message means that you have tried to define something that will expand itself repeatedly, creating an infinite loop. If you issue the command: :ab PG the PG rating system you may or may not produce an infinite loop, but in either case you won't get a warning message. For example, when the above command was tested on a System V version of UNIX, the expansion worked. Circa 1990 on a Berkeley version, the abbreviation expanded repeatedly, like this: the the the the the ... until a memory error occurred and vi quit. When tested, we obtained the following results on these vi versions:
We recommend that you avoid repeating your abbreviation as part of the defined phrase. 7.3.2. Using the map CommandWhile you're editing, you may find that you are using a command sequence frequently, or you may occasionally use a very complex command sequence. To save yourself keystrokes, or the time that it takes to remember the sequence, you can assign the sequence to an unused key by using the map command. The map command acts a lot like ab except that you define a macro for vi's command mode instead of for insert mode.
Before you can start creating your own maps, you need to know the keys not used in command mode that are available for user-defined commands:
NOTE: The = is used by vi if Lisp mode is set, and to do text formatting by several of the clones. In many modern versions of vi, the _ is equivalent to the ^ command, and elvis and vim have a "visual mode" that uses the v, V, and ^V keys. The moral is to test your version carefully. Depending on your terminal, you may also be able to associate map sequences with special function keys. With maps you can create simple or complex command sequences. As a simple example, you could define a command to reverse the order of words. In vi, with the cursor as shown: you can the scroll page the sequence to put the after scroll would be dwelp: delete word, dw; move to the end of next word, e; move one space to the right, l; put the deleted word there, p. Saving this sequence: :map v dwelp enables you to reverse the order of two words at any time in the editing session with the single keystroke v. 7.3.3. Protecting Keys from Interpretation by exNote that when defining a map, you cannot simply type certain keys, such as RETURN, ESC, BACKSPACE, and DELETE as part of the command to be mapped, because these keys already have meaning within ex. If you want to include one of these keys as part of the command sequence, you must escape the normal meaning by preceding the key with CTRL-V. The keystroke ^V appears in the map as the ^ character. Characters following the ^V also do not appear as you expect. For example, a carriage return appears as ^M, escape as ^[, backspace as ^H, and so on. On the other hand, if you want to use a control character as the character to be mapped, in most cases all you have to do is hold down the CTRL key and press the letter key at the same time. So, for example, all you need to do in order to map ^A is to type:
There are, however, three control characters that must be escaped with a ^V. They are ^T, ^W, and ^X. So, for example, if you want to map ^T, you must type: :map CTRL-V CTRL-T sequence The use of CTRL-V applies to any ex command, not just a map command. This means that you can type a carriage return in an abbreviation or a substitution command. For example, the abbreviation: :ab 123 one^Mtwo^Mthree expands to this: one two three (Here we show the sequence CTRL-V RETURN as ^M, the way it would appear on your screen.) You can also globally add lines at certain locations. The command: :g/^Section/s//As you recall, in^M&/ inserts, before all lines beginning with the word Section, a phrase on a separate line. The & restores the search pattern. Unfortunately, one character always has special meaning in ex commands, even if you try to quote it with CTRL-V. Recall that the vertical bar (|) has special meaning as a separator of multiple ex commands. You cannot use a vertical bar in insert mode maps. Now that you've seen how to use CTRL-V to protect certain keys inside ex commands, you're ready to define some powerful map sequences. 7.3.4. Complex Mapping ExampleAssume 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 would like to convert this glossary list to troff format, so that it looks like this: .IP "map" 10 n An ex command... The best way to define a complex map is to do the edit once manually, writing down each keystroke that you have to type. Then recreate these keystrokes as a map. You want to:
That will be quite an editing chore if you have to repeat it more than just 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~ Note that you have to "quote" both the ESC and RETURN characters with CTRL-V. ^[ 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 baud rate you can actually see the edits happening individually. At a fast baud rate it will seem to happen by magic. Don't be discouraged if your first attempt at key mapping 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. 7.3.5. More Examples of Mapping KeysThese examples will give you an idea of the clever shortcuts possible when defining keyboard maps:
In the previous example, even though ^J is a vi command (it moves the cursor down a line), this key is safe to map because it's really the same as the j command. There are many keys that either perform the same tasks as other keys or that are rarely used. However, you should be familiar with the vi commands before you boldly disable their normal use by using them in map definitions. 7.3.6. Mapping Keys for Insert ModeNormally, maps apply only to command mode—after all, in insert mode, keys stand for themselves and shouldn't be mapped as commands. However, by adding an exclamation mark (!) to the map command, you can force it to override the ordinary meaning of a key and produce the map in insert mode. This feature is useful when you find yourself in insert mode but need to escape briefly to command mode, run a command, and then return to insert mode. For example, suppose you just typed a word but forgot to italicize it (or place quotes around it, etc.). You can define this map: :map! + ^[bi<I>^[ea</I> Now, when you type a + at the end of a word, you will surround the word with HTML italicization codes. The + won't show up in the text. The sequence above escapes to command mode (^[), backs up to insert the first code (bi<I>), escapes again (^[), and moves ahead to append the second code (ea</I>). Since the map sequence begins and ends in insert mode, you can continue entering text after marking the word. Here's another example. Suppose that you've been typing your text, and you realize that the previous line should have ended with a colon. You can correct that by defining this map sequence:[32]
:map! % ^[kA:^[jA Now, if you type a % anywhere along your current line, you'll append a colon to the end of the previous line. This command escapes to command mode, moves up a line, and appends the colon (^[kA:). The command then escapes again, moves down to the line you were on, and leaves you in insert mode (^[jA). Note that we wanted to use uncommon characters (% and +) for the previous map commands. When a character is mapped for insert mode, you can no longer type that character as text. To reinstate a character for normal typing, use the command: :unmap! x where x is the character that was previously mapped for insert mode. (Although vi will expand x on the command line as you type it, making it look like you are unmapping the expanded text, it will correctly unmap the character.) Insert-mode mapping is often more appropriate for tying character strings to special keys that you wouldn't otherwise use. It is especially useful with programmable function keys. 7.3.7. Mapping Function KeysMany terminals have programmable function keys (which are faithfully emulated by today's terminal emulators on bitmapped workstations). You can usually set up these keys to print whatever character or characters you want using a special setup mode on the terminal. However, keys programmed using a terminal's setup mode only work on that terminal; they may also limit the action of programs that want to set up those function keys themselves. ex allows you to map function keys by number, using the syntax: :map #1 commands for function key number 1, and so on. (It can do this because the editor has access to the entry for that terminal found in either the terminfo or termcap database and knows the escape sequence normally put out by the function key.) As with other keys, maps apply by default to command mode, but by using the map! commands as well, you can define two separate values for a function key—one to be used in command mode, the other in insert mode. For example, if you are an HTML user, you might want to put font-switch codes on function keys. For example: :map #1 i<I>^[ :map! #1 <I> If you are in command mode, the first function key will enter insert mode, type in the three characters <I>, and return to command mode. If you are already in insert mode, the key will simply type the three-character HTML code. NOTE: If function keys have been redefined in the terminal's setup mode, the #n syntax might not work since the function keys no longer put out the expected control or escape sequence as described in its terminal database entry. You will need to examine the terminfo source (or termcap entry) for your terminal and check the definitions for the function keys. In addition, there are some terminals whose function keys perform only local actions and don't actually send any characters to the computer. Such function keys can't be mapped. The terminal capabilities k1, k2 through k0 describe the first ten function keys. The capabilities l1, l2 through l0 describe the remaining function keys. Using your terminal's setup mode, you can change the control or escape sequence output by the function key to correspond with the terminfo or termcap entry. (For more information, see termcap & terminfo, published by O'Reilly & Associates.) If the sequence contains ^M, which is a carriage return, press CTRL-M. For instance, in order to have function key 1 available for mapping, the terminal database entry for your terminal must have a definition of k1, such as: k1=^A@^M In turn, the definition: ^A@^M must be what is output when you press that key. To see what the function key puts out, use the od (octal dump) command with the -c option (show each character). You will need to press RETURN after the function key, and then CTRL-D to get od to print the information. For example: $ od -c ^[[[A ^D 0000000 033 [ [ A \n 0000005 Here, the function key sent Escape, two left brackets, and an A. 7.3.8. Mapping Other Special KeysMany keyboards have special keys, such as HOME, END, PAGE UP, and PAGE DOWN that duplicate commands in vi. If the terminal's terminfo or termcap description is complete, vi will be able to recognize these keys. But if it isn't, you can use the map command to make them available to vi. These keys generally send an escape sequence to the computer—an escape character followed by a string of one or more other characters. In order to trap the escape, you should press ^V before pressing the special key in the map. For example, to map the HOME key on the keyboard of an IBM PC to a reasonable vi equivalent, you might define the following map: :map CTRL-V HOME 1G This appears on your screen as: :map ^[[H 1G Similar map commands display as follows: :map CTRL-V END G displays :map ^[[Y G :map CTRL-V PAGE UP ^F displays :map ^[[V ^F :map CTRL-V PAGE DOWN ^B displays :map ^[[U ^B You'll probably want to place these maps in your .exrc file. Note that if a special key generates a long escape sequence (containing multiple non-printing characters), ^V quotes only the initial escape character, and the map doesn't work. You will have to find the entire escape sequence (perhaps from the terminal manual) and type it in manually, quoting at the appropriate points, rather than simply pressing ^V and then the key. 7.3.9. Mapping Multiple Input KeysMapping multiple key strokes is not restricted just to function keys. You can also map sequences of regular keystrokes. This can help make it easier to enter certain kinds of text, such as SGML or HTML. Here are some :map commands, thanks to Jerry Peek, co-author of O'Reilly's Learning the UNIX Operating System, which make it easier to enter SGML markup. (The lines beginning with a double quote are comments. This is discussed below in Section 7.4.4.) " ADR: need this :set noremap " bold: map! =b </emphasis>^[F<i<emphasis role=bold> map =B i<emphasis role=bold>^[ map =b a</emphasis>^[ " Move to end of next tag: map! =e ^[f>a map =e f> " footnote (tacks opening tag directly after cursor in text-input mode): map! =f <footnote>^M<para>^M</para>^M</footnote>^[kO " Italics ("emphasis"): map! =i </emphasis>^[F<i<emphasis> map =I i<emphasis>^[ map =i a</emphasis>^[ " paragraphs: map! =p ^[jo<para>^M</para>^[O map =P O<para>^[ map =p o</para>^[ " less-than: map! *l < ... Using these commands, to enter a footnote you would enter insert mode, and type =f. vi would then insert the opening and closing tags, and leave you in insert mode between them: All the world's a stage.<footnote> <para> _ </para> </footnote> Needless to say, these macros proved quite useful during the development of this book. 7.3.10. @-FunctionsNamed buffers provide yet another way to create "macros"—complex command sequences that you can repeat with only a few keystrokes. If you type a command line in your text (either a vi sequence or an ex command preceded by a colon), then delete it into a named buffer, you can execute the contents of that buffer with the @ command. For example, open a new line and enter: This will appear as: cwgadfly^[ on your screen. Press ESC again to exit insert mode, then delete the line into buffer g by typing "gdd. Now whenever you place the cursor at the beginning of a word and type @g, that word in your text will be changed to gadfly. Since @ is interpreted as a vi command, a dot (.) will repeat the entire sequence, even if the buffer contains an ex command. @@ repeats the last @, and u or U can be used to undo the effect of @. This is a simple example. @-functions are useful because they can be adapted to very specific commands. They are especially useful when you are editing between files, because you can store the commands in their named buffers and access them from any file you edit. @-functions are also useful in combination with the global replacement commands discussed in Chapter 6. 7.3.11. Executing Buffers from exYou can also execute text saved in a buffer from ex mode. In this case, you would enter an ex command, delete it into a named buffer, and then use the @ command from the ex colon prompt. For example, enter the following text: ORA publishes great books. ORA is my favorite publisher. 1,$s/ORA/O'Reilly \& Associates/g With your cursor on the last line, delete the command into the g buffer: "gdd. Move your cursor to the first line: kk. Then execute the buffer from the colon command line: :@gRETURN. Your screen should now look like this: O'Reilly & Associates publishes great books. O'Reilly & Associates is my favorite publisher. Some versions treat * identically to @ when used from the ex command line. In addition, if the buffer character supplied after the @ or * command is *, the command will be taken from the default (unnamed) buffer. Copyright © 2003 O'Reilly & Associates. All rights reserved. |
||||
|