95

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.

  • 1
    You can map any key combination anywhere, and you can't do quite anything else. Is that what you mean? – yo' Feb 4 '15 at 13:56
  • 7
    Learned the Ctrl+c shortcut THX! – Philippe Gachoud Oct 22 '17 at 17:58
  • 1
    How about mapping Caps Lock as Esc? – Kolob Canyon Aug 28 '18 at 20:18
  • 3
    @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 Sep 2 '19 at 0:47

15 Answers 15

69

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

| improve this answer | |
  • 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 Feb 4 '15 at 13:59
  • 7
    cc is a much more common two letter key combination, in English at least. So that may not be the best choice – Nick J Adams Feb 4 '15 at 14:44
  • 5
    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 Feb 5 '15 at 1:19
  • 1
    I kinda like have jk/kj exit normal mode even in Dvorak; they're the "c" and "v" keys on a QWERTY keyboard, so they're easy for my left pointer and middle fingers. – alxndr Feb 18 '15 at 23:33
  • 3
    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. – Sridhar Sarnobat Nov 28 '18 at 0:21
46

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.

| improve this answer | |
  • 2
    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. Feb 4 '15 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) – Giovanni Maggiorani Feb 5 '15 at 21:42
  • FWIW KeyRemap4MacBook has been renamed Karabiner: pqrs.org/osx/karabiner/index.html.en – alxndr Feb 19 '15 at 0:00
  • 3
    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. – Nash Bridges Dec 16 '16 at 19:08
  • 1
    The edit in the queue is related to the original keyboard layout of the ADM3A – D. Ben Knoble Nov 18 '19 at 20:03
31

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.

| improve this answer | |
  • 83
    But I'm writing a book about playing Edsgar Dijkstra playing blackjack in Reykjavik! ;) – durron597 Feb 4 '15 at 15:05
  • 1
    Both are horrible if you want to write any Dutch. – Hielke Walinga May 21 '19 at 10:20
17

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

| improve this answer | |
  • Again, these are not improvements, as Ctrl is just as far from the home row as Escape is. – durron597 Feb 4 '15 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 Feb 4 '15 at 15:10
  • 6
    Note that <C-c> is not exactly the same as <Esc>. – wchargin Feb 4 '15 at 15:11
  • 2
    Agreed. Habitual use of <C-c> can subtly break plugins and autocmds. – David Lord Feb 5 '15 at 1:22
  • 2
    On most European keyboard layouts, typing <C-[> will give you Emacs-fingers. – Lstor Apr 28 '17 at 7:53
5

You can map Caps Lock to Control without using any external program:

  • 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 home row, making it easier to press all Control modified strokes, including Ctrl-[ and Ctrl-C which both exit Insert mode in Vim.

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.

Ctrl-[ is the preferred alternative, since it always works like Escape.

One noticeable difference with Ctrl-C is that 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

| improve this answer | |
5

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.

| improve this answer | |
3

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.

| improve this answer | |
  • 2
    Can you elaborate on what is single-normal-mode-key? You mean Escape? – kenorb Feb 19 '15 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! :-) – Martin Tournoij Feb 19 '15 at 9:52
  • 1
    that actually depends on your terminal configuration and is not generally set as you seem to suggest. – Christian Brabandt Jul 9 '16 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. – underscore_d Oct 29 '16 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 Aug 14 '18 at 23:16
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.

| improve this answer | |
  • 1
    maybe better to map it to escape than mapping it to ^[ right? :) – Alexander Mills Feb 28 '17 at 22:20
2

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.

| improve this answer | |
  • 2
    What exactly is Ctrl-3 supposed to be doing? – Christian Brabandt Jul 30 '16 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 Jul 30 '16 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 Sep 26 '16 at 11:20
1

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

| improve this answer | |
  • 3
    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 Feb 19 '15 at 4:08
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>
| improve this answer | |
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.

| improve this answer | |
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.

| improve this answer | |
-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.

| improve this answer | |
-3

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+.
| improve this answer | |
  • 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 May 4 at 20:49
  • 2
    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. – Martin Tournoij May 5 at 1:18

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