25

Is it possible to remap the Esc key to another key in Vim? When I'm using Vim in a full screen VMware vSphere web client console window and I press the Esc key to exit edit mode, it also takes me out of full screen mode in the console window.

I am using the Vim that comes from the RHEL YUM repo. (RHEL 7.5; Vim 7.4).

2

4 Answers 4

26

Usually, C-[ is also a way to get out of insert mode. Some people like to map jj in insert mode as a way to exit and return to normal mode. I personally prefer jh as my fingers 'slide' better with this and I don't strain my middle finger much.

From somewhere in my neovim settings:

inoremap jh <Esc>

This remaps jh to escape in insert mode.

Is this what you wanted?

9
  • 4
    Ctrl-[ is the same as 'ESC'
    – Herb
    Aug 1, 2018 at 21:20
  • 1
    I played around and also discovered this works: ":inoremap <F1> <Esc>". This maps the F1 key in the place of the Esc key. This can be shortened to ":ino <F1> <Esc>".
    – Beam Davis
    Aug 1, 2018 at 23:31
  • 1
    Also, the capital letters aren't necessary.
    – Beam Davis
    Aug 1, 2018 at 23:38
  • Yeah, capitals aren't necessary and 'ino' is shorter to write, but in my configs I prefer brevity over cool-shortcuts. I'm glad I helped!
    – Amir Eldor
    Aug 2, 2018 at 12:48
  • 1
    While brevity is cool and everything...it's the soul of wit after all...I'm guessing you meant something else. Clarity? Verbosity? Non-brevity? ;)
    – B Layer
    Aug 28, 2018 at 4:11
17

TL;DR

It's actually nice to remap esc to the home row. Here's how you decide what to map it to: Pick a hand and roll your 4 fingers across your desktop as fast as you can in whichever direction is fastest. Use that to choose what keys to remap.

For me it's more natural to roll from pinky to index finger. Therefore I chose to use kj. Also, I did a grep -rHin kj ~/src (recursive grep on the parent directory of all my source code) and found no matches.

" esc in insert & visual mode
inoremap kj <esc>
vnoremap kj <esc>

" esc in command mode
cnoremap kj <C-C>
" Note: In command mode mappings to esc run the command for some odd
" historical vi compatibility reason. We use the alternate method of
" existing which is Ctrl-C

Note: If you do need to type a literal kj (or whatever your mapping is) just type them with 2 seconds in between. (You'll see the cursor hesitate to move after the 1st char. Wait for it to move before typing the 2nd.)

You can shorten this delay by setting a shorter timeout to timeoutlen. For example, to have a timeout of 500ms add the following to your vimrc:

set timeoutlen=500

I have been using vim for 19 years. I was horrified when Apple updated their MacBook Pros to have a touch bar and no physical esc key. I saw a post that suggested mapping jj and jk to <Esc> and hated it. But I patiently jotted down on an index card every time it annoyed me. About 15 minutes later I reviewed my notes and came up with the TL;DR above to address the following:

  1. I tend to hit esc allot when I'm not in insert mode just to "be sure" and jj would drop me 2 lines.
  2. jk is a no-op (down, up) but kj is more natural for my hands.
  3. I also need to use esc to exit command mode without completing the command.

It's my hope that I can finally break my habit of hitting esc in other applications and closing windows when I'm several minutes into writing. I'd much rather insert stray kj characters than lose my work!

9
  • 1
    Note, for your cnoremap, when the esc comes from a mapping, it will execute the command, rather than aborting it. That is for compatibility reasons with vi Dec 4, 2018 at 7:01
  • 2
    the point is, for command mode, an escape that comes from the mapping will not abort the command, but run it. Try :ls kj Dec 4, 2018 at 7:46
  • 1
    @ChristianBrabandt Thanks for pointing that out. I changed the mapping to Ctrl-C which also exists command mode. I had to come up with that on my own. I cannot find the issue (or Ctrl-C) documented or discussed anywhere. I'd love to link to a reference if you can find one. Feel free to edit my answer. Dec 6, 2018 at 3:40
  • 1
    I don't remember whether this is being documented, I stumbled over that behaviour several years ago. Ah, it is kind of documented at :h c_Esc: In macros [...] start entered command. (Macros usually also include mappings) Dec 6, 2018 at 7:12
  • 1
    Given the lack of physical escape key I've gotten into the habit of pressing C-[ to generate an escape. Unfortunately I also have a habit of mis-hitting even that and pressingC-] which then brings up the CTag completion list. To resolve that I've just added inoremap <C-]> <Esc>
    – Alnitak
    Jun 10, 2019 at 10:14
3

If you have a non-English keyboard layout, you can turn the extra keys into your advantage!

For example, Hungarian layout has the ö (o umlaut) where 0 would be on an English keyboard. Most would never use this letter in vim.

So I just remap Esc to it everywhere without fear. But as Bruno found out, in command mode remapping to Esc alters the behaviour, so there we use the equivalent Ctrl+C:

nnoremap ö <esc>         " Remap in Normal mode
inoremap ö <esc>         " Remap in Insert and Replace mode
vnoremap ö <esc>         " Remap in Visual and Select mode
xnoremap ö <esc>         " Remap in Visual mode
snoremap ö <esc>         " Remap in Select mode
cnoremap ö <C-C>         " Remap in Command-line mode
onoremap ö <esc>         " Remap in Operator pending mode

If you miss the ability to type that letter, you can simply assign it to Ctrl+ö in insert and command mode. (I can't think of an use for it in other modes, but of course you can add the bindings to all of them.)

inoremap <C-ö> ö         " Remap in Insert and Replace mode
cnoremap <C-ö> ö         " Remap in Command-line mode

Put the above commands in your ~/.vimrc.

3
  • 2
    They are not equivalent! Simple example: 5ii<esc> vs 5ii<c-c>
    – D. Ben Knoble
    Nov 10, 2020 at 13:22
  • @D.BenKnoble I mean, (effectively) equivalent in command mode. But I'm not a Vim expert, so please correct me if I'm wrong!
    – Neinstein
    Nov 30, 2021 at 10:14
  • Oh, for some reason I jumped ahead without reading fully :) I think for : commands though C-c being Esc depends on a compatibility option, because Esc can be set to execute rather than cancel the command. And C-c always cancels, IIRC
    – D. Ben Knoble
    Nov 30, 2021 at 12:23
0

If you don't have a qwerty keyboard and you cannot press <C-[>, simply <C-C> works fine ! And for any mode ! the only sad thing is the prompt when you press <C-C> in normal mode, telling you that you should press :qa to quit vim.

2
  • 3
    See my comment on Neinstein’s answer. They are not equivalent, it will not work “just fine”.
    – D. Ben Knoble
    Apr 10, 2021 at 13:24
  • thanks for that, i did not know that they where not the same ! Sep 19, 2021 at 14:30

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.