Remap SHIFT + SPACE to underscore?

Solution 1:

If you don't want to use a third party tool, you will face a similar problem as the OP in this question (DefaultKeyBinding.dict: Syntax error near unexpected token) that only applications which use the Cocoa text system will support this.

Additionally it appears that the shift ⇧ key can't be properly used as modifier key if shift ⇧+some key doesn't have another representation than an unmodified some key - and shift ⇧+space doesn't have one.

You may either create a DefaultKeyBinding.dict or install Karabiner and configure a custom keycode.

DefaultKeyBinding.dict (only applications which use the Cocoa text system will support this)

Open Terminal and enter:

mkdir ~/Library/KeyBindings
touch ~/Library/KeyBindings/DefaultKeyBinding.dict 

Use nano ~/Library/KeyBindings/DefaultKeyBinding.dict to add (using another shortcut like cmd ⌘ctrl ^space here):

{
    "@^ " = ("insertText:", "_");
}

to output an underscore.

Karabiner (all apps should support this)

  1. Download, install and open Karabiner
  2. Open in the menulet Karabiner -> Preferences
  3. Open the tab Misc & Uninstall
  4. Hit the button Open private.xml
  5. Open the file private.xml with an appropriate editor
  6. Enter the customized keycode

    <?xml version="1.0"?>
    <root>
        <item>
            <name>Shift Space to Underscore</name>
            <appendix>(Change Shift-space to _)</appendix>
            <identifier>remap.shiftspace2underscore</identifier>
            <autogen>__KeyToKey__ KeyCode::SPACE, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_SHIFT, KeyCode::MINUS, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_SHIFT,</autogen>
        </item>
    </root>
    
  7. Save the file

  8. Open in the menulet Karabiner -> Preferences the tab Change Key
  9. Hit the ReloadXML button
  10. Enable the remapping. It should be listed at the beginning.

Solution 2:

The Karabiner code in the 2015 response is now outdated, as Karabiner now uses JSON instead of XML. You can add a custom complex modification by creating a file called ~/.config/karabiner/assets/complex_modifications/my_modification.json and use the following JSON code. Then open the karabinder-Elements app, navigate to "Complex Modifications", and enable the rules you have just added.

    {
        "title": "Underscore mapping",
        "rules": [
            {
                "manipulators": [
                    {
                        "description": "Change left_shift+space to underscore (_).",
                        "from": {
                            "key_code": "spacebar",
                            "modifiers": {
                                "mandatory" : [
                                    "left_shift"
                                ]
                            }
                        },
                        "to": [
                            {
                                "key_code": "hyphen",
                                "modifiers": [
                                    "left_shift"
                                ]
                            }
                        ],
                        "type": "basic"
                    }
                ]
            },
            {
                "manipulators": [
                    {
                        "description": "Change right_shift+space to underscore (_).",
                        "from": {
                            "key_code": "spacebar",
                            "modifiers": {
                                "mandatory" : [
                                    "right_shift"
                                ]
                            }
                        },
                        "to": [
                            {
                                "key_code": "hyphen",
                                "modifiers": [
                                    "left_shift"
                                ]
                            }
                        ],
                        "type": "basic"
                    }
                ]
            }
        ]
    }