5

Right now in Vim when I go to a new line (or press 'p' or 'o' in normal mode) I get a lovely automatic indent, that also disappears if I exit insert mode without adding anything to it.

Is there a way to bind something to before I exit insert mode, such as inserting a phantom character then removing it?

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Jookia
  • 6,544
  • 13
  • 50
  • 60
  • 1
    See also "[stopping vim from removing indentation on empty lines](http://stackoverflow.com/questions/7413036/stopping-vim-from-removing-indentation-on-empty-lines)". – ib. Sep 28 '11 at 01:37

3 Answers3

2

Argh, I just read about this exact thing like two days ago but I can't remember where.

Anyway, the trick is to input a character right after <CR> and delete it immediately. There are a bunch of ways to do it:

<CR>a<Esc>x
<CR>a<C-w>
<CR>a<BS>

--EDIT--

Vim being Vim there are probably many other ways.

To automate these, you need to add a mapping to your .vimrc:

inoremap <CR> <CR>a<BS> " insert mode mapping for <CR>
nnoremap o oa<BS>       " normal mode mapping for o

But I'm not sure you should overwrite defaults like that.

--EDIT--

However, what is annoying with Vim's default behaviour is that you may need to do some <Tab><Tab><Tab><Tab> before actually inputing some text on non-indented line or do == when you are done or rely on the automatic indentation rules for your language at the next <CR>.

All that can be skipped by using <S-S> which puts you in INSERT mode right at the correct indentation level.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Oddly when pressing Esc Vim still removes the whitespace, this also doesn't work when using 'p' or 'o' to enter insert mode. – Jookia Sep 28 '11 at 06:36
  • `p` stands for "put", you don't go in INSERT mode after a `p`. Did you try all three of my propositions? Did you try ``? Also why do you `` after `o`? If you want a blank line to separate code blocks the presence/absence of white space at the beginning of the line doesn't really matter, does it? Are you sure `oa` doesn't work in that usecase? What are your settings if any for `tabstop`, `softtabstop`, `expandtab`, `shiftwidth`, `shiftround` and `smarttab`? – romainl Sep 28 '11 at 07:29
  • I'm not exactly sure how to use your propositions. Adding a character then removing it works, but I'm not sure how I'd automate it. – Jookia Sep 28 '11 at 08:56
2

Try either cc or S in normal mode to change a line with respect to indention. No need for phantom characters.

:h cc
:h S
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
  • 2
    I back this up. Do not try to overcome the normal Vim behavior which is not accidental. Usually, lines containing only whitespace (which are a special case of lines with trailing whitespace) are undesired in code (for example, Linux coding style [notices to avoid this](https://github.com/torvalds/linux/blob/master/Documentation/CodingStyle#L222)). – ib. Sep 28 '11 at 01:35
  • It seems to me to just lack consistency rather than be good practice. Is there some paper showing the benefits? – Jookia Sep 28 '11 at 06:40
  • Actually I agree with ib and Peter. Overwriting default behaviours is a bad idea in general if only because you may use someone else's or a stock/older vim on some server one day. Everything in Vim is carefully thought out and has been so since a long time. That's what I alluded to in my answer and comments. – romainl Sep 28 '11 at 16:46
1

A mapping like the following should do the trick:

imap <esc> <esc>:s/\s\+$//<CR>

This one deletes trailing characters when you press esc in insert mode.

lucapette
  • 20,564
  • 6
  • 65
  • 59