2016-04-19

Remap Keyboard Shortcut on Mac OSX

Recently I bought a secondhand Macbook Pro, since it's sold in a really-really good price. This is my first time experiencing Mac OSX, and my experience has been quite good, almost everything is there and easy as easy as yaourt+better automation (called brew), better UI than Windows, and I never feel any lag (maybe because it's SSD). All great, except, the shortcut keys, it's quite weird to have Fn key on the left of Ctrl key (just like most Lenovo laptops), also command key that replaces almost every normal shortcut on Windows and Linux.

First few days, using the shortcuts, I quite dissatisfied until I found about Karabiner! With that app, I could remap every keyboard shortcuts that I like so it becomes similar to Linux and Windows settings.


First thing I've done is switch Ctrl and Fn and vice versa. For all other shortcut, I edit the private.xml to match my preferred shortcut to Mac's shortcut. These are the content of the file:

<?xml version="1.0"?>
<root>
    <appdef>
        <appname>ITERM2</appname>
        <equal>com.googlecode.iterm2</equal>
    </appdef>
    <appdef>
        <appname>INTELLIJ</appname>
        <equal>com.jetbrains.intellij</equal>
    </appdef>
    <item>
        <name>Command-Q to Control-C</name>
        <appendix>Cmd-Q to Ctrl-C for iTerm2</appendix>
        <identifier>CQ2CC</identifier>
        <only>ITERM2</only>
        <autogen>
            __KeyToKey__
            KeyCode::Q, ModifierFlag::COMMAND_L,
            KeyCode::C, ModifierFlag::CONTROL_L
        </autogen>
    </item>
    <item>
        <name>Control to Alt</name>
        <appendix>Ctrl to Alt (Left/Right/Backspace)</appendix>
        <identifier>C2A</identifier>
        <not>INTELLIJ</not>
        <autogen>
            __KeyToKey__
            KeyCode::DELETE, ModifierFlag::CONTROL_L,
            KeyCode::DELETE, ModifierFlag::OPTION_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::CURSOR_LEFT, ModifierFlag::CONTROL_L,
            KeyCode::CURSOR_LEFT, ModifierFlag::OPTION_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::CURSOR_RIGHT, ModifierFlag::CONTROL_L,
            KeyCode::CURSOR_RIGHT, ModifierFlag::OPTION_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::CURSOR_LEFT, ModifierFlag::CONTROL_L | ModifierFlag::SHIFT_L,
            KeyCode::CURSOR_LEFT, ModifierFlag::OPTION_L | ModifierFlag::SHIFT_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::CURSOR_RIGHT, ModifierFlag::CONTROL_L | ModifierFlag::SHIFT_L,
            KeyCode::CURSOR_RIGHT, ModifierFlag::OPTION_L | ModifierFlag::SHIFT_L
        </autogen>
    </item>
    <item>
        <name>Control to Command</name>
        <appendix>Ctrl to Cmd (A/C/F/R/S/T/V/W/X/Z)</appendix>
        <identifier>C2C</identifier>
        <autogen>
            __KeyToKey__
            KeyCode::A, ModifierFlag::CONTROL_L,
            KeyCode::A, ModifierFlag::COMMAND_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::C, ModifierFlag::CONTROL_L,
            KeyCode::C, ModifierFlag::COMMAND_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::F, ModifierFlag::CONTROL_L,
            KeyCode::F, ModifierFlag::COMMAND_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::L, ModifierFlag::CONTROL_L,
            KeyCode::L, ModifierFlag::COMMAND_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::R, ModifierFlag::CONTROL_L,
            KeyCode::R, ModifierFlag::COMMAND_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::S, ModifierFlag::CONTROL_L,
            KeyCode::S, ModifierFlag::COMMAND_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::T, ModifierFlag::CONTROL_L,
            KeyCode::T, ModifierFlag::COMMAND_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::V, ModifierFlag::CONTROL_L,
            KeyCode::V, ModifierFlag::COMMAND_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::W, ModifierFlag::CONTROL_L,
            KeyCode::W, ModifierFlag::COMMAND_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::X, ModifierFlag::CONTROL_L,
            KeyCode::X, ModifierFlag::COMMAND_L
        </autogen>
        <autogen>
            __KeyToKey__
            KeyCode::Z, ModifierFlag::CONTROL_L,
            KeyCode::Z, ModifierFlag::COMMAND_L
        </autogen>
    </item>
</root>

Keyboard map above will trigger most of mac shortcut, but using Ctrl key instead of Cmd key:

  • Cmd-Q will trigger Ctrl-C (exit current program) on iTerm2
  • Ctrl-Left/Right: skip word
  • Ctrl-Shift-Left/Right: select word
  • Ctrl-Backspace: delete 1 word
  • Ctrl-A: select all
  • Ctrl-C: copy
  • Ctrl-F: find
  • Ctrl-L: location
  • Ctrl-R: refresh
  • Ctrl-S: save
  • Ctrl-T: new tab
  • Ctrl-V: paste
  • Ctrl-W: close tab
  • Ctrl-X: cut
  • Ctrl-Z: undo
I didn't put some other shortcuts that I didn't use (mostly because it's can be configured on the IDE/TextEditor) or just because it's already works well, for example Ctrl-Tab for switching tab, Ctrl-D for logout, and so on. If you want to learn more, you can read the reference here.