简体   繁体   中英

Why is so complicated to remap Esc to CAPS LOCK in Vim?

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?

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.

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.

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!

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.

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.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM