148

The primary reason I like vim over emacs is that my hand movement distance away from the home row is very low (even as a programmer). However, the one main time that I do this is leaving insert mode, by pressing Escape.

I recently learned that I can use Ctrl+C to leave insert mode as well. However, this isn't really an improvement.

How can I exit insert mode without having my hands leave the home row, and without massively impacting what I have the ability to type? Note: I use a mostly vanilla vim with a dvorak keyboard layout, though I'd love to hear solutions for a qwerty layout as well.

4
  • 1
    You can map any key combination anywhere, and you can't do quite anything else. Is that what you mean?
    – yo'
    Commented Feb 4, 2015 at 13:56
  • 22
    Learned the Ctrl+c shortcut THX! Commented Oct 22, 2017 at 17:58
  • 2
    How about mapping Caps Lock as Esc? Commented Aug 28, 2018 at 20:18
  • 12
    @PhillippeGachaud: Don't use Ctrl-C as a replacement for Esc. Ctrl-C is not the same as Escape, it is a forced Escape and may behave differently under some circumstances.
    – Lie Ryan
    Commented Sep 2, 2019 at 0:47

17 Answers 17

101

A common binding you'll see is jj, because it works well for QWERTY layouts if you use home row positioning.

inoremap jj <ESC>

In that case, to type a literal jj - you should wait for 1 sec (by default) between typing the second character. (see :help 'timeout' for details)

There is also c-o which will take you out of insert, letting you do one normal command, and then put you right back into insert.

http://vim.wikia.com/wiki/Avoid_the_escape_key

12
  • 1
    I use this, it's not often that I need to type two j's together, when I do I just need to give it a second or so between clicks. Commented Feb 4, 2015 at 13:58
  • 1
    In dvorak, j is the c key. So this wouldn't be too bad (to type cc). I could also bind hh (qwerty j key = dvorak h), which would only boot me if i was typing shhhhhhh or something.
    – durron597
    Commented Feb 4, 2015 at 13:59
  • 8
    cc is a much more common two letter key combination, in English at least. So that may not be the best choice Commented Feb 4, 2015 at 14:44
  • 6
    How about mimicking jk? Find two keys which are adjacent in Dvorak but not common combinations in your preferred language. tn, for example, is the positional equivalent of jk.
    – David Lord
    Commented Feb 5, 2015 at 1:19
  • 6
    I think we can do even better - use ii instead. Yes you do have to move your fingers off the home row but it's still comfortable. And even more crucially, you can switch back and forth between modes without moving your finger. Commented Nov 28, 2018 at 0:21
65

Beside the built-in alternatives <C-[> and <C-c> to <Esc> key cited by others, another popular solution is to remap <CapsLock> as an additional Escape. This both on a Qwerty and Dvorak keyboard.

This way you can press Esc very easily with the left little finger, without removing hands from the HomeRow (incidentally, Escape was just above CapsLock in the ADM-3A Terminal used by Bill Joy while writing vi).

Remapping can be done:

  • on Windows using AutoHotKey utility;
  • on Mac using the KeyRemap4MacBook utility and others;
  • on Linux, varies according to the Desktop Environment. For example on Linux Mint/Cinnamon is just a matter of selecting the proper option in Menu->Keyboard->Keyboard Layouts->Caps Lock behavior. Alternatively, but less easily, it can be remapped at Xorg level using the xmodmap program and having it run the following .Xmodmap file:

    clear Lock
    keycode 0x42 = Escape
    

    The system can be setup to automatically run it at every login by placing strategically the following shell script lines (supposing .Xmodmap is on your home dir ~) in the chain of login auto-sourced files:

    if [ -s ~/.Xmodmap ]; then
      xmodmap ~/.Xmodmap
    fi
    

    It is tricky however, because actually sourced files vary according to the (desktop) environment used. As example, I have xmodmap called as an entry in the Cinnamon "Startup Applications" (which allows me to do more than the standard GUI key remapping customization).

Some people go even further and program the CapsLock key to behave:

  • as a Ctrl key by pressing and holding it,
  • as an Esc key by pressing and quickly releasing it.

This can be done on Windows and Mac by properly setting/programming the corresponding remap utility (specifically for AutoHotkey hints can be found here and for KeyRemap4MacBook check here). On Linux using the program xcape.

8
  • 3
    Fully agree. Having the ctrl key on the home row improves a lot of usability (scrolling through completions with ctrl+n, scrolling through the file with ctrl+f/ctrl+b, escaping with ctrl+] etc. All of these key combos are easier to perform this way.
    – John O'M.
    Commented Feb 4, 2015 at 20:09
  • 2
    About pressing the Ctrl key (at the regular position) without twisting the little finger: you can press it with the external palm of the hand, as it is described well here (I found this nice clever trick on the 'Net, time to give it back :) and kudos to who invented it) Commented Feb 5, 2015 at 21:42
  • FWIW KeyRemap4MacBook has been renamed Karabiner: pqrs.org/osx/karabiner/index.html.en
    – alxndr
    Commented Feb 19, 2015 at 0:00
  • 5
    If you remap Ctrl to Alt (Cmd) and vice versa, then <C-[> really shines. Pinky is weak, thumb is strong. On QWERTY you don't even need to leave home row for that, on Dvorak it is not so good, but acceptable. Commented Dec 16, 2016 at 19:08
  • 1
    The edit in the queue is related to the original keyboard layout of the ADM3A
    – D. Ben Knoble
    Commented Nov 18, 2019 at 20:03
42

The mappings that I use are:

inoremap jk <esc>
inoremap kj <esc>

This way, you can simply hit j and k at the same time, without having to worry about which one you press first.

3
  • 107
    But I'm writing a book about playing Edsgar Dijkstra playing blackjack in Reykjavik! ;)
    – durron597
    Commented Feb 4, 2015 at 15:05
  • 2
    Both are horrible if you want to write any Dutch. Commented May 21, 2019 at 10:20
  • 2
    +1, this is a superior solution to the accepted answer. Pressing two adjacent keys with different fingers is much more comfortable than pressing the same key twice in a row. The principle still works even if jk is inappropriate for your use case - just choose a different two letters (e.g. fd)
    – JBentley
    Commented Apr 16, 2021 at 10:16
23

<C-[> and <C-c> are two native alternatives to <Esc>.

See :help i_<esc> and :help i_ctrl-c which explains the difference between <C-c> and <Esc>.

7
  • Again, these are not improvements, as Ctrl is just as far from the home row as Escape is.
    – durron597
    Commented Feb 4, 2015 at 14:25
  • 4
    @durron597 On a QWERTY keyboard, Ctrl may be just as far from home row, but it does have an important difference: it's below the home row. You only have to move your finger to press it, whereas with Esc you have to rotate your whole hand somewhat. Also, the <C-[> has a nice symmetry to it (both little fingers moving in opposite directions).
    – wchargin
    Commented Feb 4, 2015 at 15:10
  • 9
    Note that <C-c> is not exactly the same as <Esc>.
    – wchargin
    Commented Feb 4, 2015 at 15:11
  • 2
    Agreed. Habitual use of <C-c> can subtly break plugins and autocmds.
    – David Lord
    Commented Feb 5, 2015 at 1:22
  • 4
    On most European keyboard layouts, typing <C-[> will give you Emacs-fingers.
    – Lstor
    Commented Apr 28, 2017 at 7:53
9

I would suggest using my plugin vim-easyescape.

Plug "zhou13/vim-easyescape"
let g:easyescape_chars = { "j": 1, "k": 1 }
let g:easyescape_timeout = 100
cnoremap jk <ESC>
cnoremap kj <ESC>

The problem with a simple map sequence inoremap jj <ESC> is that Vim will pause whenever you type j or k in insert mode (it is waiting for the next key to determine whether to apply the mapping). The pause causes visual distraction which you may or may not notice. vim-easyescape does not have such problem and supports custom timeout.

7

If you have an US English keyboard, pressing Ctrl-[ is equivalent for pressing Esc. This provides an easy way to exit from insert mode. Alternatively use Ctrl-c.

If you've vim in easy mode (-y), then you've to press Ctrl-l (Control-L) to exit insert mode.

There is also Ctrl+o which will only temporary exit insert mode just to execute one command and return back to insert mode (see: :help i_CTRL-O)

Or Ctrl+r, but it's only for inserting content of register or expression, e.g. Ctrl+r, =5*5 (see: :help i_CTRL-R, :help c_CTRL-R and stackoverflow post).

Read more at: Avoid the escape key at vim wikia

1
  • 4
    FYI: Ctrl+r is for inserting registries (including the expression = reg). If you want to temporarily execute a one off command use Ctrl+o.
    – Sukima
    Commented Feb 19, 2015 at 4:08
7

You can map Caps Lock to Control without installing any extra software:

  • In Mac OS X visit System Preferences > Keyboard -> Modifier Keys
  • In Linux execute: setxkbmap -option caps:ctrl_modifier
  • In Windows edit your registry.

This brings Control onto the middle row, making it easier to press all Control modified strokes, including Ctrl-[ and Ctrl-C which both exit Insert mode in Vim.

Make Caps Lock perform both Ctrl and Escape?

Alternatively, at the bottom of his answer Giovanni suggests to map Caps Lock to perform both Control and Escape depending on the length of press, but this requires installing extra software.

Some people love this.

I did not. Because sometimes I press Ctrl, then change my mind, and release it. If I do that too quickly, within the configured timeout, then it sends an Escape stroke, which in some situations can be quite disruptive (for example closing the current dialog window).

Differences between Ctrl-[ and Ctrl-C

Ctrl-[ is probably what you want, since it always works like Escape.

One difference with Ctrl-C is: When leaving an edit in blockwise visual mode, Ctrl-C will keep the changes you made on the current line, but will not repeat the changes over the other lines in the block, which is the point of that mode.

It also won't check for abbreviations, or trigger the InsertLeave event. See :help i_CTRL-C

4

In terminal vi and Vim, alt + single-normal-mode-key will work for quick edits in most terminals.

This works because most terminals send the alt modifier as the escape character. For example, when you type alt+k the terminal emulator sends two character to the running program: esc, k. Vi and Vim interpret this as you would expect; it leaves insert mode (because of the escape) and executes the normal mode command (in the example, moving the cursor up one line).

This can be a convenient way to exit insert mode if your keyboard has the alt key next to the space bar (so that it can be pressed by thumb, leaving your fingers on the home row), so long as you are using a terminal emulator with this behaviour and don't mind running a normal mode command.

6
  • 2
    Can you elaborate on what is single-normal-mode-key? You mean Escape?
    – kenorb
    Commented Feb 19, 2015 at 9:10
  • 2
    I don't quite understand this answer either; Alt+x actually seems to do something different than Alt in normal mode? And some other keys don't seem to work? But maybe I'm using it wrong..? Maybe you could expand on your answer a little bit to explain how it works, and link to some Vim docs would also be good :-) Thanks! :-) Commented Feb 19, 2015 at 9:52
  • 1
    that actually depends on your terminal configuration and is not generally set as you seem to suggest. Commented Jul 9, 2016 at 18:11
  • 1
    I upvoted this, then checked the history and realised that for some reason a moderator had edited it to add 99% of the text from thin air, rather than just leaving a comment like the rest of us are expected to do. So I had to reverse my vote. Surely what happened here goes beyond the scope of sensible edits? I've had far less disruptive edits rejected. The new content is great, sure, but doesn't belong in this answer. It should've been its own. Commented Oct 29, 2016 at 21:35
  • 1
    @underscore_d, why on earth did the authorship of the extra content mean you "had to" reverse your vote? The purpose of the votes is to make the best answers visible, and only secondarily to award reputation to the answerers. If it's a good answer, upvote it; don't worry about the edit history.
    – Wildcard
    Commented Aug 14, 2018 at 23:16
3

I've been using Ctrl+3 since quite some time, I find it easier to type than most alternatives and seems to work nicely (at least on Linux). My Caps Lock is mapped to Ctrl, so it's very comfy to type it.

I tried using Ctrl+c for some time, but I had some issues with it which I don't recall exactly now, but I wasn't getting the exact same behavior as Esc, like I get now with Ctrl+3.

3
  • 3
    What exactly is Ctrl-3 supposed to be doing? Commented Jul 30, 2016 at 6:46
  • I don't know how it works, but it works like Esc in vanilla Vim. I found it empirically by testing out combinations, don't know details about how the internals work.
    – elias
    Commented Jul 30, 2016 at 16:18
  • 1
    This is not a vi thing, works also on the shell. Very useful in case you have vi mode enabled on your shell with bindkey -v
    – golfadas
    Commented Sep 26, 2016 at 11:20
3

FWIW I use:

:map! ;l ^[
:vmap ;l ^[

It feels similar to hitting Return. I use ; as the leader for other Insert-mode mappings too.

The only time I've needed to type ';' + letter is when code golfing; I've not had any conflicts otherwise.

1
  • 1
    maybe better to map it to escape than mapping it to ^[ right? :) Commented Feb 28, 2017 at 22:20
1

I have settled for <A-e> for the escape:

noremap  <A-e> <Esc>
snoremap <A-e> <Esc>
inoremap <A-e> <Esc>
tnoremap <A-e> <Esc>

" For some reason in command line mode '\e' is interpreted as Enter.
cnoremap <A-e> <C-c>

Edit: Just found about <C-\><C\n> which sends you to normal mode from any other mode. It also has better overall behavior. The mappings become:

noremap <A-e> <C-\><C-n>
noremap! <A-e> <C-\><C-n>
1

Here's a no-config, Mac-only alternative that I only just learned about. In macOS's Terminal.app and in MacVim's GUI version, (but not, notably, in the iTerm2 terminal*,) the standard macOS key combination . (Command-period) can be used to emulate an Esc keystroke.

* At least, not using any configuration options I could find in the preferences.

1
  • 1
    Awesome... I didnt even know this!
    – D. Ben Knoble
    Commented Apr 21, 2020 at 14:03
1

To overcome the lagging caused by naive mapping inoremap jk <ESC>. I have written a plugin called better-escape.vim. Here is how to use it:

let g:better_escape_shortcut = 'jk'
let g:better_escape_interval = 200

By default, the time interval threshold for the pressing of j and k is set to 150 ms. That is, if the time interval between pressing k and pressing j is above the threshold, we assume that you want to insert jk literally. Otherwise, we assume you want to leave insert mode. This time interval can be customized via option g:better_escape_interval. In the above example, we have the time interval to 200ms.

With better-escape.vim, you can even set multiple shortcuts to escape insert mode:

let g:better_escape_shortcut = ['jk', 'jj', 'kj', 'лл']

Any of those shortcuts will help you escape insert mode easily.

1

As well as the other answers, remember that you don't always need to exit insert mode to perform commands. Two examples:

  • CTRL-O (:help i_CTRL-O) will accept one normal mode command and then return to insert mode. For example, if on an empty line you enter insert mode and type the brown you can then type ^Obquick ^OA to change the text to the quick brown and remain in insert mode with your cursor after brown.

    This can be particularly handy with normal-mode mappings you create yourself. I map <TAB>d to insert the current date and normally use this via ^O^Id while I'm inserting text.¹

  • CTRL-R (:help i_CTRL-R) lets you insert buffers and other information into the text as you're typing in insert mode. For example, ^R" will insert the your most recent deletion, and ^R% will insert the current filename.


¹ I use :noremap <TAB><TAB> <TAB> to maintain access to the "move (count) forward in jump list" command.

2
  • I'm almost certain you meant C-r %, not C-r #. And your example C-o quick C-o A doesn't make much sense unless you're missing something like b after the first C-o. Note that mapping <tab>d makes the default C-i command harder to trigger
    – D. Ben Knoble
    Commented Oct 28, 2022 at 13:54
  • 1
    @D.BenKnoble Thanks for the corrections! Yes, I meant % and a b was missing; I've corrected those in the post. I'll also add a note about how I deal with command-mode <TAB>.
    – cjs
    Commented Oct 29, 2022 at 2:52
0

As already mentioned, remapping of CAPSLOCK to ESC and CTRL requires additional SW. For me, this is not an issue and I'm enjoying this mapping a lot! This is how I did it:


Remap CAPSLOCK to ESC and CTRL on Windows

If you want to remap CAPSLOCK to both

  1. to ESC (when pressed alone)
  2. to CTRL (when pressed with other keys) you can use this little piece of open source software by ililim.

You do not need Admin privileges for this and ESC and CTRL are still working as expected.

I use it to enjoy convenient CTRL + anything presses without hurting my pinky and to toggle modes in Vim that I use via ssh in MobaXterm. For installation just follow this description.


Remap CAPSLOCK to ESC and CTRL on Linux

To do the same on Linux, you can use XCAPE. To install it on Ubuntu use:

sudo apt install xcape

For the actual mapping do:

setxkbmap -option ctrl:nocaps
xcape -e 'Control_L=Escape'

The 1st command is to map CAPSLOCK to ESC whereas the 2nd one takes care of CTRL pressed with other keys.

To have this permanently working in X sessions, you can add this to ~/.xprofile.

-1
nnoremap <C-I> <C-I>
vnoremap <Tab> <Esc>gV
onoremap <Tab> <Esc>
inoremap <Tab> <Esc>`^

I remap tab to exit. I think historically that's where the esc key used to be, so I used it and never looked back. I hardly ever need to put in a tab in a document, and when I do I just press shift + tab. I have never once missed the original tab binding.

0
-2

vim v8.1 press any combine below in insert mode, will exit insert mode.

Alt+e
Alt+w
Alt+j
Alt+k
Alt+h
Alt+l
Alt+.
2
  • 3
    Welcome to Vi and Vim bo0k! Your post was automatically flagged as being low-quality because it's short and mostly code—it would be better if you could give some explanation with the answer. You can edit to do so. (Off the top of my head, I happen to know this works in xterm-like terminals because Alt + key sends escape sequences that actually include triggering <esc>.)
    – D. Ben Knoble
    Commented May 4, 2020 at 20:49
  • 4
    Alt+e will send <Esc>e, so it actually exits insert mode and issues the e command (to go to the end of the word). A useful shortcut in some cases perhaps, but it does more than just "exit insert mode" :-) You see which characters are sent to Vim by using <C-v><Alt-e> in insert mode; that will insert the literal characters. Commented May 5, 2020 at 1:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.