Vim Tips Wiki
Register
(Change to TipImported template + severe manual clean)
(Move categories to tip template)
Line 1: Line 1:
{{Duplicate|1019}}
 
 
{{review}}
 
{{review}}
 
{{TipImported
 
{{TipImported
|id=1327
+
|id=644
|previous=1326
+
|previous=643
|next=1330
+
|next=645
|created=September 10, 2006
+
|created=January 27, 2004
 
|complexity=basic
 
|complexity=basic
|author=Decker
+
|author=stsi
|version=5.7
+
|version=6.0
|rating=8/8
+
|rating=24/8
  +
|category1=
  +
|category2=
 
}}
 
}}
  +
In 'smartindent' mode, '#' removes the indent if it is the first char on the line. That can be very annoying.
The default behavior of the 'autoindent' option is as follow:
 
   
  +
From Vim's help:
:Copy indent from current line when starting a new line (typing <CR> in Insert mode or when using the "o" or "O" command). If you do not type anything on the new line except <BS> or CTRL-D and then type <Esc> or <CR>, the indent is deleted again.
 
  +
:When typing '#' as the first character in a new line, the indent for that line is removed, the '#' is put in the first column. The indent is restored for the next line. If you don't want this, use this mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H.
   
  +
This helps, but it fails to work when placed in vimrc.
I find that behavior quite annoying since the cursor jumps to the left when empty lines are inserted and 'escape' is pressed. Fortunately, it is possible to force Vim to keep the indentation with the following lines in your vimrc:
 
  +
  +
The following mapping works in any case:
   
 
<pre>
 
<pre>
  +
:inoremap # a#^Oh^Ox^OA, where ^O is entered with CTRL-V CTRL-O.
inoremap &lt;Enter&gt; &lt;Enter&gt;&lt;space&gt;&lt;bs&gt;
 
nnoremap o o&lt;space&gt;&lt;bs&gt;
 
nnoremap O O&lt;space&gt;&lt;bs&gt;
 
 
</pre>
 
</pre>
   
 
==Comments==
 
==Comments==
  +
Too bad that map doesn't work when there are characters on the line after where you are trying to insert the #, then you wind up with an "a" instead. Instead try
  +
  +
<pre>
 
:inoremap # a#&lt;Left&gt;&lt;BS&gt;&lt;Right&gt;
  +
</pre>
  +
  +
----
  +
Just do as the help on smartindent states and add the following line to your .vimrc file:
  +
  +
<pre>
  +
:inoremap # X&lt;C-H&gt;#
  +
</pre>
  +
  +
----
  +
Nice, but does not help for indenting with '&gt;&gt;' or '&lt;&lt;'. The lines are still ignored.
   
 
----
 
----

Revision as of 04:03, 25 April 2008

Tip 644 Printable Monobook Previous Next

created January 27, 2004 · complexity basic · author stsi · version 6.0


In 'smartindent' mode, '#' removes the indent if it is the first char on the line. That can be very annoying.

From Vim's help:

When typing '#' as the first character in a new line, the indent for that line is removed, the '#' is put in the first column. The indent is restored for the next line. If you don't want this, use this mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H.

This helps, but it fails to work when placed in vimrc.

The following mapping works in any case:

:inoremap # a#^Oh^Ox^OA, where ^O is entered with CTRL-V CTRL-O.

Comments

Too bad that map doesn't work when there are characters on the line after where you are trying to insert the #, then you wind up with an "a" instead. Instead try

:inoremap # a#<Left><BS><Right>

Just do as the help on smartindent states and add the following line to your .vimrc file:

:inoremap # X<C-H>#

Nice, but does not help for indenting with '>>' or '<<'. The lines are still ignored.