How do you consistently leave insert mode in Neovim?

30 June 2017

flickr photo by Ikhlasul Amal shared under a Creative Commons (BY-NC) license

When I write my own research papers I often create them in Neovim. However, before using Neovim, I have spent years writing papers and code in Vim. Like many others who use Vim for programming and writing, I have added the following two commands to my .vimrc file, thus enabling me to press jK when I want to leave insert mode. This works great in Vim — but I found that I needed a few other settings to ensure that I could consistently leave insert mode in Neovim!

inoremap jk <ESC>
inoremap <ESC> <NOP>

If you decide to use both Vim and Neovim, then you need to have a configuration file that contains code blocks for one text editor or the other, achieved by using conditional logic in the following code segment. The first line of code see to it that <ESC> is mapped the same way in Neovim as it is in Vim. The second line of code is useful since Neovim now includes a built-in terminal window. Since you want to be able to exit insert mode in Neovim's terminal window in the same way that you exit insert mode in a regular buffer, this second line is needed.

" Leave using a different command than ESC
if has("nvim")
  inoremap <ESC> <C-\><C-n>
  tnoremap jk <C-\><C-n>

If you are a longtime Vim user, what are some ways in which you tweaked your configuration so that it worked for Neovim? I hope that you will soon contact me with your own tips for using Neovim.

Enjoy this post? If so, please read, SEED Interview with Timothy Tsai, my most recent article.


Like my work? Support it!