Sublime Forum

Set buffer syntax from a macro

#1

Is it possible to set the syntax of the current (possibly unsaved) buffer from within a macro? I’ve looked at the existing macros in my Packages folder but I couldn’t find a command to do this. I looked for something like

{
	"command": "set_syntax",
	"args":	{"syntax_name": "XML"}
}

but I could not find anything.

I need to be able to:

  1. Ctrl+N to open a new buffer
  2. Paste some text
  3. Hit a shortcut that sets a predefined syntax and runs a macro on the contents
0 Likes

#2

If you view the console and enter the command

sublime.log_commands(True)

You’ll see all the commands that ST2 is running. Doing that and then using the syntax selector at the bottom right of the window, I saw this command being run when I set a syntax:

set_file_type {"syntax": "Packages/ShellScript/Shell-Unix-Generic.tmLanguage"}

Obviously you’d need to replace the syntax file with whatever is appropriate for your situation. Let us know how this works out for you!

0 Likes

#3

It worked! Thank you very much, that was exactly the step I was missing.

Following your hints, I added the following into my macro and now it works exactly as I need it.

{
	"command": "set_file_type",
	"args": {"syntax": "Packages/XML/XML.tmLanguage"}
}

Initially I tried the following thinking it would be more abstract (and thus safer between different OSes), but it doesn’t work reliably

{
	"command": "set_file_type",
	"args": {"file": "${packages}/XML/XML.tmLanguage"}
}

Thanks again.

0 Likes