Wikia

Vim Tips Wiki

Changes: Restoring indent after typing hash

Edit

Back to page

(Suggest disabling smartindent)
(Adjust previous/next navigation)
Line 1: Line 1:
  +
{{Duplicate|1019}}
 
{{review}}
 
{{review}}
 
{{TipImported
 
{{TipImported
|id=644
+
|id=1327
|previous=643
+
|previous=1326
|next=645
+
|next=1332
|created=January 27, 2004
+
|created=September 10, 2006
 
|complexity=basic
 
|complexity=basic
|author=stsi
+
|author=Decker
|version=6.0
+
|version=5.7
|rating=24/8
+
|rating=8/8
 
|category1=
 
|category1=
 
|category2=
 
|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 <CR> <CR><Space><BS>
  +
nnoremap o o<Space><BS>
  +
nnoremap O O<Space><BS>
 
</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#<Left><BS><Right>
 
</pre>
 
 
----
 
Just do as the help on smartindent states and add the following line to your .vimrc file:
 
 
<pre>
 
:inoremap # X<C-H>#
 
</pre>
 
 
----
 
Nice, but does not help for indenting with '>>' or '<<'. The lines are still ignored.
 
 
----
 
 
You can avoid all this nastiness by just not enabling 'smartindent'. It's not very smart and really only makes sense for C-like languages.
 

Revision as of 02:54, July 20, 2009

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 1327 Printable Monobook Previous Next

created September 10, 2006 · complexity basic · author Decker · version 5.7


The default behavior of the 'autoindent' option is as follow:

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.

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:

inoremap <CR> <CR><Space><BS>
nnoremap o o<Space><BS>
nnoremap O O<Space><BS>

Comments

Around Wikia's network

Random Wiki