Sublime Forum

Is there a hotkey to split a selection into two carets at the start & end?

#1

I’d love to have this for selecting words and phrases and pasting a <em> before them and a </em> after. Can have both on my clipboard but I don’t know how to perform the double-caret selection without my mouse

0 Likes

#2

Yes. Just use Edit -> Tag -> Wrap Selection With Tag. The keyboard shortcut is Alt + Shift + W

2 Likes

#3

Oh sweet. Is there a way to change the default tag for that or am I gonna have to retype the em each time?

0 Likes

#4

There are a few ways, but the easiest would be to add this binding to your user key bindings (Preferences > Key Bindings):

    { "keys": ["alt+shift+w"], "command": "insert_snippet",
      "args": {
          "contents": "<${1:em}>${0:$SELECTION}</${1/([^ ]+).*/$1/}>"
      }
    },

The em in ${1:em} is what sets the default tag here; you can change it to what you like or have multiple keys, etc.

1 Like

#5

i’m uncertain how this part works </${1/([^ ]+).*/$1/}>
i understand the regex but how is this corresponding to the content of the opening tag?

also i have in my default key bindings this
{ “keys”: [“alt+shift+w”], “command”: “insert_snippet”, “args”: { “name”: “Packages/XML/Snippets/xml-long-tag.sublime-snippet” } },
so i guess it works out of the box with the same pattern in xml-long-tag.sublime-snippet but p instead of em as default

0 Likes

#6

See the community docs on snippets for more detailed information, but in short ${1} specifies that this is snippet field 1, and the :em means that’s what the default value for the snippet it. Fields are mirrored so that anywhere the field ${1} appears, the text you type into the first one is replaced there. The extra regex bits do some textual substitutions on the text in the mirrored copy.

This snippet content is indeed exactly the snippet contents of the file that you’re mentioning (except that the default is p instead of em), but in this example the snippet content is inlined instead of being in a snippet file.

The default uses a snippet file because the command is also available from the menu.

Other ways to make the same change include making your own copy of the snippet file and expanding it from there or making an override on the snippet file you referenced above to make the same change so that the default key and menu will use the same default.

Generally speaking key bindings are more handy for these sorts of things than menu items, overrides can get you into trouble if you’re not careful, and creating snippet files for things you only ever want to trigger from a key binding makes for extra work when you want to change what they do (since you need to open the bindings file AND the snippet instead of just the one file).

0 Likes