Vim Tips Wiki
(merge advice into tip proper)
Line 1: Line 1:
{{Duplicate|1019}}
+
{{duplicate|1327}}
 
{{review}}
 
{{review}}
 
{{TipImported
 
{{TipImported
|id=1327
+
|id=1019
|previous=1326
+
|previous=1018
|next=1332
+
|next=1020
|created=September 10, 2006
+
|created=October 13, 2005
 
|complexity=basic
 
|complexity=basic
|author=Decker
+
|author=Char
 
|version=5.7
 
|version=5.7
|rating=8/8
+
|rating=-1/1
 
|category1=
 
|category1=
 
|category2=
 
|category2=
 
}}
 
}}
  +
The help for autindent says:
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.
+
...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):
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:
 
  +
*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>
inoremap <CR> <CR><Space><BS>
+
imap <CR> <CR> <BS>
nnoremap o o<Space><BS>
 
nnoremap O O<Space><BS>
 
 
</pre>
 
</pre>
   
  +
At least, now if I pasted some text after pressing <Enter>, it starts from the current cursor position.
If you are using filetype indentation (with <tt>filetype indent on</tt> in your [[vimrc]]) rather than using the 'autoindent' setting, this is unnecessary. After Vim removes the indentation from an empty line, you can press <tt>cc</tt> or <tt>S</tt> on that empty line and Vim will reapply the indentation rules. This happens because <tt>cc</tt> works by deleting the entire line and then re-entering insert mode on a brand-new line (or as {{help|cc}} says, ''"delete {count} lines and start insert linewise"'').
 
   
 
==Comments==
 
==Comments==
  +
what about simply pressing <tt>Ctrl-R"</tt> ?
Thank you. This tip is very helpful. I was getting very annoyed with Vim deleting the blank lines because it interferes with the way I type.
 
   
  +
----
The way I type the following is: <code>class A extends B {<CR><CR>}<Up><Tab>A() {<CR><CR>}<Up><Tab>super();</code>
 
  +
Unfortunately, this also has the side-effect of messing up the autoindent feature of if / while / for statements (at least in Java)
<pre>
 
class A extends B {
 
A() {
 
super();
 
}
 
}
 
</pre>
 
   
  +
----
I assume most other people do this: <code>class A extends B {<CR><Tab>A() {<CR><Tab>super();<CR><BS>}<CR><BS>}</code>. I wouldn't say either way of typing is better than the other, since they both require the same number of keystrokes.
 
  +
You can press Ctrl+R_ to make sure indent would not be deleted.
:How does that interfere with Vim (correctly) deleting whitespace on empty lines? This tip is generally misguided, since most users who think they need it, really need to learn to use <tt>S</tt> {{help|S}}. That, and turn <tt>:filetype indent on</tt>, which removes the need to type <Tab> in the above code. ([[User:Spiiph|Spiiph]] 09:05, 15 August 2009 (UTC))
 
::Hmm. You just showed me something new! For some reason, it never occurred to me that "delete {count} lines and start insert linewise" would reapply the filetype indent, but after you pointed this out it becomes obvious. We should mention this fact in the tip, or create a new tip. I've always just used A<Tab><Tab>... until I had the indentation I wanted on empty lines. No longer! --[[User:Fritzophrenic|Fritzophrenic]] 00:20, 17 August 2009 (UTC)
 
:::The ''Vim does everything correctly; don't try to fight it''-tip? :) ([[User:Spiiph|Spiiph]] 01:29, 17 August 2009 (UTC))
 
::This tip is not the slightest bit misguided. Some people like to leave indentation on empty lines, so for those people Vim deleting whitespace on empty lines is not correct behavior. Anyway, thanks for mentioning <code>S</code>--I added it and <tt>cc</tt> to the list.
 
:::You added mappings <tt>nnoremap S S<Space><BS></tt> and <tt>nnoremap cc cc<Space><BS></tt>. However, Spiiph did not mean that, so I removed the mappings. Spiiph is saying that you should have file type indentation on (<tt>:filetype indent on</tt> in vimrc). And, if you want to add code to a blank line, do not press <tt>i</tt> or <tt>A</tt> to start insert mode. Instead, press <tt>S</tt> which will enter insert mode with the cursor correctly indented. I hope to fix all these messy indent tips in a week or two... [[User:JohnBeckett|JohnBeckett]] 07:45, 16 August 2009 (UTC)
 
   
  +
----
I would like my reply to 24.247.181.100 to stay here longer in case the user returns: ''<nowiki>[Snip]</nowiki> restored above''
 
  +
See also [[VimTip1327]]
   
  +
----
I hope to fix all these messy indent tips in a week or two, and certainly will find a place for Spiiph's advice which I will copy from this comment to some "how to indent" tip, and I will be merging any useful advice from this tip to somewhere else. [[User:JohnBeckett|JohnBeckett]] 03:22, 17 August 2009 (UTC)
 
:Sorry, I have no idea how I wound up deleting the comment, that was unintentional. I must have accidentally edited a historic version accessed via a diff link from "recent changes" rather than the latest version. I've restored the deleted remarks, and I'll be more careful from now on! --[[User:Fritzophrenic|Fritzophrenic]] 13:54, 17 August 2009 (UTC)
 

Revision as of 08:44, 23 August 2009

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