21

I generally run Linux. I use Vim a lot, so the Escape key is extremely important. I also use other keyboard shortcuts a lot, so Control is "key" as well. On Linux, I've solved this by using xcape to map the Caps lock key to Escape if pressed by itself, or Control if pressed in combination with another key. This puts the best of both worlds within quick reach of my pinky finger. Is there any way I can do this on a Mac?

18

Yes, using Karabiner (formerly KeyRemap4Macbook).

First, go into the Keyboard preference pane, click "Modifier Keys…", and change Caps Lock to Control. Then, go into Karabiner and search for this option:

Control_L to Control_L
      (+ when you type Control_L only, send Escape)

Check that, and you're done. With Karabiner you can do lots more interesting things, like mapping your (now kind of useless) Escape key to a Hyper key with even more keybindings.

  • is it possible remap esc to caps lock and map caps lock to control and escape? Because sometime, I still feel the need to use caps lock to type a long upper case string. – Aaron Shen Jul 21 '15 at 23:36
  • Karibiner now tells you to change Caps Lock to "No Action" in the OSX Keyboard preferences (instead of setting it there to Control) and to use Seil, Karabiner's sister program to do the remapping. The last part of the above answer (+ when you type Control_L only...) is still done in Karabiner. – ScoBe Oct 1 '15 at 15:16
  • @AaronShen just remap escape to caps lock ;-). – Ven Oct 28 '15 at 8:37
  • @ScoBe I'd recommend against doing that, you really don't need Seil, – Ven Oct 28 '15 at 8:38
  • I recommend reducing Karabiner's Key Overlaid Modifier > Timeout parameter from 1000ms to 200ms. If I hold the key but then change my mind and release it, I don't want to fire an Escape. – joeytwiddle Sep 4 '17 at 15:47
16

Karabiner is currently incompatible with MacOS Sierra. Another, Sierra-friendly solution, is with Keyboard Maestro (it is not free). Change the behavior of the Caps-Lock key to Control through System Preferences -> Keyboard -> Modifier Keys as others have suggested.

Then within Keyboard Maestro, setup a macro as follows:

  • Triggered by USB Device Key Trigger
  • Press the Caps-Lock key on your keyboard
  • is tapped once
  • with these modifiers: ^
  • (you will need to repeat the previous trigger for each USB keyboard you use)
  • Then add an action Text -> Type a keystroke
  • Press Esc
  • 1
    Just to be clear, there is a project called Karabiner Elements that does run on Sierra (which is a precursor to a version of Karabiner that will run on Sierra). I haven't tried it, so I don't know if this functionality is available or not. – tubedogg Oct 5 '16 at 20:51
  • 5
    Karabiner-Elements does not yet support acting on a modifier key tap, which would be required for this. – grokfully Oct 6 '16 at 20:03
  • This option 'with these modifiers: ^' only requires ^ to be pressed together with capslock to trigger an action. It's not at all what is required in the question. – defhlt Feb 18 '17 at 10:47
  • Since we first change Caps-Lock to behave like Control through System Preferences, KM sees the USB event with the Control key down. If you omit the ^ modifier, the action will never trigger. – grokfully Feb 19 '17 at 19:52
  • If you go the keyboard maestro route, is there any way to map the escape key to caps lock to regain the caps lock functionality? I love having caps lock as both ctrl and escape though. – Kvass May 19 '17 at 21:03
0

To do it with Karabiner Elements, which supports macOS 10.12 and later, open ~/.config/karabiner/karabiner.json, and add the following rule to the rules array.

{
  manipulators: [
    {
      description:
        "Change caps_lock to control when used as modifier, escape when used alone",
      from: {
        key_code: "caps_lock",
        modifiers: {
          optional: ["any"]
        }
      },
      to: [
        {
          key_code: "left_control"
        }
      ],
      to_if_alone: [
        {
          key_code: "escape",
          modifiers: {
            optional: ["any"]
          }
        }
      ],
      type: "basic"
    }
  ]
}

You can also try to use ControlEscape.spoon with Hammerspoon.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .