How do you consistently leave insert mode in Neovim?

software tool
technical writing
You know that joke about exiting Vim? Still true!

Gregory M. Kapfhammer



I write my research papers and blog posts 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. Okay, now you should be able to exit insert mode!

if has("nvim")
  inoremap <ESC> <C-\><C-n>
  tnoremap jk <C-\><C-n>
Get the Gist!
Share Expertise

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. I should also note that, since this post was written, I’ve converted my Neovim configuration to use Lua and the lazy.nvim plugin manager. This means that you should study my init.lua file instead!

Return to Blog Post Listing