Vim Tips Wiki
Advertisement
Tip 285 Printable Monobook Previous Next

created 2002 · complexity basic · author David A. Rogers · version 6.0


This tip discusses how to exit from insert mode without needing to press Esc. Many touch typists appreciate the fact that they can leave their hands on the keyboard home row while using Vim, so there are several ideas for avoiding the Esc key (pressing Esc generally requires stretching to the top of the keyboard).

No utility programs or operating system tweaks are required for this tip. For other suggestions involving mapping keys with the operating system or utilities, see Map caps lock to escape in XWindows (for Unix-based systems), or Map caps lock to escape in Windows (for Windows).

Avoiding the Esc key

Pressing Ctrl-[ (control plus left square bracket) is equivalent to pressing Esc. On a US keyboard, pressing Ctrl-[ is an easy way to exit from insert mode.

Alternatively, simply press Ctrl-c to quit insert mode (however, Ctrl-c does not expand abbreviations). :help i_CTRL-C

Mappings

It can be convenient to use a mapping so that pressing a key, or sequence of keys, generates Escape. The :imap command is used to create the mapping so that it only applies while in insert mode.

For example, the following allows you to press jj to exit from insert mode:

:imap jj <Esc>

To generate Escape, jj has to be typed quickly. :help 'timeout'

A problem with mapping a sequence like jj is that Vim will pause whenever you type j in insert mode (it is waiting for the next key to determine whether to apply the mapping). The pause is merely a visual distraction which you may not notice, and it does not slow down typing.

For simplicity, we will show :imap but careful Vimmers are in the habit of using :inoremap which does not attempt to interpret the result of the mapping (with the :imap command, the result is scanned to see whether it contains another mapping).

Alternative mappings

Here are some alternative suggestions:

" Press i to enter insert mode, and ii to exit.
:imap ii <Esc>

" Use backtick.
:imap ` <Esc>

" Two semicolons are easy to type.
:imap ;; <Esc>

" Press Shift-Space (may not work on your system).
:imap <S-Space> <Esc>
" Try the following so Shift-Space also enters insert mode.
:nmap <S-Space> i
" Or just Space to enter insert mode.
:nmap <Space> i

" In Mac OS X, mapping <S-space> does not work, but the following
" is better (press the "apple" key and the space key).
:imap <D-space> <Esc>

" Press Enter to exit from insert mode!
" On many systems, you can press Shift-Enter to end a line.
:imap <CR> <Esc>

" For gvim (GUI, not console/terminal), you might prefer
" pressing Shift-Enter to exit insert.
:imap <S-CR> <Esc>

: On gvim and Linux console Vim, you can use Alt-Space.
:imap <M-Space> <Esc>

Using the Tab key

With the following in your vimrc, you can press Tab to return to normal mode:

nnoremap <Tab> <Esc>
vnoremap <Tab> <Esc>gV
onoremap <Tab> <Esc>
inoremap <Tab> <Esc>`^
inoremap <Leader><Tab> <Tab>

In normal mode, you can press prefix keys before a command (for example, 12 for a count). The nnoremap causes Tab to cancel any prefix keys.

The vnoremap causes Tab to cancel any selection (gV is required to prevent automatic reselection).

The onoremap causes Tab to cancel any operator-pending command (for example, y).

The first inoremap causes Tab to exit insert mode, and the `^ restores the cursor position so exiting insert will not move the cursor left.

The second inoremap, assuming the default leader key, allows you to press \ then Tab to insert a tab character. You may prefer an alternative mapping to make it easier to insert a tab. For example, on some systems, you be able to use Alt-Tab to insert a tab character with:

inoremap <M-i> <Tab>

Due to the way that Vim read keys, Ctrl-i is the same as Tab. Therefore, you cannot map Tab to generate Escape, and map Ctrl-i to do something different.

When adjusting indents, you can avoid tabs altogether:

  • In normal mode: type << to shift the line left one 'shiftwidth', or >> to shift right (and press . to repeat).
  • In insert mode, press Ctrl-d to shift left, or Ctrl-t to shift right.

Improving the Esc key

You may not find attempts to replace the Esc key satisfactory. However, if you hate how exiting from insert mode moves the cursor left, you may like this adaption from the Tab key section above:

inoremap <Esc> <Esc>`^

Mapping problems

Vim runs on many different hardware and software platforms. Therefore some key sequences may not be available. For example, you may be able to map Shift-Space in a GUI Vim, but not in a terminal Vim (and even if you could, if you were running via PuTTY, for example, Vim might not receive the key code).

To test your system, enter insert mode then press Ctrl-K followed by the key of interest (for example, press Ctrl-K then Shift-Space). :help i_CTRL-K

Related tips

 TO DO 
Think about merging the following somewhere.

Comments

Advertisement