80

How to disable the annoying Option-Space key combination for non-breaking spaces?

This is very very annoying in Eclipse or the terminal as I often type the non-breaking space instead of a regular one.

2
  • 3
    FWIW, It's referred to as the option key rather than the alt key on a Mac.
    – Chealion
    Dec 4, 2009 at 7:04
  • Is it possible to make the Terminal display nbsp as a reverse character or some sort, so you would instantly see it when you mistype? Because some times you actually do want to type a nbsp.
    – forthrin
    Mar 24, 2018 at 9:12

11 Answers 11

73

Make your ~/Library/KeyBindings/DefaultKeyBinding.dict file look like this (if it doesn’t exist create it, if there are already bindings in it just add the one from below):

{
"~ " = ("insertText:", " ");
}

What does this do? Every time you press Option-Space instead of inserting a non breaking space OS X now inserts a regular space. Problem fixed.

You need restart the device to apply this change.

11
  • I couldn't get this work on OSX 10.7 :( Sep 17, 2012 at 8:43
  • 4
    @EemeliKantola It still works for me, but you have to reopen applications to apply the changes. Terminal, Xcode, and many cross-platform applications don't support DefaultKeyBinding.dict.
    – Lri
    Sep 25, 2012 at 12:23
  • 1
    @LauriRanta Terminal seems to support the DefaultKeyBinding.dict file.
    – Ragnar123
    Jan 24, 2014 at 15:36
  • This works on Yosemite 10.10.2. I've created the directory and the file from scratch. Thanks a bunch! Apr 6, 2015 at 2:11
  • 1
    Also worked for me on El Capitan, OS X 10.11. Thanks!
    – n2o
    Oct 13, 2015 at 14:08
17

I use iTerm2 for most of my work and the mapping can be added in the "Keys" preference pane, by adding a new key combination in Preferences -> Keys -> the plus button. Note when adding the key make sure to put a single space in the lower box as shown.

screencap of adding the keyboard shortcut

2
  • Very good solution! Heck, I'm still getting valuable information from a four year old post. :-)
    – Malax
    Nov 26, 2013 at 14:12
  • 1
    You make my day!
    – Mmm
    Mar 17, 2014 at 13:23
16

As mentioned in the comments, the solution provided by Martin does not work in some applications, most importantly (for me) in my editor, Sublime Text 2.

So for all of you who are troubled by none breaking spaces in your code, you can add the following custom keybinding into your user keymap ("Sublime Text 2 > Preferences > Key Bindings - User"):

{ "keys": ["alt+space"], "command": "insert_snippet", "args": {"contents": " " } }

This will insert a regular space instead of a non-breaking one.

7

I also could not get the KeyBindings fix to work. Similar to the Sublime answer, I ended up configuring my editor (VS Code) to insert a normal space by adding

{ "key": "alt+space", "command": "type", 
                      "when": "editorTextFocus",
                      "args": { "text": " " } }

to my global keybindings.json (Preferences: Open Keyboard Shortcuts File in the command palette).

6

You could also create a custom keyboard layout with Ukelele (see https://superuser.com/a/515151) or use KeyRemap4MacBook:

https://github.com/tekezo/KeyRemap4MacBook/blob/master/src/core/server/Resources/include/checkbox/standards/space.xml

<item>
  <name>Non-Breaking Space to Normal Space</name>
  <appendix>(Option+Space to Space)</appendix>
  <appendix>(Option+Shift+Space to Space)</appendix>
  <identifier>remap.option_space_to_space</identifier>
  <autogen>__KeyToKey__ KeyCode::SPACE, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_OPTION | ModifierFlag::NONE, KeyCode::SPACE</autogen>
  <autogen>
    __KeyToKey__
    KeyCode::SPACE, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_OPTION | MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_SHIFT | ModifierFlag::NONE,
    KeyCode::SPACE,
  </autogen>
</item>
6

Using OSX Sierra (10.12.6), and resolved this very easy by using Karabiner-Elements:

https://github.com/tekezo/Karabiner-Elements

Once opened, go to "Complex Modifications" > "Add Rule" > "Import more rules from the Internet", and download the "Disable alt+spacebar (nonbreaking space)" rule. Add it, and you're done!

1
  • Year 2023, Macos 13 Ventura, only this option working. Don't forget to click to Enable button after download ;) Jul 13, 2023 at 13:58
4

A possible global solution is to set alt-space to something else like Show Notification Center in System Preferences > Keyboard > Shortcuts > Mission Control.

enter image description here

1
  • To improve this answer: Set any Shortcut in this Menu to alt-space and then disable the Shortcut by unchecking the Checkbox in this row. This disables the Nonbreaking Space behaviour AND frees up this key combination to be used by any other tool like BetterTouchTool or Alfred! This is actually the simplest solution using built in "System Preferences" and still works with Big Sur 11.3
    – Lukas Zech
    May 27, 2021 at 11:54
3

Since this is the answer that Google shows you when searching for "How to disable non-breaking spaces in Sublime Text" (see x3ro's answer) I thought I'd post the answer for Sublime Text on Linux. Add this to your key bindings:

{ "keys": [" "], "command": "insert_snippet", "args": { "contents": " " } }

Go to "Preferences" → "Key bindings - User" to edit your key bindings.

Note that the space in "keys": [" "] is a non-breaking space.

Also note that this key binding also affects widgets, like the search box. In other words, you can no longer type non-breaking spaces into your search box, which could be a problem. To disable non-breaking spaces just in the text editor, use this key binding instead:

{
    "keys": [" "],
    "command": "insert_snippet", "args": { "contents": " " },
    "context": [
        { "key": "setting.is_widget", "operand": false }
    ]
}
2

For me the the KeyBindings fix didn't work. I use Coda2 as an editor. I managed to get the replacement to work with BetterTouchTool though.

BetterTouchTool: "Keyboard > Add New Shortcut" then type "alt + space" in "Keyboard Shortcut" and "Space" in "Trigger other Keyboard Shortcut"

1
  • Thanks!, bettertouchtool has a solution to all problems :p
    – Steven B.
    Apr 16, 2018 at 10:23
0

On Sublime Text 4113 on Mac OS Big Sur, this answer doesn't work.

Here is the correct keymap:

{ "keys": ["alt+space"], "command": "insert", "args": {"characters": " "} },

0

My DefaultKeyBinding.dict contains following:

{
  "~ " = ("insertText:", " ");
  "~\U0022"  = ("insertText:", "\U0022");
}

where last line replaces ” with " which in my case was another often happening mis-spelling.. It simply inserts " when option was down when writing double quotes.

You must log in to answer this question.

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