Sublime Forum

Wrap selection with % how " behaves?


I use % a lot when writing batch scripts.

How can I wrap the selection like how the " behaves.

Like this:



To do that you need to add some customized key bindings. The bindings that do this for the internal autopairing (quotes, braces, etc) are visible in the left hand pane of the window that opens when you edit your key bindings. You can take items from there and customize them.

Something that would do what you want for wrapping the selection would be:

    { "keys": ["%"], "command": "insert_snippet", "args": {"contents": "%${0:$SELECTION}%"}, "context":
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }

That is, if you press the % key while the auto_match_enabled setting is turned on and the selection is not empty, use the insert_snippet command to replace the selection with a copy of itself wrapped in % characters.

If you like, you can also add the following line inside of the context array, which adds an extra condition that makes it apply only inside of batch files (assuming you’re using the syntax that ships with Sublime for your syntax highlighting):

            { "key": "selector", "operator": "equal", "operand": "source.dosbatch" },
1 Like


Thanks very much.

I think I’ll stick with that as always on as there are other cases where I’d need it.

I also changed the operand to true, like so (which might prove annoying in the future :crazy_face:)

{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }