When I type (
and hit Space, I want Sublime Text to automatically add a space and move the caret so the end results is ( ^ )
, where ^
is the cursor.
How can I adjust the key bindings to allow for this?
I've been able to adjust the key bindings using
{ "keys": ["("], "command": "insert_snippet", "args": {"contents": "( $0 )"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true }
]
}
but this will always format it as ( ^ )
when typing (
. I'd rather have it format after I hit space
.
I can see in the default bindings a case when hitting enter
after typing {
which will run a macro so I thought I could use it as a template, but I can't find that macro on my filesystem to see how it works.
{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
]
},
You have the right idea here with your initial binding, but what you want is to bind it to pressing space instead of pressing the open parenthesis; that would require you to first manually open the paired parenthesis followed by pressing space to insert the snippet that you want:
{ "keys": [" "], "command": "insert_snippet", "args": {"contents": " $0 "},
"context": [
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\($", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
]
},
Now pressing space will insert two spaces with the cursor between them, but only when the cursor is directly between two parenthesis.
Note however that this binding will stop the binding on Backspace that exists by default to remove both parenthesis when you backspace because that binding requires that the cursor be surrounded by parenthesis just like this one does, but once this binding triggers that will no longer be the case.
So you could also include this binding as well, which detects when the cursor is in that situation and runs the same macro as the default binding does; that would allow you to press Space to insert the spaces, then decide you don't want them and hit Backspace to remove them.
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\( $", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^ \\)", "match_all": true }
]
},
I can see in the default bindings a case when hitting enter after typing { which will run a macro so I thought I could use it as a template, but I can't find that macro on my filesystem to see how it works.
Packages that ship with Sublime are stored in sublime-package
files (zip files with a changed extension). The easiest way to look inside of them from core sublime is to use View Package File
from the command palette, which shows you every file in every package that is currently loaded. You can then enter filter text to narrow in on the file you want to see and select it to open the file.
Note that the files that are opened this way are read only (because they're sourced from the package file) as a reminder that editing them has no effect.