Vim Pre-Exit (Esc Key) Team?

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

Is there a way to bind something before I exit insert mode, for example by inserting a phantom character and then deleting it?

+5
source share
3 answers

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

In any case, the trick is to enter the character immediately after <CR>and immediately delete it. There are several ways to do this:

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

--EDIT--

Vim, Vim, , .

, .vimrc:

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

, .

--EDIT--

, Vim, , <Tab><Tab><Tab><Tab>, - ==, , <CR>.

, , <S-S>, INSERT .

+2

cc, S , . phantom.

:h cc
:h S
+2

A display like the one below should do the trick:

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

This removes trailing characters when you press escin insert mode.

+1
source

All Articles