67

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
  • 2
    Learned the Ctrl+c shortcut THX! – Philippe Gachoud Oct 22 '17 at 17:58
  • How about mapping Caps Lock as Esc? – Kolob Canyon Aug 28 '18 at 20:18

11 Answers 11

55

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

  • 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
  • 4
    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
  • 1
    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
38

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, CapsLock is where Escape used to be on the keyboards when vi was invented).

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.

  • 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
  • 1
    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
  • I tried this solution, but found that I hit Caps Lock by accident when pressing Shift some times, which was annoying. So your mileage may vary depending on how agile your left pinky is. :) – Godsmith Sep 26 '16 at 7:31
  • 2
    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
27

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.

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

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

  • 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
  • On my machine, it's <C-[> works, but it takes a half second longer to register than <C-c> – Alexander Mills Feb 28 '17 at 22:19
4

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

4

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.

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.

  • 2
    Can you elaborate on what is single-normal-mode-key? You mean Escape? – kenorb Feb 19 '15 at 9:10
  • A single key press that you would use in normal mode can be used in insert mode with the alt key. e.g. Alt+x to delete a character. – ewatt Feb 19 '15 at 9:21
  • 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
  • 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
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.

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

  • 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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