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 + [.

Why use the safe 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 the left hand into an infinite loop. Read more here.

Sources:

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s