UNIX Hints & Hacks |
||||||||||||||||||||||||||||||||
Chapter 8: Editors |
|
|||||||||||||||||||||||||||||||
|
You have the ability within the editor to set up macros to perform various tasks and functions by mapping them to keystrokes. Like the abbreviation, macros can be defined either in the .exrc file or when you are in the command line mode.
Flavors: AT&T, BSD
Shells: All
Syntax:
map! name sequence map name sequence
There are two different types of mapping commands. One performs the commands in insert mode ( map!); the other does so in the command mode ( map). Macros that are mapped to the insert mode act much like an abbreviation, but with more flexibility. Those macros that are mapped to the command mode also have the capability to issue commands and execute UNIX commands outside the editor. There are three parts to the syntax of a macro. First is the type of map, followed by the map name (or key sequence) and then the sequence that it will be substituted with.
There are basic rules to macros you might want to follow and in other cases you might want to conform to:
Map names should be unique from other commands in the editor. If you define a macro with the same name as an already-reserved command name, your macro takes precedence over the existing command. You can begin with a backslash ( \) or a semicolon ( ;) to add a larger scope to the names available.
You cannot use pipes ( |) or the macros will not function. If the pipes exist within a definition in the .exrc file, you run the risk of corrupting the rest of the macro definitions. Some versions of vi do support carets ( ^) to perform the function that is equivalent to a pipe.
Never use a partial word for the name of a map. If you define a map name to be ap you will never be able to type any words that begin with the letters ap ( apple, Apache, approve, ape, apogee, and so on). Place a backslash ( \) or semicolon ( ;) in front if you want to use those specific letters. The map names ;ap and \ap are valid names.
To configure control characters, use Ctrl-V followed by the control character you want to use. If you want to embed a carriage return ( ^M) you would use Ctrl-V and Ctrl-M; this would be interpreted as a Ctrl-M.
Here is what a simple insert mode macro looks like. If you enter this map from the command line mode or add it to your .exrc file, you will notice some differences in the way macros are handled versus abbreviations.
map! myboss Mr. Jeff Monroe
When you begin to type the map name, myboss, you will notice something a little different from how the macros are working. Macros never echo any characters when you type the name, unlike abbreviations. Also, a macro will perform its function when the last letter of the map name is typed. They do not wait until a Spacebar, the Enter key, or a metacharacter is typed.
A macro that is mapped to a command mode key sequence has the capability to execute a series of commands to perform a certain function. If you want to append some data to the end of the last line of the file you are working on, you have to enter the commands that get you there. You have to move the cursor to the bottom (Shift-G), go to the end of the line ($), and issue an append ( a) to put you into insert mode. This can all be done in one macro.
map v G$a
When you type the letter v in the command mode, you are instantly placed at the end of the file and in insert mode after the last character.
There are some other things to note as well about macros. They can be executed in the middle of words and lines. They do not need to be separated by spaces or at the start of a new line, much like abbreviations.
Macros can also use the function keys along the top of the keyboard (F1-F12) by naming the map #n, where n is the number of the function key. One such function could be to tell what time it is. If you have no way to tell what time it is on the computer without exiting the editor, a function key can be mapped to F1 that executes the UNIX date command:
map #1 :!date^v^m
Type F1 and you will see
:!date [No write since last change] Fri Jan 1 01:31:38 PST 1999 [Hit return to continue]
When the F1 key is typed in the command mode, the macro performs a function that puts the editor into the command line mode ( :), and executes ( !) the command date. The command date actually does not get executed until you type the Enter key. So at the end of the macro a Ctrl-v followed by a Ctrl-m are added for the command to execute. If you leave off the ending control characters, the macro only sets up the command and waits for you to type the Enter key. Leaving off the Enter key would be a good idea if you want to add certain arguments or options or filename to a command, such as ls, cat, more, or find.
There is a way to join multiple lines together so that more than one macro doesn't have to be created. If you have multiple lines that you want displayed, you don't need to build macros for each line. If you need both a formal and casual signature macro you need three lines to achieve this:
map! ;name John Fredrick Doe map! ;sig1 Senior UNIX Administrtaor/I.S. Dept. map! ;sig2 Jet-Ski Enthusiast
Then you select the two macros that make up the signature file you want. You still have to type the Enter key after each one to put them on a new line.
;name ;sig1
Here is how you can save keystrokes and set up multiple lines within a single macro; simply apply Ctrl-v + Ctrl-m between the two lines.
map! ;sig1 John Fredrick Doe^v^mSenior UNIX Administrtaor/I.S. Dept.^v^m map! ;sig2 John Fredrick Doe^v^mJet-Ski Enthusiast^v^m
Now all you have to do is pick ;sig1 or ;sig2 and both lines are displayed with the appropriate carriage returns applied to the end of each line.
Macros don't have to be confined to one mode. You can have complete control switching from insert mode to command mode and back to insert. You can also switch from command mode to command line mode. I know it all sounds kind of confusing, but it will make sense.
There are some words that you find yourself constantly performing a search on. You map a macro so that, at a keystroke, the cursor moves to the occurrence of the pattern you regularly search. To perform this function, you have to switch from the command mode and then to the command line mode:
map ;10 /129.134.10^v^m map ;20 /129.134.20^v^m
If you have to search through the /etc/hosts table for systems that reside on particular subnets you can map a search to the specific subnets. When you find the first occurrence, you can use the n key to find the remaining patterns that match the string.
You can also perform more advanced functions that switch back and forth between the insert and command modes. One such use for this function is inserting text into a line after that entire line has been displayed.
There are many instances where you are constantly sending messages regarding outages, meeting notices, thank you letters, and, if you're lucky enough, a class or conference. There is one thing that all those have in common. You have to associate a date with them. Here are some macros that put together the message and enable you to enter the date after the line has been entered.
map ;thx Hey , I really appreciated all the help you gave me!^vESC11bi map ;class I will be gone for 4 days on for a class.^vESC4bi map ;out We are scheduling an OUTAGE for . Is this okay?^vESC5bi map ;meet There is a meeting at to discuss when the next meeting is.^vESC8bi
When you are in insert mode and you type the name of one of the macros, the first thing it does is display the entire line. When the line is displayed, a series of events takes place. A Ctrl-v is applied to force the switch to the command mode with the Esc key, the cursor is then sent back ( b) to the point where the date needs to be entered manually, and you are placed back into the insert mode ( i).
With the capability to manipulate the cursor and the various modes, building HTML files is not much easier to set up. Complete blocks of code can be built into one macro:
map! ;ti <TITLE></TITLE>^vESCbhhi map! ;bd <BODY BGCOLOR="#FFFFFF" TEXT="#000000">^v^m map! ;ce <CENTER></CENTER>^vESCbhhi map! ;ah <A HREF=""></A>^vESC?"^v^mi map! ;im <IMG SRC="" ALT="">^vESCBhhi map! ;bo <B></B>^vESChhhi map! ;br <BR>^v^m map! ;tb <TABLE border=1>^v^m <TR>^v^m <TD></TD>^v^m </TR>^v^m</TABLE>^vESC2k2ba
From these macros you can quickly and painlessly build a simple Web page. You can't cut-and-paste the macros into place, because they have embedded mode changes within them. The earlier abbreviations had none, and cutting-and-pasting the data would have worked, but it is not advisable to do it with macros. The data will come out entirely different than what you expect.
;ti ;bd ;ce;bo ;br ;im ;ah ;tb
Line 1: Build the TITLE entry with the cursor in the center waiting for input.
Line 2: Display a fixed BODY setting.
Line 3: Build a CENTER entry with the cursor in the center of the entry waiting for input. The BOLD entry is the input and it then waits for input.
Line 4: Apply a blank line.
Line 5: Build an IMAGE entry and move the cursor so the path of the source can be entered.
Line 6: Build an anchor HYPERLINK, and move the cursor back to enter the path or URL of the link.
Line 7: Build a simple standard table and move the cursor back up to the TABLE DATA area for input.
Here is the final result of the HTML macros. The solid square () represents the areas where you would be prompted to enter data after the macro finishes the function that was defined for it.
<TITLE></TITLE> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <CENTER><B></B></CENTER> <BR> <IMG SRC="" ALT=""> <A HREF="">Enter Text</A> <TABLE border=1> <TR> <TD></TD> </TR> </TABLE>
You can see how this can greatly increase creation time for building HTML files for Web pages. You should be able to build off of these to create your own macros from the HTML tags that you use the most.
HTML Macro Table-- http://www.avalon.net/~drenze/vi/macros/HTML.html
Mastering the VI editor-- http://www.eng.hawaii.edu/Tutor/vi.html
UNIX Hints & Hacks |
||||||||||||||||||||||||||||||||
Chapter 8: Editors |
|
|||||||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.