What is a macro

A macro is just the simple replacement of an input key with a string of characters. The replacement string contains the keys you would have to type to perform the required function manually. As an example, let's look at the definition of the Ins key. Each time it is typed, it toggles option character insert (oi). You could perform this task manually by going to the command line and typing the command

oi~

which will toggle the option. You would then want to go back to the text area if that's where you came from. If you were already on the command line with a partially typed command then the situation is slightly more complex. You can't type in a new command on the command line without deleting what is currently there. To overcome this difficulty, the editor provides a very special character {command_char} which causes all input following it, up to the next newline to be collected (without echo) in a hidden command buffer which is then executed. By prefixing all commands with this character you can execute commands regardless of whether you are on the command line or in the text area. The value of the command character is hexadecimal FF and may be entered directly from your key board by holding down the Alt key and typing the Backspace key normally used for character delete. You can now toggle insert mode at any time by typing the string

{command_char}oi~<newline>

where {command char} is an AltBackspace and <newline> is either a carriage return or a linefeed. To turn this into a macro for the Ins key you would translate the value generated by the Ins key into the above string.

t \ab \ffoi~\0a
   |    |    |
   |    |    +--  Linefeed character.
   |    +-------  Command character.
   +------------  Hexadecimal value generated by
                  the Ins key.

If you leave off the command char then the string

oi~

followed by a newline would be entered at the active cursor. Likewise, if you leave off the newline, then the command will be collected, but you will have to supply the newline yourself by typing a carriage return.

Most macros are this simple. However, care should always be taken to ensure reasonable behavior when a requested operation may fail. An example of this is the down arrow key on the keypad. It is designed to move you down one line. This could be performed by executing the command

    .+1

which works fine until you try to move past the last line in your buffer where you will be greeted by an unpleasant error message which will quickly annoy any user. The simple solution is to limit the line address to remain within the buffer using the | operator. The resulting macro definition would be:

\ff.+1|\0a

which behaves in a friendlier fashion. As one final point, to prevent your screen from being re-centered when you attempt to leave the screen, you might add a Zap Cursor Lock command resulting in

\ff.+1|zcl\0a

Lets look at one more simple example illustrating the importance of planning your macro's behavior in all possible situations. You want the PgDn key to display the next page of your buffer. Your text area is 23 lines so you might simply attempt to move forward 23 lines via a

.+23

This has two problems

  1. You may not have another 23 lines in the buffer.
  2. If your current line was not on line 1, then you would miss part of the next page. You should be jumping forward relative to the address of the top line of the current screen. The PgDn is currently defined as
    
    
        t \aa \ff@+23|\0a
           |       | |
           |       | +----  Limit address to lie within buffer.
           |       +------  Address of top line on the screen.
           +--------------  Hexadecimal value generated by the
                            PgDn key.