created 2006 · complexity intermediate · author Bernd Pol · version n/a
The macro ability of Vim to execute the contents of a register (see :help q and :help @) comes in handy when one has to insert the same text over and over again. Usually, this snippet will not be used literally all over again, but contain variable parts which need to be adapted for every occurrence. Usually you would either enter the varying parts manually or use the :substitute command, both of which will become cumbersome if there are many occurrences. Using a parameter substitution engine, such as VPars (script#1696) can relieve you of much of this burden.
For example, I often use in my scripts a command sequence like this to optionally initialize some script local variable from a global one:
let s:name = "value" if exists( "g:global_name" ) let s:name = g:global_name endif
Here "name" is some variable name and "value" is the default value it will assume. Now, instead of explicitely hunting for "name" 4 times each time the snippet was inserted, I make them VPars variables by including them in "<| .. |>" delimiters, like this:
let s:<|name|> = "<|value|>" if exists( "g:global_<|name|>" ) let s:<|name|> = g:global_<|name|> endif
This snippet will then be saved in a suitable register (I use m for 'macro'). And in another register (the q register comes in very handy for this purpose), I put the following commands. Just type and then copy this sequence into the q register by "qy or equivalent:
This could as well be accomplished using the q keystroke recording function, which however would require a final <Esc> to get back to command mode again in order to stop recording. Unfortunately, this <Esc> gets recorded as well, causing Vim to later drop out of insert mode when executing the macro.
Now all I have to do is to position the cursor at the line where the snippet is to be inserted, and then issue @q. Vim will insert the snippet and call VPars with the ",jj" shortcut. VPars in turn will stop at the first variable (# denotes the cursor):
let s:<|#name|> = "<|value|>"
and wait there, in insert mode already, for some replacement value. Just type it in and press the <F3> function key (or use the ,jj shortcut). VPars will substitute <|name|> for this text at all occurrences immediately, and then wait for a <|value|> replacement. Enter text, press <F3>, and VPars will after substitution put the cursor at the end of the snippet. There you may issue @q again and repeat the game as many times as wanted.