Command Loops |
Top Previous Next |
Command Line Loops. Loops repeatedly execute a group of commands on the command line. They are similar to “for” loops in a programming language. Loops are created by enclosing a group of commands inside a set of braces “{}“. If a number is placed in front of the braces, every command inside the braces is executed that number of times. If an error is detected within the loop, the loop aborts immediately without executing any more commands in the braces. For example, if you are doing a search and the search fails, the loop stops before executing the rest of the commands inside the loop. Here is an example:
<Esc>10{SJULY <Ctrl+W><Ctrl+W>, <Esc>I2016<Esc>}<Esc><Esc>
This command line searches for any date in July and adds “2016” to it. It executes 10 times or until it fails to find the search string.
Loops can also be nested. For example:
<Esc>20{10Sfrog<Esc>Sdog<Esc>Ipig<Esc>}<Esc><Esc>
This command does 10 searches for a complex string then inserts the string “pig”. It does this whole operation 20 times.
If you use a “J” command within a loop, the “J” causes the execution to jump to the beginning of the loop, not to the beginning of the whole command line.
Errors cause the current loop to abort. If a loop is nested or if there are other commands on the command line after the loop, the editor continues to execute these commands. When there is an error within a loop, the editor clears the error as soon as it exits from the loop so it can continue executing commands outside the loop. This is useful for things like searching for the last occurrence of a string. For example, this locates and marks the innermost “begin-end” pair in a block of code:
<Esc>#{Sbegin<Esc>}I *<Esc>Send<Esc>I *<Esc><Esc>
Assuming you have a single procedure in the edit buffer, this command does an infinite search for “begin”. When the search fails, the cursor is located just after the last “begin”, which is the innermost “begin”. The code then places an asterisk after that “begin” then searches for the next “end”. This “end” is the corresponding innermost “end” and an asterisk is placed after it.
Conditional Loops. If you combine expressions and conditionals with loops, you can create very powerful macros. To facilitate the use of conditionals with loops, the editor has the semicolon “;” command. This forces a loop to terminate at the point it is encountered. This provides a way to terminate a loop based on a condition. For example:
<Esc>#{<Ctrl+A>(@=$1B)[<Ctrl+A>D;]}<Esc><Esc>
This command searches through the text for an escape character ($1B). When it finds an escape, it deletes it and stops.
Examples. The following examples illustrate the power of combining expressions and conditionals.
Search for and delete escape characters:
<Esc>#{(@=$1B)[<Ctrl+A>D;]<Ctrl+A>}<Esc><Esc>
Search for the first occurrence of a numeric character:
<Esc>#{<Ctrl+A>(@>=$30 & @<=$39)[;]}<Esc><Esc>
Count the number of "a" characters in the text and display the number on the status line:
<Esc>(0)#{sa<Esc>(%+1)}bn<Esc><Esc>
Move to the 12th column on the first line with more than 11 columns:
<Esc><Ctrl+S>#{<Ctrl+A>(C>=12)[;]}<Esc><Esc>
This macro generates a list of numbers 0 through 99 on the left side of the screen:
<Esc>(0)100{bdi<Enter><Esc>(%+1)}<Esc><Esc>
This macro lists the numbers 0 through 255 in hex on the left side of the screen:
<Esc>(0)255{BHI<Enter><Esc>(%+1)}<Esc><Esc>
This macro displays the hex code and its ASCII character for the full character set:
<Esc>(0)256{BHI - <Esc>BAI<Enter><Esc>(%+1)}<Esc><Esc>
|