vim jj to esc mapping

For a single vim session (safe version)

:inoremap jj <Esc>

For every vim session (still safe)

Save this to your .vimrc file in your home directory.

" this is a comment so you remember this later
" insert mode; no recursive; map; <from>; <to>
inoremap jj <Esc> 

* change jj to anything you want, fd , jk even ;;

What is happening?

i means we are creating a mapping for the insert mode

nore means we designate “no recursive” mode (see below for more)

map means we are mapping 🗺

jj is the commands we are mapping from

<Esc> is the command we are mapping to

No change needed: Ctrl + [

If you already have Caps Lock mapped to Ctrl, then a solid alternative is: Ctrl + [.

What is a “safe” version? (the no infinite loops version)

When you create mappings in vim, there is a recursive lookup to all other mappings.

We use inoremap instead of imap above because the nore (not recursive) portion, will avoid recursive mapping. An example of recursive mapping is this :nmap dd jddk where the dd in the right hand definition will actually map to “dd” in the left side into an infinite loop. Read more in the “Learn Vimscript the Hard Way” section of Mapping:recursion (link)

Sources:

  1. Stack Overflow
  2. Learn Vim The Hard Way
  3. vim.fandom
  4. In vim > :help inoremap