Vim Tips Wiki
Register
(Move categories to tip template)
(Remove html character entities)
Line 18: Line 18:
 
<pre>
 
<pre>
 
" insert #if 0 - #endif around block of code
 
" insert #if 0 - #endif around block of code
map ;' mz'aO&lt;Esc&gt;i#if 0&lt;Esc&gt;'zo&lt;Esc&gt;i#endif&lt;Esc&gt;
+
map ;' mz'aO<Esc>i#if 0<Esc>'zo<Esc>i#endif<Esc>
 
</pre>
 
</pre>
   
Line 25: Line 25:
   
 
<pre>
 
<pre>
vmap ;' '&lt;O&lt;Esc&gt;i#if 0&lt;Esc&gt;'&gt;o&lt;Esc&gt;i#endif&lt;Esc&gt;
+
vmap ;' '<O<Esc>i#if 0<Esc>'>o<Esc>i#endif<Esc>
 
</pre>
 
</pre>
   
Line 36: Line 36:
   
 
----
 
----
I tweaked it a bit by using 0&lt;C-D&gt; (instead of &lt;Esc&gt;i) and made a version for Perl by replacing '#if 0' with '=pod' and '#endif' with '=cut'.
+
I tweaked it a bit by using 0<C-D> (instead of <Esc>i) and made a version for Perl by replacing '#if 0' with '=pod' and '#endif' with '=cut'.
   
 
----
 
----

Revision as of 05:27, 29 September 2008

Tip 249 Printable Monobook Previous Next

created May 22, 2002 · complexity basic · author David Thompson · version 5.7


One of my favorite macros that I use in vim (and vi) inserts a #if 0 #endif sandwich around a block of code. I always map this to the 2 key sequence ;' which is the semi-colon followed by the single quote. Look at your keyboard, you will notice these keys are adjacent to one another. I like this mapping because it's very fast, my fingers easily roll from one key to the next, obviously YMMV.

To use this mapping, go to the line of code that you want the '#if 0' to be on, type ma to mark this line with the marker a, then move to the line that should be last line just above the '#endif' and press ;'

" insert #if 0 - #endif around block of code
map ;' mz'aO<Esc>i#if 0<Esc>'zo<Esc>i#endif<Esc>

Comments

You could also modify this just a bit and make it a vmap, that way you wouldn't need to mark the beginning of the line set (though not vi compatible).

vmap ;' '<O<Esc>i#if 0<Esc>'>o<Esc>i#endif<Esc>

What about removing '#if 0...'? It should be easy to do.

I like to do this with a range argument as well - "3;'" in your case.

However, you get all this with various schemes for commenting/uncommenting code...


I tweaked it a bit by using 0<C-D> (instead of <Esc>i) and made a version for Perl by replacing '#if 0' with '=pod' and '#endif' with '=cut'.