d11wtq
d11wtq

Reputation: 35298

Vim InsertLeave event doesn't occur when using CTRL-C instead of ESC

I'm reading through Steve Losh's Learn Vimscript the Hard Way and have added to my .vimrc

" make status line red while in insert mode
augroup hi_statusline
  autocmd!
  autocmd InsertEnter * hi StatusLine ctermbg=15  ctermfg=9
  autocmd InsertLeave * hi StatusLine ctermbg=238 ctermfg=253
augroup END

I've noticed an odd behaviour, however. Sometimes I hit C-c to get out of insert mode, which works fine and is documented. When I do this, the InsertLeave event doesn't fire. When I use ESC it works fine. Is this a known oddity of vim? Is there perhaps a workaround, by hooking into another event that suggests InsertMode is no longer active?

EDIT | Meh, adding a inoremap <C-c> <ESC> resolves it and as far as I can tell has absolutely no side-effects, since C-c already does what ESC does (goes back to normal mode). Correct me if I'm wrong.

Upvotes: 11

Views: 2031

Answers (2)

benjifisher
benjifisher

Reputation: 5112

This is documented under :help i_CTRL-C:

CTRL-C  Quit insert mode, go back to Normal mode.  Do not check for
        abbreviations.  Does not trigger the |InsertLeave| autocommand
        event.

As usual, Bram lives up to the design goals.

:help design-documented

Upvotes: 3

jthill
jthill

Reputation: 60255

:ino <C-C> <Esc>

^C is conventionally the get-me-out-of-here-now-please key, if you've got an autocmd you need to not run C-C becomes your friend.

Upvotes: 16

Related Questions