Vim Tips Wiki
(Move categories to tip template)
(Move categories to tip template)
Line 1: Line 1:
{{duplicate|1327}}
+
{{Duplicate|1019}}
 
{{review}}
 
{{review}}
 
{{TipImported
 
{{TipImported
|id=1019
+
|id=1327
|previous=1018
+
|previous=1326
|next=1020
+
|next=1330
|created=October 13, 2005
+
|created=September 10, 2006
 
|complexity=basic
 
|complexity=basic
|author=Char
+
|author=Decker
 
|version=5.7
 
|version=5.7
|rating=-1/1
+
|rating=8/8
 
|category1=
 
|category1=
 
|category2=
 
|category2=
 
}}
 
}}
  +
The default behavior of the 'autoindent' option is as follow:
The help for autindent says:
 
   
...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...
+
: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:
This give some difficulties (at least for me):
 
*If I type <Enter>, the cursor moves to the new line and indented, but when I type <Esc>, the indent is deleted (the cursor moves the the leftmost position)
 
*If I type <Enter>, and then paste something, the first line of the pasted text is not indented (the pasted text is not positioned from the cursor position).
 
   
To fix this, I use the mapping :
 
 
<pre>
 
<pre>
imap &lt;CR&gt; &lt;CR&gt; &lt;BS&gt;
+
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>
 
At least, now if I pasted some text after pressing &lt;Enter&gt;, it starts from the current cursor position.
 
   
 
==Comments==
 
==Comments==
what about simply pressing <tt>Ctrl-R"</tt> ?
 
 
----
 
Unfortunately, this also has the side-effect of messing up the autoindent feature of if / while / for statements (at least in Java)
 
 
----
 
You can press Ctrl+R_ to make sure indent would not be deleted.
 
 
----
 
See also [[VimTip1327]]
 
   
 
----
 
----

Revision as of 09:31, 25 April 2008

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 <Enter> <Enter><space><bs>
nnoremap o o<space><bs>
nnoremap O O<space><bs>

Comments