Vim Tips Wiki
Register
(restore deleted remarks)
(merge advice into tip proper)
Line 24: Line 24:
 
nnoremap O O<Space><BS>
 
nnoremap O O<Space><BS>
 
</pre>
 
</pre>
  +
  +
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==

Revision as of 14:01, 17 August 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>

If you are using filetype indentation (with filetype indent on in your vimrc) rather than using the 'autoindent' setting, this is unnecessary. After Vim removes the indentation from an empty line, you can press cc or S on that empty line and Vim will reapply the indentation rules. This happens because cc 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

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: class A extends B {<CR><CR>}<Up><Tab>A() {<CR><CR>}<Up><Tab>super();

class A extends B {
    A() {
        super();
    }
}

I assume most other people do this: class A extends B {<CR><Tab>A() {<CR><Tab>super();<CR><BS>}<CR><BS>}. I wouldn't say either way of typing is better than the other, since they both require the same number of keystrokes.

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 S :help S. That, and turn :filetype indent on, which removes the need to type <Tab> in the above code. (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! --Fritzophrenic 00:20, 17 August 2009 (UTC)
The Vim does everything correctly; don't try to fight it-tip? :) (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 S--I added it and cc to the list.
You added mappings nnoremap S S<Space><BS> and nnoremap cc cc<Space><BS>. However, Spiiph did not mean that, so I removed the mappings. Spiiph is saying that you should have file type indentation on (:filetype indent on in vimrc). And, if you want to add code to a blank line, do not press i or A to start insert mode. Instead, press S which will enter insert mode with the cursor correctly indented. I hope to fix all these messy indent tips in a week or two... 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: [Snip] restored above

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. 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! --Fritzophrenic 13:54, 17 August 2009 (UTC)