Sublime Forum

How to expand {% for Django tags

#1

I’m currently using Sublime Text 4 as my primary text editor for writing Django apps and I would like to add support for Django tags. Currently I can expand double curly braces({{ expands to }}), but not curly and percentage brackets ({% %}. Is there a way I can configure Sublime Text to do this?

0 Likes

#2

Try adding the code below to your key bindings. Just make sure you change every instance of source.ahk to the proper scope name. The scope name can be found by pressing ctrl + shift + alt + p in Windows.

{ "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": "setting.command_mode", "operator": "equal", "operand": false },
		{ "key": "selector", "operator": "equal", "operand": "source.ahk" },
	] },
{ "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 },
		{ "key": "setting.command_mode", "operator": "equal", "operand": false },
		{ "key": "selector", "operator": "equal", "operand": "source.ahk" },
	] },
{ "keys": ["%"], "command": "insert_snippet", "args": {"contents": "%${1:$SELECTION}%$0"}, "context": [
		{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
		{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
		{ "key": "setting.command_mode", "operator": "equal", "operand": false },
		{ "key": "selector", "operator": "equal", "operand": "source.ahk" },
	] },
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "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 },
		{ "key": "selector", "operator": "equal", "operand": "source.ahk" },
	] },
0 Likes

#3

Worked perfectly thanks!

0 Likes