Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In some text editors (e.g. Kate, gedit), when auto indent is enabled, pressing return twice will leave a trailing whitespace (which I want):

if (code) {

While others cater to the coding standard where trailing spaces (even in blank lines) aren't allowed:

if (code) {


What annoys me about this is that if I arrow up after auto-indenting, the auto-indent is lost:

if (code) {

If I run vim and :set autoindent , I get the latter behavior.

My question is, how do I set vim to keep the trailing spaces rather than automatically removing them if they go unused?

share|improve this question
My initial reaction is that you're doing it wrong. By leaving blank lines indented like that you kill the possibility to use { or } (e.g. d{ etc). – hlovdal Oct 24 '11 at 7:16
up vote 3 down vote accepted

See this hint on the vim wiki for how to have correct indention even for empty lines. If you just want to keep the previous indent (ignoring what vim calculated as the correct indent) use let ind = indent(prevnonblank(v:lnum - 1)) like explained in a comment under the same wiki entry.

share|improve this answer

I found this solution to work for me:

:inoremap <Return> <Space><BS><Return>
share|improve this answer
This looks like a hack/workaround. Isn't there a proper option to turn off that "unindent blank line" behaviour? – Doub Oct 30 '12 at 11:26
Not that I know of - otherwise I would've posted it here. – Xiong Chiamiov Nov 15 '12 at 0:19

In my .vimrc:

" Change a blank line upon creation so that its leading spaces won't be removed
inoremap <Return> <Return><Space><BS>
nnoremap o o<Space><BS>
nnoremap O O<Space><BS>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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