Sublime Forum

Run Command According to Syntax

#1

is there away to make a certain command to run only on specific syntax ? basically i want to do is something like that

{ "keys": "super+s"], "args": { "commands": "save_all"] ], "scope": (source.html), "commands": "html_beautify"] ], "scope": (source.css, source.less), "commands": "css_comb"] ] }

0 Likes

#2

You can create multiple key bindings, and use context to specify when it should run. The following is from the Default key binding file

[code] // HTML, XML close tag
{ “keys”: “/”], “command”: “close_tag”, “args”: { “insert_slash”: true }, “context”:

		{ "key": "selector", "operator": "equal", "operand": "(text.html, text.xml) - string - comment", "match_all": true },
		{ "key": "preceding_text", "operator": "regex_match", "operand": ".*<$", "match_all": true },
		{ "key": "setting.auto_close_tags" }
	]
}[/code]

The context entry that would be of interest to you is the first one, where key is selector. You may need to play with the operand to get things just right, but you should be able to create the behavior you like. You will need to create 1 entry in your key binding file for each command/context pair.

0 Likes

#3

[quote=“skuroda”]You can create multiple key bindings, and use context to specify when it should run. The following is from the Default key binding file

[code] // HTML, XML close tag
{ “keys”: “/”], “command”: “close_tag”, “args”: { “insert_slash”: true }, “context”:

		{ "key": "selector", "operator": "equal", "operand": "(text.html, text.xml) - string - comment", "match_all": true },
		{ "key": "preceding_text", "operator": "regex_match", "operand": ".*<$", "match_all": true },
		{ "key": "setting.auto_close_tags" }
	]
}[/code]

The context entry that would be of interest to you is the first one, where key is selector. You may need to play with the operand to get things just right, but you should be able to create the behavior you like. You will need to create 1 entry in your key binding file for each command/context pair.[/quote]

thanx but sadly it doesn’t work because the key-bind can only be used with one command ,any other with the same key will simply be ignored ,like

[code]{ “keys”: “super+s”], “command”: “html_beautify”, “context”:

		{ "key": "selector", "operator": "equal", "operand": "(text.html)", "match_all": true },
	]

},
{ “keys”: “super+s”], “command”: “css_comb”, “context”:

		{ "key": "selector", "operator": "equal", "operand": "(source.less)", "match_all": true },
	]

},
{ “keys”: “super+s”], “command”: “save_all”, “context”:

		{ "key": "selector", "operator": "equal", "operand": "(text.html, source.less)", "match_all": true },
	]

}[/code]

will only execute the “save_all” ,removing it will then use “html_beautify” only and ignore the other ,btw could this be used with run_multi_commands plugin ? , am trying to get it working but i dont know how to add context : “window” + add the key : “selector”.

EDIT:
i found another way ,install plugin call “Hooks” and add the cmnd to the language setting file so this cmnd will now work in that syntax only ,i don’t know if this way will simply work on whatever plugin even it it have other scopes to work with but for now this fix my problem.

0 Likes