Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
  +
{{duplicate|1327}}
 
{{review}}
 
{{review}}
 
{{TipImported
 
{{TipImported
|id=644
+
|id=1019
|previous=643
+
|previous=1018
|next=645
+
|next=1020
|created=January 27, 2004
+
|created=October 13, 2005
 
|complexity=basic
 
|complexity=basic
|author=stsi
+
|author=Char
|version=6.0
+
|version=5.7
|rating=24/8
+
|rating=-1/1
 
}}
 
}}
  +
The help for autindent says:
In 'smartindent' mode, '#' removes the indent if it is the first char on the line. That can be very annoying.
 
   
  +
...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...
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 give some difficulties (at least for me):
This helps, but it fails to work when placed in vimrc.
 
  +
*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).
The following mapping works in any case:
 
   
  +
To fix this, I use the mapping :
 
<pre>
 
<pre>
 
imap &lt;CR&gt; &lt;CR&gt; &lt;BS&gt;
:inoremap # a#^Oh^Ox^OA, where ^O is entered with CTRL-V CTRL-O.
 
 
</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> ?
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>
 
  +
Unfortunately, this also has the side-effect of messing up the autoindent feature of if / while / for statements (at least in Java)
:inoremap # a#&lt;Left&gt;&lt;BS&gt;&lt;Right&gt;
 
</pre>
 
   
 
----
 
----
  +
You can press Ctrl+R_ to make sure indent would not be deleted.
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>
 
   
 
----
 
----
  +
See also [[VimTip1327]]
Nice, but does not help for indenting with '&gt;&gt;' or '&lt;&lt;'. The lines are still ignored.
 
   
 
----
 
----

Revision as of 03:34, 16 December 2007

Duplicate tip

This tip is very similar to the following:

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

Tip 1019 Printable Monobook Previous Next

created October 13, 2005 · complexity basic · author Char · version 5.7


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...

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 :

imap <CR> <CR> <BS>

At least, now if I pasted some text after pressing <Enter>, it starts from the current cursor position.

Comments

what about simply pressing Ctrl-R" ?


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