Vim Tips Wiki
(adjust previous/next navigation + minor manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
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 <tt>:substitute</tt> command, both of which will become cumbersome if there are many occurrences. Using a parameter substitution engine, such as VPars ({{script|id=1696}}) can relieve you of much of this burden.
+
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 <code>:substitute</code> command, both of which will become cumbersome if there are many occurrences. Using a parameter substitution engine, such as VPars ({{script|id=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:
 
For example, I often use in my scripts a command sequence like this to optionally initialize some script local variable from a global one:
Line 30: Line 30:
 
</pre>
 
</pre>
   
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 <tt>"qy</tt> or equivalent:
+
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 <code>"qy</code> or equivalent:
   
 
<pre>
 
<pre>
Line 38: Line 38:
 
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.
 
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 <tt>@q</tt>. Vim will insert the snippet and call VPars with the ",jj" shortcut. VPars in turn will stop at the first variable (# denotes the cursor):
+
Now all I have to do is to position the cursor at the line where the snippet is to be inserted, and then issue <code>@q</code>. Vim will insert the snippet and call VPars with the ",jj" shortcut. VPars in turn will stop at the first variable (# denotes the cursor):
   
 
<pre>
 
<pre>
Line 44: Line 44:
 
</pre>
 
</pre>
   
and wait there, in insert mode already, for some replacement value. Just type it in and press the <F3> function key (or use the <tt>,jj</tt> 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 <tt>@q</tt> again and repeat the game as many times as wanted.
+
and wait there, in insert mode already, for some replacement value. Just type it in and press the <F3> function key (or use the <code>,jj</code> 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 <code>@q</code> again and repeat the game as many times as wanted.
   
 
==Comments==
 
==Comments==

Latest revision as of 06:26, 13 July 2012

Tip 1438 Printable Monobook Previous Next

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:

0"mPV4j,jj

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.

Comments[]