Search code examples
windowsmacrossublimetext3

Can't bind command to Spacebar in Sublime Text


I'm using this two bindings for auto complete:

{
"keys": ["tab"],
"command": "move",
"args": {"by": "lines", "forward": true},
"context":
    [
        { "key": "auto_complete_visible", "operator": "equal", "operand": true }
    ]
},
{
"keys": ["shift+tab"],
"command": "move",
"args": {"by": "lines", "forward": false},
"context":
    [
        { "key": "auto_complete_visible", "operator": "equal", "operand": true }
    ]
},

I would like to add "commit_completion" command to space key:

"keys": ["space"],
"command": "commit_completion",
"context":
    [
        { "key": "auto_complete_visible", "operator": "equal", "operand": true }
    ]
},

But it's not binding, when I press space it acts as normal space(it makes space xD). I'm able to bind it to any other key but not space. What am I missing?


Solution

  • There's not a key code for the space bar that you can bind like that; if you want to bind something to space, you need to use a literal space character:

    {
      "keys": [" "],
      "command": "commit_completion",
      "context": [
        { "key": "auto_complete_visible", "operator": "equal", "operand": true }
      ]
    },
    

    This is visible in the Sublime console by using sublime.log_input(True) to turn on input logging and then pressing the key; the only thing that gets logged is a character event, and not a key event.

    It's also important to note that you never want to bind anything to a character like this unless you're using a context to constrain when the binding applies, or you lose the ability to type that character.