50

I use Debian and I switched from Vim to Visual Studio Code, when I noticed, that there is a Vim movement extension for VS Code from vscodevim. While I was using Vim, I mapped my Caps lock key to the ESC key. In Vim it works perfectly, but VS Code is not reacting on a Caps lock input.

I hope there is a solution. If there are any questions feel free to ask.

This is how I changed my keyboard layout: I navigated to /etc/default/ and edited the keyboard file. This is how it looks now

# KEYBOARD CONFIGURATION FILE

# Consult the keyboard(5) manual page.

XKBMODEL="pc105"
XKBLAYOUT="de"
XKBVARIANT=""
XKBOPTIONS="caps:escape"

BACKSPACE="guess"

The Caps lock key works also in other programs e.x. "Synaptic Package Manager"

Gama11
  • 31,714
  • 9
  • 78
  • 100
Kevin
  • 601
  • 1
  • 5
  • 4
  • How did you map Caps lock? Do other applications see the mapping, or just Vim? – Ingo Karkat Jan 24 '18 at 12:44
  • Thanks; that'll help. I use Ubuntu, and did the same remapping via the GNOME Tweak Tool (Make Caps Lock an additional ESC key). It works in all applications, too. I don't have Visual Studio Code, though. – Ingo Karkat Jan 24 '18 at 15:35

6 Answers6

101

Late answer, but may be useful for someone:

Switched CAPS and ESC in Ubuntu over the keyboard preferences section. Worked in almost all applications, but not in Visual Studio Code.

So I modified VS Code settings:

{
    "keyboard.dispatch": "keyCode"
}

and it worked.

Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
NaN
  • 7,441
  • 6
  • 32
  • 51
19

For those who have the same problems on Linux:

If in your system wide config, Caps Lock is Esc, Esc is Caps Lock ( ['caps:swapescape'] ),

  1. disable the key mapping in your system. Make it default.

  2. In VSCode, press ctrl+shift+p and select Preferences: Open Keyboard Sortcuts

  3. search for the vim_escape

  4. double click on it and press original Caps Lock and save it.

  5. Edit your keymap config in your system.

That is it. There is no problem. Keys are working as expected.

DuDa
  • 3,718
  • 4
  • 16
  • 36
msyargin
  • 191
  • 1
  • 2
  • 8
    This would not be the best solution if you like swapping Caps ans Esc system wide. – sergeysynergy May 13 '21 at 10:54
  • Unfortunately, this will not work as expected. The caps lock has the unintended consequence of changing the letterCase (go figure!). So if we're in upperCase in normal mode, then all the keybindings are interpreted as their uppercase versions. If we look at the insert, then it's too tedious, cause all the letters are typed as uppercase, unless you manually hold shift for each key. All round bad answer. Would retract my upvote if I could – sayandcode Jul 25 '22 at 10:54
  • On another note, this could work, if mentally we understand that vim_escape requires two strokes of the Caps Lock key, in order to circumvent the upperCase letter bindings. – sayandcode Jul 25 '22 at 10:57
6
  1. Set the Caps-lock to act as Esc in your settings
  2. Post this in your keybindings json for vscode (Ctrl-K Ctrl-S -> open the JSON from the icon on the top right corner):
  {
    "key": "capslock",
    "command": "extension.vim_escape",
    "when": "editorTextFocus && vim.active && !inDebugRepl"
  },

If you are looking for a system wide tool, you might be interested in keyd https://github.com/rvaiya/keyd. You can avoid this setup. The readme has an example on how setup caps lock as both ctrl and esc.

justCozz
  • 61
  • 2
  • 4
5

Ubuntu solution

  • First make sure you swaped escape with capslock like: source
gsettings set org.gnome.desktop.input-sources xkb-options "['caps:swapescape']"
  • Then inside VSCode press ctrl+shift+p search for Preferences: Open keyboard Shortcuts (JSON) (it will open keybindings.json) and paste:
    {
        "key": "capslock",
        "command": "extension.vim_escape",
        "when": "editorTextFocus && vim.active && !inDebugRepl"
    },
    {
        "key": "escape",
        "command": "-extension.vim_escape",
        "when": "editorTextFocus && vim.active && !inDebugRepl"
    }
  • Reload VSCode and you are good to go.
Giorgos Xou
  • 1,461
  • 1
  • 13
  • 32
4

Enlighten by an answer from this question.
In Visual Studio Code, how to toggle between vim-emulation and no-vim-emulation when the vscodevim extension is installed?

Another solution is
Click the "Gear icon" -> Keyboard Shortcuts->type "esc" or "Escape" ->
find extension.vim_escape -> edit by click the pencil icon or right click choose change keybinding
->type you desired key and ENTER

Shihe Zhang
  • 2,641
  • 5
  • 36
  • 57
0

Windows solution

  • Install AutoHotkey
  • Go to AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
  • Create a file caps_to_esc.ahk and insert code:
#IfWinActive ahk_exe Code.exe
Capslock::Esc
Esc::Capslock
#IfWinActive
  • Save, run the file and you are good to go.
Giorgos Xou
  • 1,461
  • 1
  • 13
  • 32