Top  Previous  Next

Conditionals are used to selectively perform an editing operation. Conditional operations are indicated using square brackets “[]”. Commands enclosed in square brackets are only executed if the value of the Variable is true. For example:




This command line deletes escape characters ($1B), something that normally cannot be done. The command line starts by moving the cursor forward one character. If the cursor is on an escape character, the commands in the square brackets are executed. These commands move the cursor forward one character then delete the escape.


Conditionals can be nested, or placed inside loops. They cannot have a numeric value in front of them like other commands. For example, this would give an error:




Examples. The following examples illustrate the power of expressions and conditionals.


1. The command iteration count can be used to execute an operation a variable number of times. This is done using the underline character “_” which copies the value of the Variable into the iteration count. Since the value of the Variable is set by the value of the last expression evaluated, any command can be executed a calculated number of times. For example, the following command can be used to pad a string with spaces:


<Esc>S<Enter><Esc><Ctrl+Q>(8-C)<Ctrl+W>_I <Esc><Esc>


This command right-justifies a string that could be up to eight characters long. The string is assumed to be on a line by itself. The first command searches for the carriage return that terminates the line. The next command backs up to the last character of the string. The column position is then subtracted from 8. This gives the number of spaces needed to justify the string. The cursor is then moved to the beginning of the line, and the proper number of spaces are inserted.


2. You can also move to a position in the editor based on a calculated value. The colon command “:” moves the cursor to the location specified by the Variable. For example:




This macro moves the cursor 20 locations ahead if the character under the cursor is “a”.


3. This example displays the value of the character under the cursor in hex:




4. It is often useful to have the editor do something only if the user approves of the operation. For example, this searches for the word “the” and if the user hits the “+”  key, the searched "the" string is deleted.