Hacker News new | ask | show | jobs
by wodenokoto 3551 days ago
I don't use VIM a lot, mostly when committing stuff in git, so I'm quite naive about it.

How would you get out of insert mode by writing jk? Or, how would you write jk in insert mode without exiting?

1 comments

Add the following to your .vimrc:

  if !&insertmode
    inoremap jk <esc>
    snoremap jk <esc>
  endif
(the insertmode bit can probably be dropped, but it's technically correct)

With this mapping, typing "jk" in insert mode acts as if you'd hit escape instead (so it doesn't type the j or the k). To type jk in insert mode without exiting, you either wait a second after hitting the j (once it shows up on screen you're good) before hitting the k, or if you're impatient you hit another key and then hit delete before the k, e.g. "jj<delete>k". The reason this mapping is popular is because "jk" is extremely rare to type in insert mode; you usually only type it if you're typing out the alphabet, or if you're talking about how you map jk to escape.

The only annoyance here is if you type j by itself, you don't actually see the letter show up immediately. This is because Vim is waiting to see if you're going to type a k. If you type nothing, after a second Vim will timeout and decide you're not invoking the jk mapping after all (and will then show the j). Or if you hit anything besides k then you're obviously not triggering the mapping. You can observe this behavior with any other multi-character mapping you might have as well (e.g. anything with <leader> or <localleader>).