Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Replace category 'C plus plus' with 'C++')
Line 12: Line 12:
 
If you try to impose any sort of alignment on your preprocessor directives, rather than just starting them on column 0, this mapping will align the #endif 'correctly' when you type '#en', start a new line, and bring you back to the correct alignment to edit code.
 
If you try to impose any sort of alignment on your preprocessor directives, rather than just starting them on column 0, this mapping will align the #endif 'correctly' when you type '#en', start a new line, and bring you back to the correct alignment to edit code.
   
  +
<pre>
inoremap &lt;buffer&gt; #en X&lt;BS&gt;&lt;Esc&gt;?#if&lt;CR&gt;"zy0^Og0"zpDa#endif&lt;CR&gt;X&lt;BS&gt;&lt;Esc&gt;?#end?-1&lt;CR&gt;^"zy0^O0"zpDa
+
inoremap &lt;buffer&gt; #en X&lt;BS&gt;&lt;Esc&gt;?#if&lt;CR&gt;"zy0^Og0"zpDa#endif&lt;CR&gt;X&lt;BS&gt;&lt;Esc&gt;?#end?-1&lt;CR&gt;^"zy0^O0"zpDa
  +
</pre>
   
 
==Comments==
 
==Comments==
 
This tip is currently at -1/1 - I'm not sure why, since I use it a lot. In any case, here's an updated version that handles nested #if blocks.
 
This tip is currently at -1/1 - I'm not sure why, since I use it a lot. In any case, here's an updated version that handles nested #if blocks.
   
  +
<pre>
inoremap #en #endif&lt;Esc&gt;bbd0%y0&lt;C-o&gt;0PA
+
inoremap #en #endif&lt;Esc&gt;bbd0%y0&lt;C-o&gt;0PA
  +
</pre>
   
 
I've stopped positioning the cursor on the next line because I found myself doing &lt;Esc&gt;dd too often.
 
I've stopped positioning the cursor on the next line because I found myself doing &lt;Esc&gt;dd too often.
Line 25: Line 29:
 
----
 
----
 
[[Category:C]]
 
[[Category:C]]
[[Category:C plus plus]]
+
[[Category:C++]]

Revision as of 06:12, 12 April 2008

Tip 251 Printable Monobook Previous Next

created May 24, 2002 · complexity basic · author Kartik Agaram · version 5.7


If you try to impose any sort of alignment on your preprocessor directives, rather than just starting them on column 0, this mapping will align the #endif 'correctly' when you type '#en', start a new line, and bring you back to the correct alignment to edit code.

inoremap <buffer> #en X<BS><Esc>?#if<CR>"zy0^Og0"zpDa#endif<CR>X<BS><Esc>?#end?-1<CR>^"zy0^O0"zpDa

Comments

This tip is currently at -1/1 - I'm not sure why, since I use it a lot. In any case, here's an updated version that handles nested #if blocks.

inoremap #en #endif<Esc>bbd0%y0<C-o>0PA

I've stopped positioning the cursor on the next line because I found myself doing <Esc>dd too often.

In a perfect world, we wouldn't have to deal with code with such a high density of preprocessor directives as to make indenting them, and therefore this tip, useful.