27

I saw the vim wiki tips and it says that in order to remap Esc to CAPS LOCK you have to edit the following windows code:

REGEDIT4
[HKEY_CURRENT_USER\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,01,00,3a,00,00,00,00,00

Is it possible to remap Esc to CAPS LOCK by only adding or modifying lines in the _vimrc?

ikh
  • 10,119
  • 1
  • 31
  • 70
alexchenco
  • 53,565
  • 76
  • 241
  • 413
  • Sorry, I *have* to ask: why do you want to do this? – paxdiablo Jan 27 '10 at 06:53
  • 15
    paxdiablo, this is extremely common. Why? Because CAPS LOCK is the most useless key ever, but has prime real estate. It is a much handier location for, say, another ctrl key, or, if you've ever used Vim, the much-needed ESC key. – Jay Jan 27 '10 at 06:58
  • Sorry, I misunderstood. I thought you wanted to use the ESC key as CAPSLOCK. Still I have a hard time understanding that too much time would be saved by moving the ESC key down an inch or two. Have you actually measured the time wasted throughout the day? Perhaps you should think about a non-modal editor :-) – paxdiablo Jan 27 '10 at 07:03
  • 13
    "Perhaps you should think about a non-modal editor" You obviously don't use Vim. – FlemishBeeCycle Jan 27 '10 at 07:05
  • I _do_ use vim, but strictly in its modal sense (I come from an old-school vi background). Many a keyboard has been thrown out because the ESC key was the first to go :-) – paxdiablo Jan 27 '10 at 07:06
  • 5
    One wouldn't have to throw out the keyboard if remapped to caps. – Jay Jan 27 '10 at 07:15
  • 2
    I would advise against this. If you get used to having Caps Lock be your Esc, what happens when you begin working on another machine than your own? One of the great benefits of Vim is that it is platform independent and always works the same (as long as you have your .vim folder and .vimrc available) regardless of what OS you're on. By doing this you'll form a bad habit that is not portable. – Pierre-Antoine LaFayette Jan 27 '10 at 13:08
  • 5
    @Pierre: I'm one of the CAPS-LOCK to ESC mappers. I can confirm that working on a machine without the remapping is annoying for me. However it is still worth the productivity increase, as 99% of the time I work on my own machines (or SSH from my own machine) which has the remapping. – Wim Coenen Jan 27 '10 at 14:56
  • 5
    I use [Uncap](https://github.com/susam/uncap). I wrote it to conveniently map Caps Lock to Escape. It only requires downloading `uncap.exe` from the [project page](https://github.com/susam/uncap#readme) and double-clicking it. No installation, configuration or reboot is required. Thus it is easy to use it on a machine other than my own too. The mapping can be turned off easily by killing this program with the `uncap -k` command. Additionally, this project comes with [detailed documentation](https://github.com/susam/uncap#readme) on other ways of mapping keys on Windows, Linux and Mac OS/X. – Susam Pal Dec 19 '15 at 07:56
  • 2
    @SusamPal: Thank you so much for uncap! This is the only solution (and a great one) whenever I don't have admin privileges. – pbarill Jul 23 '18 at 14:44
  • It's worth knowing that Ctrl + [ is an alternative for ESC. I found that out rather late and it's been a life changer. No remapping required. It's the standard alternative for it in the command line, not just in vim. So it isn't surprising that it works elsewhere sometimes. – joeljpa May 23 '23 at 07:55

7 Answers7

29

I recommend that you use AutoHotkey for this.

You can do a per-application hotkey change:

SetTitleMatchMode,2
#IfWinActive,VIM
   CAPSLOCK::ESC
return

#IfWinActive
   CAPSLOCK::CTRL
return

This script, for example sets caps to escape in vim, and control everywhere else.

Jay
  • 56,361
  • 10
  • 99
  • 123
3

There's no vim key identifier for the capslock key, so you have to do it with a scan code.

It would be like trying to map the ctrl key or the shift key to something - those keys don't do anything by themselves, they are key modifiers, so they can't be trapped the same way.

Jansen Price
  • 2,452
  • 1
  • 14
  • 5
2

The mapping has to be done at the OS level instead of vim, so the complication comes from the OS. For example, on my Ubuntu machine I have the following in ~/.Xmodmap:

! Esc on caps lock
remove Lock = Caps_Lock
keysym Caps_Lock = Escape

However, I don't think the REGEDIT script is so bad. It's kind of ugly, but it's only two lines!

permanuno
  • 429
  • 3
  • 4
  • 8
    Easier: `setxkbmap -option caps:escape` or add that to the relevant section of the X/input configuration. – ephemient Jan 28 '10 at 23:10
  • 1
    It is also possible to do this via the GUI. Go to System/Preferences/Keyboard, open the Layout tab, open the Options dialog. Under the Caps Lock heading there are a list of different behaviours to chose from and one of them is swapping Caps Lock and ESC. – hugomg Feb 10 '12 at 15:32
1

Another alternative that worked well for me was to use the instructions found in this website

Essentially: Install Microsoft's Keyboard Layout Creator, then edit its source code to allow remapping of CAPS LOCK and create your own custom layout.

graysonian
  • 21
  • 1
1

You could also use Ctrl-C to escape (Available at least in vim). Which is what I normally use. I commonly swap control and capslock, though, and have done so through registry settings in windows and keyboard conf in ubuntu.

Edit: as @mMontu kindly notes, Ctrl-C is more brutal than Esc: As stated in this appropriate question,

vim isn't going to bother checking if you just wrote part of an abbreviation, and it isn't going to run the fancy auto commands your plugins have set up for every time you leave insert mode

I hadn't noticed because my vimming is not so advanced yet.

Community
  • 1
  • 1
Adriano Varoli Piazza
  • 7,297
  • 5
  • 39
  • 50
  • 3
    You should be aware of the subtle difference between Ctrl-C and ESC: http://stackoverflow.com/questions/5030164/whats-the-difference-between-ctrlc-and-ctrl/5036294#5036294 – mMontu Oct 17 '11 at 18:41
1

Also you can try this: SharpKeys. I swaped CapLock and Esc with it.

rp101
  • 1,239
  • 2
  • 10
  • 10
0

Remap CAPSLOCK to ESC and CTRL on Windows

A very nice tool that I've already described in this answer gives you the possibility to remap CAPSLOCK to both

  1. to ESC (when pressed alone) and
  2. to CTRL (when pressed with other keys)

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

Wolfson
  • 1,187
  • 17
  • 22