11

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.

2
  • 2
    +1 for "Learn Vimscript the Hard Way) reference, seems very useful!
    – mMontu
    May 25, 2012 at 15:58
  • <C-c> really means: abort all running operations. This includes scripts, and autocommands are scripts. Simple as that.
    – sehe
    May 25, 2012 at 21:17

2 Answers 2

16
: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.

2
  • Thanks, also figured this out just before you posted too ;) Will accept your answer once the 5 minute timeout passes and SO lets me do it ;) I had C-c mapped to ESC in my vimrc previously, but had removed it since it I realised it already supported the behaviour... obviously with this subtlety though.
    – d11wtq
    May 25, 2012 at 15:27
  • 1
    np, accept+upvote actually feels like overkill for this. Glad it's workin for ya :-)
    – jthill
    May 25, 2012 at 15:33
3

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
1
  • It's also documented under :help InsertLeave. But one might be forgiven for thinking that the event InsertLeave triggers when you leave insert mode. Documentation is good, but intuitive design is even better.
    – wardw
    Jun 11, 2023 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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