Search code examples
sublimetext3shortcut

Sublime Text 3 keyboard shortcut for bold


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?


Solution

  • 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"