How to Exit Insert Mode in Vim

Exit Insert Mode in Vim

by | Mar 2, 2024 | Useful Tech

Wish to get out of inserting text in Vim, but what you type keeps showing up on screen as letters and numbers? Find out here how to exit “insert mode” in Vim and Neovim (even temporarily) and how to set up custom keys to get out of “insert mode”.

First, Smell Your Beans

When you embark on a journey of tastes to explore the subtle nuances that set apart German Kellerbier from Austrian Zwickl, say, or Nilgiri tea from teas grown in Assam’s hills, you will do well to “reset” your senses after each sip and snell. A whiff and sniff of coffee beans works well to do this, I am told (or, of course, casually smelling yourself).

So, coffee is one way to get taste out of your system; let’s now see how to get insert mode out of Vim—or out of insert mode once Vim is in it?

How to Exit Insert Mode in Vim and Neovim

Time needed: 1 minute

To exit insert mode in Vim and return to normal mode so you can navigate and manipulate text:

  1. Optional: Verify you are in insert mode.

    Here’s how to tell: With its default configuration, Vim displays — INSERT — in the bottom left corner of its window, i.e. in the command line where you also enter commands when you are in command-line mode.

  2. Press Esc.

    With insertmode”: If Vim is set up for insertmode, pressing Esc will do nothing and ring the audible or visual bell; see below how to exit insertmode insert mode.
    Alternative keys: If the Esc key does not work or is not available, you can resort to other keys; see below.

Exit Vim Insert Mode Temporarily

You can exit insert mode for just one command in Vim. Use this to run a command such as deleting whole words without having to re-enter insert mode.

To run an individual command from normal mode (exit insert mode temporarily):

  1. Press Ctrl O in Vim insert mode.
    Command line: — INSERT — in the command line will be replaced with — (insert) — while temporary normal mode is in effect.
    You can exit insert mode temporarily in Vim using Ctrl O
  2. Type the desired command in normal mode
    Examples:
    Type di( to delete everything within the enclosing brackets (which you could just as well do with Esc ci(, of course).
    Enter gqap to re-flow the current paragraph (such as a comment).
    Type :%y* to copy all text to the system clipboard (provided Vim was compiled with the * register).
    Press ~ to toggle the case of the just-typed character (the character just left of the insertion cursor).
  3. Continue typing (back) in insert mode.

Exit Vim Insert Mode with the “insertmode” Option Set

To run normal mode commands in Vim when the “insertmode” option is set and Vim’s default mode is insert mode (Neovim does not include the “insertmode” option):

  1. Optional: Check the “insertmode” option is set.
    Here’s how: Press Ctrl O, then type :set insertmode? and press Enter; if you see insertmode, the option is active.
  2. Press Ctrl L to exit insert mode to normal mode.
    Back to insert: Press Esc to return to insert mode.
    One command: Ctrl O also works for executing one normal mode command, of course.

You can also turn off insertmode. To disable it:

  1. Press Ctrl O.
  2. Type :set noinsertmode.
  3. Press Enter.

This will exit insert mode and disable “insertmode”.

How to Exit Insert Mode in Vim and Neovim: FAQ

Is there an alternative to pressing “Esc”?

Yes.

As an alternative to Esc, you can also press Ctrl C to exit insert mode.

Can I set up another key to exit insert mode?

Yes.

Most typically, you will remap another key to act as the Esc key. To do so with the Caps Lock key, for example, add the following to your .vimrc file:

inoremap <CapsLock> <Esc>

Here, you can replace <CapsLock> with a different key, of course. For instance, you can set up jj or ii to exit insert mode:

inoremap ii <Esc>

This lets you exit insert mode by pressing I twice in a row quickly.

(How to exit insert mode tested with Vim 9.0 and Neovim 0.9.5; updated March 2024)

Home » Useful Tech » How to Exit Insert Mode in Vim