Sublime Forum

How to set Default Syntax Permanently

#1

How the hell do I set the syntax to always be JavaScript when I open Sublime? I’ve searched for this, tried various things an apparently this is very painful unless I’m just missing something that I don’t know yet. I hope this is very easy to to do.

0 Likes

#2

By default you have the syntax currently being used in the bottom right corner of the status bar. When you click on it a context menu shows up with all of your syntax definitions, but on top of that list there is an entry Open all with current extension as ... - hover over that and pick the syntax you want.

0 Likes

#3

Thanks. But for some reason JavaScript is greyed out when I try that…

0 Likes

#4

I’m seeing some of the syntax definitions grayed out as well when I open new file tab, but when I open an existing file I have access to all syntax definitions.

Not sure why that is, but I’m almost certain that was not the case in ST3.

0 Likes

#5

Can be done via a simple custom plugin.

import sublime
import sublime_plugin


def plugin_loaded() -> None:
    view = sublime.active_window().active_view()
    if view:
        assign_default_syntax(view)


def assign_default_syntax(view: sublime.View) -> None:
    view_settings = view.settings()
    default_syntax = view_settings.get("default_syntax", "")

    if default_syntax and "/Plain text." in view_settings.get("syntax", ""):
        view.assign_syntax(default_syntax)


class DefaultSyntaxAssigner(sublime_plugin.EventListener):
    def on_new(self, view: sublime.View) -> None:
        assign_default_syntax(view)

And add the following in your user preferences (i.e., Preferences.sublime-settings):

    "default_syntax": "scope:source.js",

Or, there are some plugins provide that behavior.

0 Likes