MacOS – How to change a key combination without using Karabiner

keybindingskeyboardmacbook promacos

I have used Karabiner to change a key combination but the problem is, it doesn't work exactly as I want.

For example, using Karabiner, I have mapped alt to cmd and vice versa. But what Karabiner does is, it "tricks" the OS to make it think that I have pressed alt, whenever I press cmd.

This isn't the behaviour I want. The behaviour I want is, whenever I press cmd<-, I want the system to think that I have indeed pressed cmd, but instead of moving the cursor to the beginning of the line, I want it to move it to the previous word.

It is possible to do this maybe messing with some system files and stuff?

Best Answer

You can change the text editing bindings for Cocoa apps with DefaultKeyBinding.dict. This will work for almost all applications where the ⌘ command+← left and ⌘ command+→ right worked before. (The only way it wouldn't work is if the application creator reimplemented those shortcuts from scratch, which is doubtful as it would be much easier to just use the versions that come free with a textfield.)

  1. If the directory ~/Library/KeyBindings/ does not exist, create it.

  2. Create the file DefaultKeyBinding.dict in that directory with contents like this:

    {
        "@\Uf702" = moveWordBackward:;
        "@\Uf703" = moveWordForward:;
        "~\Uf702" = moveToBeginningOfLine:;
        "~\Uf703" = moveToEndOfLine:;
    }
    

    The @ refers to the Command key, ~ is Option, \Uf702 is Left, and \Uf703 is Right. The selectors on the right-hand side are the same ones used in the default file, /System/Library/Frameworks/AppKit.framework/Resources/StandardKeyBinding.dict, but switched around to match what you said in your question.

  3. Restart an application for the changes to take effect in it.

Here's an in-depth guide to the Cocoa Text System that should help explain what's going on.