skip to Main Content

I am accustomed to remapping the left ⌘ Command key remapped to Ctrl, supported by a configuration option in iTerm2, my default dev context.

I am learning VSCode and am wondering how to accomplish when within that application.

I will upvote any answer that does the job, but in descending order of preference I seek a solution that either:

  1. requires neither an extension nor an external application
  2. or requires an extension but no external application*
  3. or requires an external application

*I use vscodevim so if there is a solution involving configuration of that extension, I would value such a solution.


Why?

I developed my muscle memory using PC-layout keyboards before switching predominantly Apple-layout keyboards. Over on GitHub, other users are also seeking an answer to this question

2

Answers


  1. Keybindings in VSCode allow you to remap actions within the editor but do not support remapping modifier keys at the OS or hardware level.

    The vscodevim extension allows remapping keys within the context of Vim emulation (for Vim commands), but not the VSCode UI itself (not for general VSCode shortcuts, like saving a file or opening the command palette).


    I am aware of Karabiner and it is highly likely I will not be able to install this on my workplace machine.

    Within VSCode without external tools

    VSCode allows for extensive customization of keyboard shortcuts within the application itself through its keybindings.json file. However, it is limited to defining shortcuts for actions within VSCode and does not extend to modifying the behavior of the operating system’s modifier keys.
    That means you can reassign actions from using to Ctrl within VSCode, but you cannot change the function of the key to behave as a Ctrl key across the board within VSCode (even when limited to vscodevim).

    For example, you could manually change shortcuts that typically use on macOS to use Ctrl instead, through VSCode’s Keyboard Shortcuts editor:

    [
        {
            "key": "ctrl+c",
            "command": "editor.action.clipboardCopyAction",
            "when": "editorTextFocus"
        },
        // Add more custom bindings here
    ]
    

    Considering work restrictions

    A potential, albeit partial workaround, could involve getting creative with macOS’s built-in capabilities, such as creating custom shortcuts in System Preferences for specific actions in VSCode. However, this does not achieve a true remap of the key to Ctrl and would be very limited in scope, applying only to specific actions rather than a comprehensive key behavior change.

    You might also consider discussing with your IT department the possibility of making an exception for a tool like Karabiner-Elements, especially if it is important for your productivity and ergonomics.
    Alternatively, adapting to the default macOS shortcuts or customizing VSCode’s shortcuts within the limitations outlined might be necessary.


    For other users, outside the workplace environment:

    For system-wide remapping of the ⌘ Command key to Ctrl, you could try and use external tools like Karabiner-Elements. That application allows you to remap keys at the OS level, affecting all applications, including VSCode.

    Open Karabiner-Elements and go to the ‘Simple Modifications’ tab. Click ‘Add item’ and select your device from the dropdown.
    For ‘From key‘, choose left_command. For ‘To key‘, choose left_control.

    Again: that method would remap the key for all applications, so it affects your entire MacOS environment, not just VSCode.


    If that is too inconvenient, you would need a tool like Hammerspoon, a powerful automation tool for macOS that can detect the currently active application and execute scripts accordingly.

    You can use Hammerspoon to monitor when VSCode becomes the active window and then trigger a script to activate a specific Karabiner-Elements profile or configuration and deactivate it when VSCode loses focus.

    Open ~/.hammerspoon/init.lua in your text editor to add your custom Lua script for application monitoring. That script checks if VSCode is the active application and could be expanded to activate/deactivate a Karabiner-Elements profile via a shell command.

    function applicationWatcher(appName, eventType, appObject)
      if (appName == "Visual Studio Code") then
        if (eventType == hs.application.watcher.activated) then
          -- VSCode is focused, activate Karabiner profile
          hs.execute("/path/to/activate_karabiner_profile.sh", true)
        elseif (eventType == hs.application.watcher.deactivated) then
          -- VSCode is not focused, revert Karabiner profile
          hs.execute("/path/to/deactivate_karabiner_profile.sh", true)
        end
      end
    end
    
    appWatcher = hs.application.watcher.new(applicationWatcher)
    appWatcher:start()
    

    You would also need to create scripts (e.g., activate_karabiner_profile.sh and deactivate_karabiner_profile.sh) that modify your Karabiner-Elements configuration to activate or deactivate the desired profile. That could involve using defaults commands to modify Karabiner’s configuration files or using Karabiner’s CLI if available.

    #!/bin/bash
    # That is a placeholder command. Update it to your actual command to switch profiles.
    karabiner_cli --select-profile "VSCode"
    

    Note: Karabiner-Elements does not directly support a CLI for dynamically switching profiles. You might need to directly manipulate its configuration files or explore alternative methods such as AppleScript or automating UI actions for profile switching.

    Login or Signup to reply.
  2. As hinted in the GitHub issue ticket you linked, you can remap modifier keys at the system level. Ex. on Ventura, open the System Settings application, go to "Keyboard", then "Keyboard Shortcuts", then "Modifier Keys". Then you can switch cmd to send ctrl and ctrl to send cmd. If this is about muscle memory from non-mac usage (and it sounds like it is), then I’d recommend that over doing things on a per-application basis.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search