This is possible, but it requires a bit more setup work. There’s no shortcut for having a snippet wrap the current line without having the text selected, so you need a second key binding that knows how to carry out additional actions first.
First, you need to create a sublime-macro
file that does the work of selecting the current line for you followed by inserting your snippet. One way to do that is via the macro recorder that’s built in, but for expediency you can also save the following text into a file in your User
package:
[
{
"command": "expand_selection",
"args": {"to": "line"}
},
{
"command": "move",
"args": {"by": "characters", "extend": true, "forward": false}
},
{
"command": "insert_snippet",
"args": {"contents": "<h1>${0:$SELECTION}</h1>"}
}
]
The User
package can be found by using Preferences: Browse Packages
from the command palette or main menu. You want to save the file in the User
folder you find there as a sublime-macro
file. The name you give it is not important, but you need to remember it. Here I’ve named my file wrap_line_in_h1.sublime-macro
.
This macro will expand the selection on all lines with a cursor out to be the full line, and then it “backs” the selection up one because by default this command will literally select the whole line, which includes the newline that ends the line; I’m guessing you don’t want that.
Once that’s done, it executes insert_snippet
as in your binding, but here it knows explicitly that it needs to wrap the selection because it just selected something.
To use this, you need the following two key bindings:
{ "keys": ["alt+shift+h", "alt+shift+1"], "command": "insert_snippet",
"args": {
"contents": "<h1>${0:$SELECTION}</h1>"
}
},
{ "keys": ["alt+shift+h", "alt+shift+1"], "command": "run_macro_file",
"args": {
"file": "res://Packages/User/wrap_line_in_h1.sublime-macro"
},
"context": [
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
],
},
Both of these bindings have the same keys set, but the second one has a context
on it that makes it apply only when the selection is currently empty (i.e. there is nothing selected); the first one is just the binding you outlined in your question.
The one with the context
executes the macro you created above, so change the file
argument if you named the file something different than I did here.
It’s important that the bindings be in your file in this particular order; Sublime effectively looks “bottom-up” for bindings that match a key and uses the first one it finds that applies to the current situation, so the one with the context
needs to come second in the file so that Sublime will use it when the selection is not empty.
For more details on what exactly is going on here (should you be interested in that), I have a video covering macros in Sublime Text as well as how contexts in key bindings work.