Skip to content

Instantly share code, notes, and snippets.

@darrenjansson
Last active July 16, 2019 07:45
Show Gist options
  • Select an option

  • Save darrenjansson/504352ed3fb266965928 to your computer and use it in GitHub Desktop.

Select an option

Save darrenjansson/504352ed3fb266965928 to your computer and use it in GitHub Desktop.
Simple example of a key-mapped Sublime Text snippet (⌘+b for bold)
<snippet>
<content><![CDATA[
<strong>${TM_SELECTED_TEXT}</strong>
]]></content>
<scope>source.html</scope>
</snippet>
[
{ "keys": ["super+b"], "command": "insert_snippet", "args": {"name": "Packages/User/bold.sublime-snippet"} },
// Set context so shortcut works in HTML files only
{ "keys": ["super+b"], "command": "insert_snippet", "args": {"name": "Packages/User/bold.sublime-snippet"},"context":[{ "key": "selector", "operator": "equal", "operand": "text.html" }] }
]
@darrenjansson

Copy link
Copy Markdown
Author

Add the snippet to your Packages/User folder and then go to 'Preferences' > 'Key Bindings – User' to add a keyboard shortcut.

@darrenjansson

Copy link
Copy Markdown
Author

You can set the context for the keyboard shortcut, so it only works in certain scopes. See the example above for a simple implementation. The beauty of the scope context is that you can limit it from being HTML right down to a paragraph within a section element for example.

You'll want to use something like ScopeHunter to find the scope.

Check the key bindings docs for the operator and operand settings. With those regex options you can do anything!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment