Sublime Forum

How to set default syntax on new window/tab?

#1

Hi all,
I’d like it so that eg. JavaScript is the default syntax when opening a new tab/window in SublimeText 2.

Does anyone know how to do this? Thanks.

0 Likes

DetectSyntax updated with new_file_syntax
Very basic syntax help
#2

I very well might be wrong about this, but I don’t think you can do this without a plugin. But doing it with a plugin should be pretty straightforward.

Create a file in your Packages/User directory called ‘default_syntax.py’ or something, and add this:

[code]import sublime, sublime_plugin

class DefaultSyntaxCommand(sublime_plugin.EventListener):
def on_new(self, view):
view.set_syntax_file(‘JavaScript/JavaScript.tmLanguage’)
[/code]

I just hacked that together really fast and only checked it once, so it may not be perfect, but it’s a place to start.

Hmm. This might be an interesting feature to add to DetectSyntax. I’ll have to give this some consideration.

0 Likes

#3

Thanks Phil, appreciate that - will give it a try now.

0 Likes

#4

That seemed fine for a few days but now errors on starting Sublime Text - does the following make sense to you Phil?

“Error loading syntax file “JavaScript/JavaScript.tmLanguage”: Error parsing plist xml: Failed to open file In file “JavaScript/JavaScript.tmLanguage””

Thanks again

0 Likes

#5

I had left out part of the path to the language file. Replace

view.set_syntax_file('JavaScript/JavaScript.tmLanguage')

with

view.set_syntax_file('Packages/JavaScript/JavaScript.tmLanguage')

and see if that takes care of the problem. I noticed that only on restart (or the first new file after launch) did the error pop up, so I’m assuming that at the time the new file is created, the directory structure hasn’t been parsed or something. I tried a few times with the fuller path and it seemed to be okay.

0 Likes

#6

I just pushed an update to DetectSyntax that incorporates this functionality. All you have to do is add

"new_file_syntax": "JavaScript"

to Packages/User/DetectSyntax.sublime-settings. Obviously replace “JavaScript” with the appropriate syntax name for your particular situation.

0 Likes

#7

Fantastic Phil, I owe you a beer :smile:

0 Likes

#8

Ah, is that released yet? I’m on “Sublime Text 2 Beta, Build 2207” and don’t have that file - I created one too but nothing happens, guessing it’s not released?

Thanks again

0 Likes

#9

You have to create the user file yourself. Also you have to have DetectSyntax installed.

0 Likes

#10

To add to what quarnster said, after you install DetectSyntax via Package Control, take a look at Packages/DetectSyntax/DetectSyntax.sublime-settings. It is commented pretty well, I think, so you should be able to understand what everything does. To customize, create your own copy of DetectSyntax.sublime-settings in Packages/User and populate it with the rules you want [1]. Obviously you don’t need to include any of the default rules in your own settings file. The only difference is in your settings file, the array of rules should be labeled “syntaxes”, NOT “default_syntaxes”.

If you have any problems, I’m happy to help. Just let me know.

[1] If you prefer, you can just copy the default one and change it to your match your needs.

0 Likes

#11

ahh, thanks guys - I was under the impression DetectSyntax was some kind of internal. Got that now, cheers!

0 Likes