pitosalas
pitosalas

Reputation: 10852

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?

Upvotes: 15

Views: 10037

Answers (5)

A. John Callegari
A. John Callegari

Reputation: 149

Here's how I set up my Ubuntu 24 LTS keyboard to feel like a Mac when I'm in GNOME (this works for the terminal app too, allowing you to use Command + C for Ctrl + C, etc.). With my install of Ubuntu 24, the window manager was X11, not Wayland. This solution only works for X11.

Step 1: Check if You Are Using X11

Run this command in the terminal:

echo $XDG_SESSION_TYPE

Step 2: Create an Auto-Loading App That Runs at System Start

Create the following file:

vi ~/.config/autostart/keyboard-mapping.desktop

Step 3: Add the Following Contents to the File

[Desktop Entry]
Type=Application
Name=Keyboard Mapping
Exec=/bin/bash -c "setxkbmap -option ctrl:swap_lwin_lctl"
X-GNOME-Autostart-enabled=true

Step 4: Save and Reboot Your System

Reboot your system to apply the changes.

Step 5: Open GNOME Settings and Apply Additional Mac Keyboard Shortcuts

  • Remap navigation -> Switch applications:
    Change to Ctrl + Tab (by pressing Command + Tab since Command is now mapped to Ctrl).

  • Remap navigation -> Switch windows of an application:
    Change to Ctrl + ~ (by pressing Command + ~).

  • Remap windows -> Hide window:
    Change Super + H to Ctrl + H (by pressing Command + H).

  • Remap navigation -> Hide all normal windows:
    Change to Ctrl + Shift + H (by pressing Command + Shift + H).


Upvotes: 0

shotasenga
shotasenga

Reputation: 979

codebling/vs-code-default-keybindings helped me to deal with the same problem. You can copy-and-paste the linux.negative.keybindings.json and macos.keybindings.json. (Any custom bindings should be defined after these two.) This solution worked for my system (Arch + i3wm).

Upvotes: 0

R Ben R
R Ben R

Reputation: 364

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!
#

Upvotes: 9

suda
suda

Reputation: 2644

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.

Upvotes: 5

Nimeshka Srimal
Nimeshka Srimal

Reputation: 8920

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!!

Upvotes: 8

Related Questions