Skip to content

Instantly share code, notes, and snippets.

@beechnut
Last active December 6, 2021 06:11
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save beechnut/69a3ab6fd66f03e8e5ea to your computer and use it in GitHub Desktop.
Save beechnut/69a3ab6fd66f03e8e5ea to your computer and use it in GitHub Desktop.
Surround with backticks in Sublime Text 3
// Copy the following into Preferences > Key Bindings -- User
// Thanks to:
// http://sublimetext.userecho.com/topic/86166-backtick-quoting-selected-text-does-not-work-like-single-quotes-and-double-quotes/
[
// Auto-pair backticks
{ "keys": ["`"], "command": "insert_snippet", "args": {"contents": "`$0`"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "[`a-zA-Z0-9_]$", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single", "match_all": true }
]
},
{ "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 }
]
},
{ "keys": ["`"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^`", "match_all": true }
]
},
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "`$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^`", "match_all": true }
]
},
]
@automaticgiant
Copy link

still needs support for multiple selections. other than that, fantastic!

@sagold
Copy link

sagold commented Jan 14, 2017

Thanks. Worked for me for other shortkeys than ` (backtick), since the initial backtick key is waiting for another character, i.e. e to print è

@marcusbesjes
Copy link

nice works great here!

@njohnson7
Copy link

Thanks, that's really convenient!

@joelmellon
Copy link

@andrewrech
Copy link

This is wonderful, thank you

@YannDubs
Copy link

YannDubs commented Feb 3, 2018

@sagold If you have the backticks issue because of an international keyboard you can do this (on mac) : https://www.foobarflies.io/backticks-on-the-mac-keyboard/

@VinceSJ
Copy link

VinceSJ commented Feb 13, 2018

Thanks! Working great here for multi-selection and multi-cursor.

@coderCK2
Copy link

coderCK2 commented Jan 3, 2019

Thank you - working for multi-selection and multi-cursor for me as well!

@dakuzen
Copy link

dakuzen commented Aug 14, 2019

Thanks for sharing this!

@molleweide
Copy link

works!!!! Thx!!!!!

@ruolis
Copy link

ruolis commented Sep 15, 2020

@sagold If you have the backticks issue because of an international keyboard you can do this (on mac) : https://www.foobarflies.io/backticks-on-the-mac-keyboard/

very thanks 💯

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