Sublime Forum

Automatically set view syntax according to first line


when i create a new file i just write the code am about to use so something like

<?php i wish if ST can automatically turn the view syntax to php,
html which will then turn the view syntax into html on its own,

so could this be done :slightly_smiling: ?



Yes, you could write a simple Python plugin to do it - see the API, in particular view.set_syntax_file



the issue that am not good with python in anyway or shape :frowning:



I had never used python before writing ST3 plugins, it is a really easy language to learn :slight_smile:
how about something like:

import sublime, sublime_plugin

class SetSyntax(sublime_plugin.EventListener):
	def on_modified_async(self, view):
		if view.match_selector(0, 'text.plain'):
			syntaxes = [('<?php', 'Packages/PHP/PHP.sublime-syntax')]
			for text, file in syntaxes:
				if view.size() == len(text):
					if view.substr(sublime.Region(0, len(text))) == text:

if you go to the Tools menu - Developer - New Plugin... and replace the contents of the template it generates with the above, and save it in the default folder, named as something like then it will convert the syntax to php when the entire content of the file is just <?php (provided the current syntax is set to Plain Text)

maybe it is inefficient because the plugin runs on every change to a document, maybe you would prefer to bind a command to the enter key instead…



The header of the syntax file allows you to specify a first_line_match key:

When a file is opened without a recognized extension, the first line of the file contents will be tested against this regex, to see if the syntax should be applied.

Source: Syntax



this will when u save the file, what am after is to change the file syntax on the fly ,dynamically to what u write , without waiting for the file to be saved.



this is for the saved files, not the unsaved ones which is what am after :slightly_smiling:



what makes you think that the file has to be saved for the code I provided to set the syntax? have you tried it?



I provide a manual tip tested on ST 3109 Win.

  1. select all (ctrl+a)
  2. cut (ctrl+x)
  3. paste (ctrl+v)
  4. done

This works for all syntaxes which define a first_line_match.



WOOOOOOOOT, thanx a million
at first it didn’t work and after some restarts & like 10 trials sublime start to pickup the plugin and everything works as expected, many many thanx for ur help, U R AWESOME :heart_eyes:

one last thing, is there a way to have like an if/else in that plugin to make it easier to include more definitions instead of creating one for each ?

maybe like an array for the [language => keywords] and the plugin simply iterate over that array and everything works :sunglasses:



that works but not always, also i was more into just make a new tab and simply start typing & sublime automagically understand what language i want to write :wink:,

i believe it makes coding more fluent and easier to get ur ideas out right away without going to the bottom corner everytime u wanna write something.



that works but not always

I am pretty curious about when it won’t work but your method works. I believe most syntax package developers would define first_line_match if it’s possible.

also i was more into just make a new tab and simply start typing & sublime automagically understand what language i want to write :wink:,

Yes. Before your post, actually, I prefer ctrl+shift+p -> ssphp rather than using mouse. I hardly use the method I post here lol.



lol, hopefully sublime would get more intelligent natively without the need for installing a bazillion plugin, and things start to work like a second nature without any wasted time into learning how the app works :video_game: .




I just posted an answer @ StackOverflow the other day that implements a dictionary.

You can implement a similar dictionary pattern with the code @kingkeith provided above.


@kingkeith, I just noticed that you used

syntaxes = [('<?php', 'Packages/PHP/PHP.sublime-syntax')]
for text, file in syntaxes:

Is [ ( "string_1" , "string_2" ) ] similar to using a dictionary?   I’ve never seen that pattern…

Can you use it like:
[ ( "string_1A" , "string_1B" ), ( "string_2A" , "string_2B" ) ] ?




I was in the same boat about a year ago when I first started working with Python & SublimeText.

I’ve managed to cover a good amount of ground since then, and have compiled a list of some helpful resources to get started with.

1 Like


many thanx, will definitely give that a go, thanx again :8ball:



How about

  1. read all existing syntax definition files (is this possible?)
  2. find out all possible first_line_matchs with their corresponding syntaxes
  3. build syntaxes = [('<?php', 'Packages/PHP/PHP.sublime-syntax'), ...] automatically (customization could be a plus)

1~2 are one-time jobs upon starting ST.

It would be a good plugin to be published and even better to be a ST built-in function…

1 Like


That’s definitely the way to go for a solid plugin, the dict method I mentioned above was me being lazy but still helpful :grin:

Should be pretty straightforward to implement:


		syntaxFiles = sublime.find_resources ( "*.sublime-syntax" )

		for file in syntaxFiles:
			print ( file )


Parse syntaxFiles for first_line_match


matchFound = False
loop @ Plugin.sublime-settings/CustomSyntaxDictionary

if matchFound still == False
loop @ syntaxFiles


fico said:

Is [ ( “string_1” , “string_2” ) ] similar to using a dictionary? I’ve never seen that pattern…

Can you use it like:
[ ( “string_1A” , “string_1B” ), ( “string_2A” , “string_2B” ) ] ?

In python, square brackets denote an array, and parenthesis denote a tuple - which is an immutable (unmodifiable) array. Yes, you can use it as per your example :slightly_smiling: I think I was too lazy to create a dictionary lol.

1 Like


great idea, I might make it a proper plugin and submit it on Package Control. I already did a similar task to loop through each keymap file the other day on SO.

1 Like


Oh, cool!   I didn’t know about tuples.   Usually just work with dictionaries & arrays.

That will def come in handy for coordinate pairs & such, up until now I’ve always just made parallel arrays & use for-range instead of for-each so that I can index them both simultaneously.   Tuples seem like they would help out with code readability.

Also, your key-binding log script is awesome. :+1: Just posted a question for you on SO.