Super Stutter Sequences for “vim”

Since the early days of UNIX, the vi editor had commands called Stutter Sequences. Two commonly used Stutter Sequence commands are dd to delete one or more lines or yy to yank (copy) one or more lines. Repeating a letter while typing is very easy and very intuitive.

Also for many decades, vi and vim had the facility to define abbreviations with the X command :ab . Abbreviations are used in Insert mode, Replace mode and Command-line mode.  If you enter a word that is an abbreviation, it is replaced with the word or sequence of words it stands for. This can be used to save typing for often-used long words. And you can use it to generate more complex blocks of text because it is possible to switch from insert mode to command mode and back in the replacement sequence.

Another useful vim function is the X command :map . It allows defining a vi command or insert sequence for pressing any key, including function keys. A series of :map and :ab commands are usually pre-defined in the vi support file ~/.exrc as part of a Linux distribution. I have extended ~/.exrc to provide extra mappings and abbreviations making up the Super Stutter Seqences.

A simple example of an abbreviation:
:ab hh    hello

:ab"hh<Space>" is expanded to "hello<Space>"

I call abbreviations consisting mostly of three identical letters followed by a non-letter “Super Stutter Sequences“. The replacement string of the Super Stutter Sequences consists of a string of insertions and vim commands and is rather cryptic. I will only show and analyze the first one in detail:

:ab iii    if ) {^M^T///^M^D}^[%F)i
Explanatory form (output by :ab listing):
:ab iii    if ) {<CR><C-T>///<CR><C-D>}<Esc>%F)i
To enter <CR> type ctrl-V <Enter> which shows up as ^M
To enter <C-T> type ctrl-V ctrl-T which shows up as ^T
To enter <C-D> type ctrl-V ctrl-D which shows up as ^D
To enter <Esc> type ctrl-V <Esc> which shows up as ^[

The replacement string starts with “if ) {“. <CR> starts a new line. <C-T> indents text in that line by 4 spaces to the right. “///” is inserted in that line at the new indent. Another <CR> starts a third line. <C-D> indents text 4 spaces to the left, where the closing brace “}” to finish the block is inserted. The insert cursor is now after the closing brace. An <Esc> follows, which turns on command mode. The vi command “%” moves the insert cursor to the matching opening brace in the first line. The vi command “F)” moves the insert cursor in front of the “)” after the “if“, which was inserted at the beginning. The vi command “i” restores insert mode at which point the abbreviation for “iii” is complete. The abbreviation trigger character “(” is now inserted before the “)” character leaving the insert cursor in insert mode between the parentheses.

NOTE: in the following expansions the character ‘|‘ represents the insert cursor and not the vertical bar character.

"iii(" is expanded to
if (|) {
    ///
}

Continue reading “Super Stutter Sequences for “vim””