Use in Visual Studio Code (Vim extension) CAPS instead of ESC
Asked Answered
Z

7

62

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"

Zsigmondy answered 21/1, 2018 at 16:54 Comment(2)
How did you map Caps lock? Do other applications see the mapping, or just Vim?Fatwitted
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.Fatwitted
F
143

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.

Favrot answered 15/6, 2018 at 12:14 Comment(11)
Is it me or this option has since disappeared ?Sign
@Sign 2022 Aug, still works for me.Oust
you are a hero; this worked for meWyler
It works on PopOs(ubuntu-based linux).Decompress
This is the answer I have been searching for thank!Frontpage
2023 - still worksEvite
Oct 2023 - Works! Thanks a lot1Onomasiology
Does not work for me (Kubuntu 23.04) - vscode gets the keydown, but not the keyup...Grille
Mostly leaving this comment for myself the next time I have to do this: to get to this config file, press <C-S-p> and look for >Preferences: Open User Settings (JSON)Yours
Same issue and solution here: github.com/VSCodeVim/Vim/issues/2859Elias
I works on Debian 12 / VSC 1.87Guereza
S
23

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.

Sahib answered 19/1, 2021 at 22:27 Comment(3)
This would not be the best solution if you like swapping Caps ans Esc system wide.Cashmere
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 couldUncle
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.Uncle
P
8
  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.

Psychosomatic answered 3/7, 2022 at 16:26 Comment(2)
I tried this, and it fixes it for editing code, but not for swapping capslock and escape all over vscode. For instance, you must still use the escape key to cancel a dialog or close the search bar.Ramayana
This only enables escape for the VIM extension. I had to go through all other shortcuts that use Escape and instead mapped them to "capslock". It works, now the capslock works consistently as an Escape for dialogues, popups, etc., but it took some minutes to change everything.Recreant
S
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.
Similar answered 29/7, 2022 at 17:56 Comment(2)
This works for me on Ubuntu 22.04. Thanks!Martita
I tried this, and it fixes it for editing code, but not for swapping capslock and escape all over vscode. For instance, you must still use the escape key to cancel a dialog or close the search bar.Ramayana
N
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

Netta answered 25/10, 2018 at 1:27 Comment(0)
S
2

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.
Similar answered 30/7, 2022 at 16:45 Comment(3)
This question is specifically about Debian.Ramayana
@Ramayana I've already posted an Ubuntu solution but, I had a solution for windows too, so I've thought: knowing that there's this tendency in questions that don't explicitly mention on titles or tags anything OS oriented, to see mixed solutions, I decided to post it here. If I've missed any stackoverflow rule, let me know and II'll remove it.Similar
I was just pointing out that OP, and probably others looking for Linux-specific help, probably won't be able to use your solution. I myself would have included this answer under your Ubuntu solution. Oh, and AFAIK, it's not against any rule to post a less-than-optimal answer. If it were, I'd have been banned long ago. :-)Ramayana
E
1

In order to get the VIM keys to work normally, the following worked for me with these versions, but the changes do not affect the keyboard shortcuts outside of VSCode:

  • Ubuntu 22.04
  • VSCode v1.85.1

Change the dispatch setting

(Open VSCode) > File > Preferences > Settings > (Search "keyboard") > User > Application > Keyboard > Keyboard.dispatch > (Select "keyCode")

enter image description here

Choose your shortcuts

(Open VSCode) > File > Preferences > Keyboard Shortcuts > (Change whatever)

enter image description here

Encaenia answered 6/1 at 3:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.