80

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.

7
  • I think you should choose Dan Andreatta's answer below. Commented Jun 15, 2012 at 5:01
  • 7
    Accidentally hitting caps lock in command mode is the best part of my day. Commented Jan 21, 2015 at 21:12
  • Several old answers to this question suggest using xmodmap to map Caps Lock on Linux. However, anyone trying to do this in 2016 should use the setxkbmap command, e.g. setxkbmap -option caps:escape. I have a little project called Uncap at github.com/susam/uncap that documents all this and also provides an unobtrusive little tool for Windows to map Caps Lock to Escape. See the Alternatives section of the README for details about using setxkbmap on Linux.
    – Susam Pal
    Commented Dec 9, 2016 at 14:13
  • for mapping CAPSLOCK to CTRL (when pressed with other keys) AND ESC (when pressed alone) on Windows, there is a nice piece of SW (see my answer below).
    – Wolfson
    Commented May 19, 2020 at 14:19
  • added a solution for Linux to map CAPSLOCK to CTRL AND ESC below as well.
    – Wolfson
    Commented Sep 3, 2020 at 9:54

16 Answers 16

66

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.

3
  • 25
    I would have given you a point except you said "Google for more examples" instead of providing a link. Whenever somebody does that they always end up at the top of the mentioned Google results: google.com/search?q=vim+remap+escape+capslock
    – Gerry
    Commented Jul 7, 2012 at 21:25
  • 6
    In the question was stated explicitly that this ("registry hack") is not acceptable.
    – Kokozaurus
    Commented Oct 5, 2013 at 14:39
  • 2
    Love this hack. But it quite annoying when my eyes distracted by caps lock winking LED .
    – Brain90
    Commented May 13, 2015 at 2:14
51

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.

4
  • 10
    You can modify with :silent before the ! to avoid the press ENTER to continue prompts.
    – npit
    Commented Oct 11, 2017 at 11:44
  • 20
    To be clear, to avoid the prompt the code is au VimEnter * silent! !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape' and au VimLeave * silent! !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Caps_Lock'
    – KadeG
    Commented Apr 20, 2018 at 20:24
  • 5
    Note that au is autocmd
    – barlop
    Commented Jan 12, 2020 at 23:11
  • 3
    its good to be verbose in .vimrc
    – user12711
    Commented Jan 7, 2022 at 13:30
22

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

3
  • it will only change to control. How to remap it to Esc Commented Nov 1, 2016 at 8:39
  • 3
    Just choose 'Escape' from the drop down :) Commented Nov 1, 2016 at 13:54
  • 3
    I think the option is available only from OS X 10.12 Commented Nov 1, 2016 at 15:30
16

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.

2
13

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

1
  • Be careful, this script will swap Caps_Lock and Escape every time it is executed. See comments in the mentioned Vim wiki page.
    – carbolymer
    Commented Dec 7, 2017 at 9:35
11

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.

3
  • 9
    Not quite. It is perfectly possible for an application to detect ctrl, shift, alt and windows key presses. The problem with keys like Caps Lock and Num Lock is that the OS insists on interpreting them as a toggling key.
    – Wim Coenen
    Commented Feb 1, 2010 at 15:00
  • 1
    So can you use it to toggle between insert and command modes? Or is there something implied by your statement that I'm not aware of?
    – iconoclast
    Commented Nov 2, 2012 at 15:14
  • 3
    Yes, you could do so in most OSes if you wrote a native app. Unfortunately, if you write an app that gets its input from another app (like vim running inside a terminal app or a JS app running in a browser), you only get the keyboard events that the native wrapper passes to you. Old terminals didn't have modern keyboard events. I'm not aware of any terminal app that will pass along a keypress event for a press of the capslock key or any other modifier key--only modifier+otherkey, and only some of them. So, in practice, you can't map the capslock key itself to any action in vim.
    – Glen
    Commented Jan 10, 2014 at 2:30
10

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

Ubuntu

Run this command:

wget -O - https://raw.githubusercontent.com/grabantot/scripts/master/install/install_caps_to_esc.sh | bash

Or perform these actions manually:

  1. sudo apt-get install xdotool xbindkeys. We will also use xprop and xset (should be installed by default).
  2. Create a ~/caps_to_esc.sh script:
debug_file=/dev/shm/caps_to_esc.debug
debug_msg () {
  echo $(date +%s%3N) "$@" >> $debug_file
}

caps_off () {
  is_caps_on="false"
  xset q | grep "Caps Lock:\s*on" && is_caps_on="true"
  debug_msg "is_caps_on ""$is_caps_on"

  [ "$is_caps_on" == "false" ] && return 3
  debug_msg "Sending Caps Lock"
  debug_msg "ignore_next"
  xdotool key Caps_Lock
}

should_ignore="false"
tail -n 1 $debug_file | grep "ignore_next" && should_ignore="true"

if [ "$should_ignore" == "true" ]; then
  debug_msg "ignored"
  exit 1
fi

echo -n "" > $debug_file

# get wm_class by 'xprop | grep WM_CLASS'
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=$(xdotool getactivewindow)
active_window_wm_class=$(xprop -id $active_window_id WM_CLASS)
debug_msg "active_wm_class   ""$active_window_wm_class"

detected_wm_class=""
for wm_class in "${wm_classes[@]}"; do
  # debug_msg "$wm_class"
  if [ "$active_window_wm_class" == "$wm_class" ]; then
    detected_wm_class="$wm_class"
    debug_msg "detected_wm_class ""$detected_wm_class"
  fi
done

[ "$detected_wm_class" == "" ] && exit 2
xdotool keyup "Caps_Lock" # !!! very important
caps_off
debug_msg "Sending Escape"
xdotool key "Escape"
debug_msg "sent"
  1. Add new bindnig to ~/.xbindkeysrc:
"bash $HOME/caps_to_esc.sh"
Caps_Lock
  1. killall xbindkeys && xbindkeys

How it works:

  1. xbindkeys will detetect when Caps_Lock is pressed and call caps_to_esc.sh script
  2. in the script detect active window wm_class by xprop
  3. check if wm_class is of interest for us (gnome-terminal, vscode, gvim, chrome), exit if it is not
  4. send Escape key via xdotool
  5. check if Caps Lock is on via xset and if it is then send Caps_Lock key via xdotool
  6. xbindkeys will detect the Caps_Lock sent by us but we ignore it
1
  • This is awesome. I work on Windows at the office (and can't hack up the registry, but AHK is installed); Linux at home. Both of those places are really just a KVM apart, and now I have a lot better selection of keyboards to select from (not just uberpriced niche G60 types). Nicely done!
    – cschooley
    Commented Nov 8, 2017 at 22:30
5

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.

4

@rsoren's answer works. But the problem with that is if multiple buffers are opened, exiting from one, reverts the mapping for all of the others too. Replacing VimEnter and VimLeave with BufEnter and BufLeave, did the trick for me.

au BufEnter * silent! !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
au BufLeave * silent! !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Caps_Lock'
1
  • another way is to remap the escape key to kj By adding this line to .vimrc. " esc in command mode cnoremap kj <C-C> " esc in insert mode inoremap kj <esc>
    – EsmaeelE
    Commented Feb 17, 2022 at 3:27
3

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

1
  • You can use system preferences to remap caps lock on Mac OS. See my answer for more details. Commented Nov 20, 2014 at 15:20
2

Remap CAPSLOCK to ESC and CTRL on Windows

If you want to remap CAPSLOCK to both

  1. to ESC (when pressed alone)
  2. to CTRL (when pressed with other keys) you can 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.

Remap CAPSLOCK to ESC and CTRL on Linux

To do the same on Linux, you can use XCAPE. To install it on Ubuntu use:

sudo apt install xcape

For the actual mapping do:

setxkbmap -option ctrl:nocaps
xcape -e 'Control_L=Escape'

The 1st command is to map CAPSLOCK to ESC whereas the 2nd one takes care of CTRL pressed with other keys.

To have this permanently working in X sessions, you can add this to ~/.xprofile.

1

I went through the answers to this question to do it my own, but I was looking for something slightly different:

  1. I'm using Linux
  2. I'm happy to change CAPS even outside Vim (I press almost always by mistake, and I can keep it on the Esc button anyhow for the goals I'm using it)
  3. I'd like to use the most common software as possible, not to require any further installation
  4. I'd prefer to KISS, and then even avoid the need of further lengthy scripts
  5. I'd like to have both CAPS -> Esc AND CAPS + <something> -> Ctrl + <something>, if possible

Then my current solution it is:

# ~/.xprofile
xmodmap $HOME/.xmodmap

and:

! ~/.xmodmap
clear lock
keycode 66 = Escape
keycode 9 = Caps_Lock
add lock = Caps_Lock

clear control
add control = Control_L Control_R Escape

Looks like it's working like a charm for the time being, but I'm looking for feedbacks :)

0

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.

0

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.

0

A working example for a mere Mortal WINDOWS user like me as of 2020 is using AutoHotkey

Currently using this 1 line script:

Capslock::Esc

Run it and your problem is solved. Hope it helps. :)

0

With X (Linux), as @Dan Andreatta mentioned, use xmodmap to alter the key mapping, e.g.

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

Will map Esc to the CapsLock key.

You could also set this in stone by adding it into an X initialization file, such as .xinitrc — which is run when using startx —:

clear lock
keycode 0x42 = Escape

Typically, this is put inside an .Xmodmap file and then it's run inside .xinitrc.

Vim's documentation has an entire page dedicated to this topic, and the comments section is especially helful.

1
  • Aside from the "comments section" link, this answer isn't too useful for OP's original question, which was how to do this in Windows. Commented Mar 6, 2022 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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