How to map CAPS LOCK key in VIM?

2019-01-08 12:17发布

问题:

I'm using GVIM under Windows. And want to map CAPSLOCK to Ctrl+^

Any way to do this?

Btw, I see tons of samples over the web how to swap CAPS and Esc using registry hack, but none of them use VIM map command, instead external tools and registry changes.

回答1:

Linux? With X, use xmodmap to alter the key mapping, e.g.

xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'

Will map Esc to the CapsLock key. Google for more examples.



回答2:

If your intention is just to avoid working outside of Vim, you can put these lines in your .vimrc:

au VimEnter * !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
au VimLeave * !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Caps_Lock'

The first line maps escape to the caps lock key when you enter Vim, and the second line returns normal functionality to caps lock when you quit.

This requires Linux with the xorg-xmodmap package installed.



回答3:

Under windows? Use AutoHotkey. It's not a vim mapping, but as the others have stated you can't map it. I use AHK to map my CAPSLOCK to CTRL.



回答4:

Capslock (and Control, and Shift etc.) is a modifier key, which means that it's used with another normal key to modify the meaning of that key. AFAIK the OS does not pass the modifier keys to the application unless a normal key has also been pressed, e.g. pressing CTRL will not be seen by the application, but CTRL-C will be.



回答5:

In Linux systems this can be done with xmodmap.

Save this in a text file in the home folder

! Swap caps lock and escape
remove Lock = Caps_Lock
keysym Escape = Caps_Lock
keysym Caps_Lock = Escape
add Lock = Caps_Lock

Save this file with a name like .capstoescswitc

Then execute this file via the terminal.

xmodmap ~/.capstoescswitc 

If want to reveres it simply switch the key variables in the script file.

For more info refer this page



回答6:

For Mac OS, you can remap the 'caps lock' key system wide in 'system preferences'.

Follow this path:

system preferences > keyboard > modifier keys

Then click the drop down box next to 'caps lock' and choose '^ Control'.



回答7:

I dont think you can. I believe CAPS-LOCK is probably translated by the OS before vim ever sees it. So you'd need to do a hack at the OS level, like the registry hacks you've already seen.

EDIT: autohotkey looks like it could be used to bridge the vim-OS gap. This way a thirdparty app is doing the hacks at the OS level, and you're just hooking that app.



回答8:

Solution that doesn't break Caps Lock outside of vim

Windows

  1. Install autohotkey.
  2. Run autohotkey script:

```

;caps_to_esc.ahk
#IfWinActive, ahk_class Vim ; vim window class
Capslock::Esc
#IfWinActive

```

Linux

  1. sudo apt-get install xdotool xbindkeys python. We will also use xprop and cut.
  2. Create a ~/caps_to_esc.sh script:

```

#!/bin/bash

capsOff () {
  python -c '\
    from ctypes import *; \
    X11 = cdll.LoadLibrary("libX11.so.6"); \
    display = X11.XOpenDisplay(None); \
    X11.XkbLockModifiers(display, c_uint(0x0100), c_uint(2), c_uint(0)); \
    X11.XCloseDisplay(display) \
  '
}

declare -a wm_classes=( \
  'WM_CLASS(STRING) = "gnome-terminal-server", "Gnome-terminal"' \
  'WM_CLASS(STRING) = "gvim", "Gvim"' \
  'WM_CLASS(STRING) = "code", "Code"' \
  'WM_CLASS(STRING) = "google-chrome", "Google-chrome"' \
)

active_window_id=$(xprop -root _NET_ACTIVE_WINDOW | cut -f2 -d#)
active_window_wm_class=$(xprop -id $active_window_id WM_CLASS)

for wm_class in "${wm_classes[@]}"; do
  # echo "$wm_class" >> xbindkeys.debug
  if [ "$active_window_wm_class" == "$wm_class" ]; then
    # echo true >> xbindkeys.debug
    xdotool getactivewindow key Escape
    capsOff
  fi
done

```

  1. sudo chmod +x ~/caps_to_esc.sh
  2. Add new bindnig to ~/.xbindkeysrc:

    "~/caps_to_esc.sh"
    Caps_Lock
    
  3. xbindkeys (if you have already started xbindkes then kill it first by killall xbindkeys).



回答9:

Since there is a solution for Linux and Windows(Autohotkey), I´d like to suggest to use pckeyboardhack for Mac to remap CapsLock everywhere.



回答10:

I guess one of the reasons for doing this is to create a soft capslock, like others have mentioned, possibly to avoid keeping capslock on while in normal mode. I've used the vimcaps plugin to turn off capslock when leaving insert mode, seems to work okay.



回答11:

On mac, it is also possible to use Karabiner (https://pqrs.org/osx/karabiner/)

$ brew cask install karabiner-elements

Once installed, you can map capslock key to esc key in the simple modifications tab. Caveat is this is system wide, meaning that you lose capslock key everywhere. IMO who needs capslock.



标签: map vim capslock