Reputation:
In vim with smartindent on:
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.
Is there any way to keep this indentation and not have it deleted?
Upvotes: 6
Views: 3188
Reputation: 158
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.
Upvotes: 1
Reputation: 204678
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.
Upvotes: 8
Reputation: 5807
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.
Upvotes: 1
Reputation: 992887
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.
Upvotes: 2