Search code examples
sublimetext2key-bindings

Bind command show_at_center to space bar in Sublime Text 2


In vim, in command mode, I'm quite fond of using the space bar to center the current line on screen. nmap <space> zz maps this functionality to the space bar in my gvimrc file. I'm trying to bind the same functionality to the space bar in Sublime Text 2:

{ "keys": ["space"], "command": "show_at_center", "args": {"command_mode": true} }

The line above doesn't work. Can anyone tell me why, and what will? I have Sublime's Vintage mode enabled. Also, I have “scroll_past_end”: true in my Sublime Text 2 User Preference Settings so that even the last lines of a file can be centered.


Solution

  • Try adding the following to Packages/User/Vintage/Default (<your OS>).sublime-keymap:

    { "keys": ["space"], "command": "show_at_center",
        "context":
        [
            { "key": "setting.command_mode"}
        ]
    }
    

    I can't test it right now, but it should work :)

    EDIT - it doesn't. See below...


    UPDATE

    So I've searched far and wide, and it seems that, for whatever reason, you cannot assign an action to space on its own. Assigning it to Altspace works just fine:

    { "keys": ["alt+space"], "command" : "center_on_cursor", 
        "context": [{"key": "setting.command_mode"}] }
    

    as does assigning it to a function key (F7, for example), or even to a single letter, like M. However, space on its own only results in the cursor moving one character forward when in command mode. The good news is that hitting z,z (lowercase) whilst in command mode will do what you originally wanted to do, which is center the cursor (as does CtrlL, as you indicated). Unfortunately, it is not possible to remap this action onto the spacebar on its own. The programmed action of space in command mode,

    set_motion {"clip_to_line": true, "motion": "vi_move_by_characters", "motion_args": {"extend": true, "forward": true, "visual": false}} 
    

    just seems to be hard-programmed in. Sorry.