9
votes

I can't for the life of me find how to make a keyboard shortcut to wrap a highlighted element in strong tags.

I know I can do Alt + Shift + w to wrap something in any html tag but you still have to type the tag. I want to be able to write my own shortcut for Ctrl + b for instance and that would wrap the element in tags

Any ideas?

3

3 Answers

32
votes

You can do this with a custom key mapping. Go to Preferences -> Key Bindings-User and add the following:

{ 
    "keys": ["ctrl+super+b"], 
    "command": "insert_snippet", 
    "args": {"contents": "<strong>${0:$SELECTION}</strong>"}
}

If the file is empty when you open it, put an opening square bracket [ on the first line, and a closing bracket ] on the last line -- the file needs to be valid JSON. I used CtrlSuperB as the key binding, because CtrlB is already bound to the build command. (Super is the Windows key or the Command key, depending on your OS and keyboard.)

To use the command, you can select what you want surrounded in <strong> tags, and hit CtrlSuperB. The selection will remain selected - if you want to remove the selection and have the cursor after the closing tag, change the "contents" to this:

"<strong>$SELECTION</strong>"

Finally, after triggering the key combo, you can keep the selection, then hit Tab to move to the end of the closing tag:

"<strong>${1:$SELECTION}</strong>$0"
0
votes

If by strong you mean bold, you can also use the plugin "LatexTools" for Sublime Text. Available via the Github site or using package control. Once installed correctly, it allows you to wrap highlighted text as below:

  • cmd-l, cmd-b for \textbf{blah}
  • cmd-l, cmd-u for \underline{blah}
  • cmd-l, cmd-t for \texttt{blah}

were cmd = command key on mac, or control key on windows. Hold down cmd for both keystrokes. This saves me a great deal of time.

0
votes

if you also want to use COMBINATION KEY SHORTCUTS you can add comma at the end of each line, in my case I'm using BOLD and UNDERLINE.

(I was having problem to add multiple key bindings on Sublime for bold and underlined)

Just as Matt said

If the file is empty when you open it, put an opening square bracket [ on the first line, and a closing bracket ] on the last line.

{ "keys": ["ctrl+super+a"], "command": "insert_snippet", "args": {"contents": "<b>${0:$SELECTION}</b>" } }, { "keys": ["ctrl+super+s"], "command": "insert_snippet", "args": {"contents": "<u>${0:$SELECTION}</u>" } }