I've got a problem where in insert mode, if I type something like { that will cause indentation, then press enter twice, then match that bracket (such that there is a line in between the opening and closing bracket), all indentation on the line inbetween will be gone.

If that didn't make any sense, here is a video demonstrating the behavior.

This is what I have in my vimrc:

filetype indent on
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

How can I have the indentation stay, preferably without using an "autoclose plugin"?

  • That loss of indentation, afaik, isn't related to any indentation settings. I just do the following... iintmain() {<CR>}<ctrl-o>O, this will get you the result you want. – klaus Oct 13 '19 at 3:42

The easiest way is to change the keystrokes you use to start editing an empty line from a to cc. This allows the built-in cindent indentation to correctly adjust the indent when you enter insert mode, which it doesn't do when you specify that you want to append to the existing (blank) line.

Note that this will only work if cindent is actually switched on. This should be the case for you if you are editing a C file (and not just typing into an empty unsaved buffer with no name or filetype) because you have switched on file type detection and indent-handling with your filetype indent on command.

If you don't want to have to change your habits at all, that's easy enough to fix with a mapping that changes the behaviour of a when pressed on an empty line:

nnoremap <expr> a empty(getline('.')) ? 'cc' : 'a'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.