Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have autoindent enabled in my .vimrc file but have run into an annoying bug/feature. For example, when I'm tabbed in 3 times, and I hit return, the new line is also tabbed in 3 times. Then when I hit enter again, that new line is also indented 3 times, as it should. The problem occurs when I go back up to the previous line (the first of the 2 new lines). VIM automatically removes the whitespace because it saw it as an empty line.

Is there a way to disable this from happening? I'd like to be able to back to coding like this:

function test(){ <return> <return> } <up> <right>

Thanks!

share|improve this question

2 Answers 2

up vote 2 down vote accepted

I don't know of any option setting that would make Vim automatically leave those lines with the leading whitespace. The usual solution is to change your editing style so that you don't lose that whitespace, or to use a normal-mode command such as o, O or S to start a new line so that the indenting is done automatically, or force indenting on the current line in insert mode with Ctrl-T or Ctrl-F.

However, if you really want to use that particular key sequence and not lose your indenting, then I think this mapping will work.

:inoremap <Return> <Space><BS><Return>

By putting a space (or any character) on the line, then backspacing over it, you're telling Vim that it's a non-empty line and it will leave the leading whitespace alone.

share|improve this answer

Remapping the <Return> or <CR> seems great. It does have a downside: it makes using Vim's auto-complete (<C-n>) harder to use. It also clashes with plugins that rely on the default behaviour, e.g. supertab.

This page: http://vim.wikia.com/wiki/Get_the_correct_indent_for_new_lines_despite_blank_lines provides an alternate solution: adding a function to get the proper indent despite previous blank lines.

share|improve this answer

Your Answer

 
discard

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.