Vim Tips Wiki
No edit summary
Line 22: Line 22:
 
==Mappings==
 
==Mappings==
 
It can be convenient to use a mapping so that pressing a key, or sequence of keys, generates Escape. The <tt>:imap</tt> command is used to create the mapping so that it only applies while in insert mode.
 
It can be convenient to use a mapping so that pressing a key, or sequence of keys, generates Escape. The <tt>:imap</tt> command is used to create the mapping so that it only applies while in insert mode.
 
For example, the following allows you to press <tt>jj</tt> to exit from insert mode:
 
<pre>
 
:imap jj <Esc>
 
</pre>
 
 
To generate Escape, <tt>jj</tt> has to be typed quickly. {{help|'timeout'}}
 
 
A problem with mapping a sequence like <tt>jj</tt> is that Vim will pause whenever you type <tt>j</tt> 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 <tt>:imap</tt> but careful Vimmers are in the habit of using <tt>:inoremap</tt> which does not attempt to interpret the result of the mapping (with the <tt>:imap</tt> command, the result is scanned to see whether it contains another mapping).
 
For simplicity, we will show <tt>:imap</tt> but careful Vimmers are in the habit of using <tt>:inoremap</tt> which does not attempt to interpret the result of the mapping (with the <tt>:imap</tt> command, the result is scanned to see whether it contains another mapping).
Line 37: Line 28:
 
Here are some alternative suggestions:
 
Here are some alternative suggestions:
 
<pre>
 
<pre>
" Press i to enter insert mode, and ii to exit.
 
:imap ii <Esc>
 
 
 
" Use backtick.
 
" Use backtick.
 
:imap ` <Esc>
 
:imap ` <Esc>
 
" Two semicolons are easy to type.
 
:imap ;; <Esc>
 
   
 
" Press Shift-Space (may not work on your system).
 
" Press Shift-Space (may not work on your system).
Line 212: Line 197:
 
*[[VimTip1183|1183 Insert-mode only Caps Lock]]
 
*[[VimTip1183|1183 Insert-mode only Caps Lock]]
 
*[[VimTip1519|1519 Making CapsLock work in Vim]]
 
*[[VimTip1519|1519 Making CapsLock work in Vim]]
  +
   
 
==Comments==
 
==Comments==

Revision as of 08:59, 2 March 2011

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. The Vi editor was originally written on an ADM-3A terminal, which had the Escape key in place of the Tab key (compared to most modern keyboards). 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 at its current location (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). Some of the suggestions involve more than avoiding escape; topics also discussed include using the CapsLock key for Ctrl, and avoiding irritations from accidental use of CapsLock.

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 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:

" Use backtick.
: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>

: 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 does 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 may be able to use Alt-Tab to insert a tab character with:

inoremap <M-i> <Tab>

Some plugins may remap the Tab key. To remap them back from .vimrc, use autocmd:

au VimEnter * map <Tab> <Esc>
au VimEnter * imap <Tab> <Esc>
au VimEnter * vmap <Tab> <Esc>

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.

Using the Enter key

It's very easy to exit insert mode using the Enter key:

:inoremap <CR> <Esc>

An example of using the above would be to type ce to change to the end of a word, then type new text, then press Enter to return to normal mode.

When you want to enter a newline, you may find that Shift-Enter or Ctrl-Enter works (it should always work in gvim, but might not in a terminal, depending upon your system). If your Shift-Enter or Ctrl-Enter works, and you can get used to it, the above might be all you need.

An alternative would be to map Shift-Enter to generate Escape:

:inoremap <S-CR> <Esc>

Then Enter will always work as expected, and Shift-Enter will always exit insert mode. If Shift-Enter does not work on your system, you might find that Ctrl-Enter (<C-CR>) does.

Another, more elaborate, alternative is:

function! ToggleEnterMapping()
  if empty(mapcheck('<CR>', 'i'))
    inoremap <CR> <Esc>`^
    return "\<Esc>"
  else
    iunmap <CR>
    return "\<CR>"
  endif
endfunction
call ToggleEnterMapping()
inoremap <expr> <S-CR> ToggleEnterMapping()
" Optional (so <CR> cancels prefix, selection, operator).
nnoremap <CR> <Esc>
vnoremap <CR> <Esc>gV
onoremap <CR> <Esc>
Features
  • Initially, pressing Enter returns to normal mode.
  • In insert mode, pressing Shift-Enter enters "multiline" mode, meaning that pressing Enter will insert a newline.
  • In insert mode, after pressing Shift-Enter to enter "multiline" mode, pressing Shift-Enter again exits (returns to normal mode).
Explanation

The mapcheck() function returns the current insert-mode mapping for <CR> (Enter). The empty() function returns nonzero (true) if there is no such mapping.

The inoremap <expr> command maps Shift-Enter to generate the expression returned by calling ToggleEnterMapping() (the code for either Escape or Enter). Calling the function toggles the insert-mode mapping for Enter: if it is mapped, it is unmapped; otherwise it is mapped to <Esc>`^ (generate Escape then go to the ^ mark).

Toggling insert mode

Press Ctrl-Space to start and to stop insert mode (the same suggestion using Shift-Space is above):

nnoremap <C-space> i
imap <C-space> <Esc>

Or you may prefer to map Ctrl-Space to a rather than i so that repeatedly pressing Ctrl-Space does not move the cursor back (remember that pressing I allows you to insert a character at the beginning of the line):

nnoremap <C-space> a
imap <C-space> <Esc>

An alternative follows. It uses /i to toggle insertion at the cursor, and /a to toggle insertion at the end of the line. If you use /i (or /a) to insert, you must /i (or /a) to exit (not Esc).

nnoremap <silent> /i :let &insertmode=1-&insertmode<CR>
inoremap <silent> /i <Esc>:let &insertmode=1-&insertmode<CR>
nnoremap <silent> /a :let &insertmode=1-&insertmode<Bar>if &insertmode<Bar>:startinsert!<Bar>endif<CR>
inoremap <silent> /a <Esc>:let &insertmode=1-&insertmode<Bar>if &insertmode<Bar>:startinsert!<Bar>endif<CR>
References
Comment

Probably can use :set insertmode! to toggle.

The / is easy to type, but did you mean to use <Leader>? Also, why toggle 'insertmode'? Why not the following (and similar for append):

nnoremap <Leader>i i
inoremap <Leader>i <Esc>

Improving the Esc key

You may not find attempts to replace the Esc key satisfactory. However, if you don't like the cursor moving left when you exit from insert mode, you may want to try this variation from the Tab key section above:

inoremap <Esc> <Esc>`^

The above trick means that the cursor will not move if you press i to enter insert mode, then press Esc to exit. However, pressing a (append) then Esc will move the cursor, so this "improvement" may not satisfy you.

Command-Line Mode

If you want to map another key to Esc in command-line mode, if you attempt a mapping like

cmap <C-space> <Esc>

the resulting mapping will submit the command-line as if pressing Return rather. To create a mapping to act like Esc in command-line mode, you must map to <C-c> instead.

cmap <C-space> <C-c>

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

The following advises using <C-o> instead of <Esc> in insert mode mappings.

The following are not related to the Esc key. They try to avoid CapsLock applying in normal mode (so you can insert text in uppercase when needed, but not type in uppercase after exiting insert mode).


Comments

Use META + <next action>

Depending on your terminal and vim settings it is possible that ^[ is sent firstly while you are pressing META(Alt)+<key>. So during Insert mode you can press META+j to finish editing, return to Normal mode and move to the next line.