tags:

views:

479

answers:

4

In vim with smartindent on:

  1. press enter after say a if statement
  2. type in {
  3. press enter twice
  4. type in }
  5. if you hit up and go to the previous line, indentation is removed from the blank line.

Even the vim documentation says that "If you do not type anything on the new line except <BS> or CTRL-D and then type <Esc>, CTRL-O or <CR>, the indent is deleted again."

My question - is there any way to keep this indentation and not have it deleted?

+5  A: 

Use Shift+S to start editing on a blank line (from command mode, obviously). This will start your cursor off with the expected level of indentation.


Another doesn't-answer-the-question-as-asked-but-is-a-better-solution-overall:

When typing an opening brace in insert mode, this will insert a matching set of braces and leave the cursor on a new line in the middle.

:imap { {<CR>}<Esc>O

Similarly, this will auto-insert matching parens and square brackets.

:imap ( ()<Left>
:imap [ []<Left>

(Strip off the leading : when adding to vimrc.)

As I commented on Victor's answer, changing Vim's indentation behavior will leave "empty" lines containing extraneous spaces throughout your files. IMO, this is completely intolerable.

ephemient
I'm voting this up, in part, because I'd never heard of the <kbd> tag before.
Mark Biek
I'm not sure that this answers the question, precisely, but it's super-useful! How did I now know about this?
Emily
+2  A: 

When this happens to me, I sometimes use ddko (or ddO) to delete the line without enough spaces and open a new line with the correct indent. Or, I'll just press A and then Tab enough times to get to the correct indent.

Greg Hewgill
+1  A: 

the article here talks about you're very same problem, and what to put in vimrc to fix it.

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

I havn't exactly tested this tho.

also the same article links to a shorter alternate solution.

Victor
This will litter the file with "blank" lines containing nothing but spaces, which is rather ugly, especially with :set list. I'd prefer not to change Vim's normal indentation behavior.
ephemient
Thanks - op here, I guess I'll have to live with this solution. Not too bad.
A: 

My preferred method is {<CR>}<esc>shift+o as it outpaces {<CR><CR>}<esc>k shift+s by several strokes. I get in a rut with it, though, and end up just using o or O to grab new, properly-indented lines off an empty when I should be using S.

That is, set up your bracing structure and open line-above:

if (true) {
}//cursor here, press shift-o

And you get the indenting you expect.

The open-above trick isn't any fewer keypresses than <up><end><cr>, but with escape remapped and shift being chorded, you can throw it in quite fast.

Also, don't forget your manual indent reset and block-movement. If you're inside a mangled curly brace block, simply use ={ (or =i{ if you're on top of one of the braces). I use that when I have a Good Idea that needs to see text asap, and I don't worry about any formatting frippery until I take a breather.

Chad Wellington