Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing VSCode keybindings on Ubuntu from linux-like to Mac-like

I am running on a Mac, and using VMWare Fusion to run Ubuntu, where I have VS Code installed. Naturally the default install on Ubuntu has linux-style shortcuts, in particular, control-c (copy), control-v (paste), control-w (close window.) To make my life easier I would like to have VS Code, under Ubuntu, under VMWare to have Mac-like shortcuts, i.e. command-c, command-v, command-w, respectively, and so on. I looked through the docs and the UI and can't find a way to make this change other than manually changing each one. Any tips?

like image 596
pitosalas Avatar asked Jul 23 '18 00:07

pitosalas


People also ask

How do I change the Keybindings in VS Code?

All keyboard shortcuts in VS Code can be customized via the keybindings. json file. To configure keyboard shortcuts through the JSON file, open Keyboard Shortcuts editor and select the Open Keyboard Shortcuts (JSON) button on the right of the editor title bar. This will open your keybindings.

How do I change Keybinds in Linux?

Key bindings can be customized using the Gnome Control Center. It can be found as "System Settings" in the menu or accessed on the command line using "gnome-control-center". The key bindings are in the "Keyboard" section under a "Shortcuts" tab.


3 Answers

I believe you can edit the keybindings.json file located in the .config/Code/User/ directory to override the default keyboard shortcuts. That would not require you to edit it one by one.

I'd first get the default keyboard shortcuts file open. To do that, open the VS Code command pallete by going to:

View -> Command Pallete... (or by pressing Ctrl + Shift + p)

This will open up a textbox where you can enter you command: Then type in something like Shortcuts File and it will list a matching option which should look like:

Open Keyboard Shortcuts File

Note that it's different from "Open Keyboard Shortcuts" which opens a UI to edit the shortcuts.

Once you select the above command, it should open up your keybindings in a split view, where the left hand pane will show you a read only default shortcuts file, and the right hand pane will show you the custom keyboard shortcut bindings. (which is empty by default). Should look like this:

enter image description here

Then copy everything from the left pane (default shortcuts) and paste it on the keybindings.json file (right hand pane).

Now you can do a search and replace which is faster than manually changing each shortcut.

Hope it helps!!

like image 121
Nimeshka Srimal Avatar answered Oct 01 '22 14:10

Nimeshka Srimal


Full disclosure

I am the author of Kinto.

You might be able to lessen your work by simply swapping the keymap out before creating new bindings, however if you use the terminal any at all on Ubuntu, or any linux distro, that mac illusion will quickly fade. Also the Cmd + arrow keys won't behave the same unless you create a custom keymap for it.

If you want to create less work for yourself then I would recommend using Kinto. It will handle applying the proper (and native) keymap by listening to what app you're currently using. It does not intercept keys pressed like Autokey or other 3rd party rebinding/remapping tools.

Kinto also provides a custom keymap for normal mac Cmd + arrow key behavior & now Alt + arrow key behavior as well.

https://github.com/rbreaves/kinto

** Update 6/16/20 **

Kinto now uses xkeysnail, it is simpler and python based. Also works on a lower udev input level. Easier to configure as well because all settings belong in a single python based file. You can use kinto or xkeysnail to do what you want.

https://github.com/mooz/xkeysnail

** Old XKB method explained below **

https://medium.com/@benreaves/kinto-a-mac-inspired-keyboard-mapping-for-linux-58f731817c0

Here's a Gist as well, if you just want to see what is at the heart of it all, it will not alternate your keymap when needed though. The Gist also does not include custom xkb keymap files that setup macOS style cursors/word-wise manipulations that use Cmd and the arrow keys.

https://gist.github.com/rbreaves/f4cf8a991eaeea893999964f5e83eebb

Edit: Following the advice of another member I will also include the actual content of the gist, since I can't realistically include all the code and files that went into creating Kinto. This gist and Kinto are identical aside from Kinto's ability to dynamically change your keymap and apply more custom keymaps specific to macOS behavior.

# permanent apple keyboard keyswap
echo "options hid_apple swap_opt_cmd=1" | sudo tee -a /etc/modprobe.d/hid_apple.conf
update-initramfs -u -k all

# Temporary & instant apple keyboard keyswap
echo '1' | sudo tee -a /sys/module/hid_apple/parameters/swap_opt_cmd

# Windows and Mac keyboards - GUI (Physical Alt is Ctrl, Physical Super is Alt, Physical Ctrl is Super)
setxkbmap -option;setxkbmap -option altwin:ctrl_alt_win

# Windows and Mac keyboards - Terminal Apps (Physical Alt is Super, Physical Super is Alt, Physical Ctrl is Ctrl)
setxkbmap -option;setxkbmap -option altwin:swap_alt_win

#
# If you want a systemd service and bash script to help toggle between
# GUI and Terminal applications then look at project Kinto.
# https://github.com/rbreaves/kinto
#
# Note: The above may not work for Chromebooks running Linux, please look
# at project Kinto for that.
#
# If anyone would like to contribute to the project then please do!
#
like image 43
R Ben R Avatar answered Oct 01 '22 14:10

R Ben R


The problem you might have is that Command and Alt/Option keys are swapped between Mac and PC keyboards. On a Mac, Command is next to space bar and on PC this is usually Alt key. I followed the answer given by Nimeshka and added two steps:

  1. Replace alt with super (Linux name for Windows/Command key)
  2. Replace cmd with alt

Feel free to use my keybindings.json file.

like image 39
suda Avatar answered Oct 02 '22 14:10

suda