Sublime Forum

Apply typical file extension syntax highlighting before dotfile syntax highlighting

#1

How could I make it so that “dotfiles” that match the file extensions of a common file format use the syntax highlighting for that filetype instead of the syntax highlighting normally used for dotfiles (Plain Text, by default)?

I have files in my project that are named like .css, .js, and .php which are differentiated by the folder they appear in. So, these aren’t really dotfiles in the usual configuration file sense; they just look like dotfiles. I would like the syntax highlighting to automatically recognize these as CSS, JS, and PHP, respectively. Currently, they are given Plain Text highlighting when opened.

0 Likes

#2

Potentially you can use View > Syntax > Open All with current extension as... and pick the syntax that associates with the file, if the case is that .js is always JavaScript, etc.

Since you mentioned that they’re differentiated by the folder that they’re in, for that you would need a package that alters the default behaviour. An example of that might be ApplySyntax.

1 Like

[SOLVED] File type alias for syntax highlighting
#3

Nice. Thanks for your help.
When I said they were differentiated by the folder they were in I just meant that I have a bunch of folders named whatever and they each contain .js, .php, etcetera. It was an organizational choice.

0 Likes

#4

You can write a simple plugin (put in User/ dir) to do that for all syntax too.

from pathlib import Path

import sublime
import sublime_plugin


class SetDotSyntaxListener(sublime_plugin.ViewEventListener):
    def on_load(self) -> None:
        if not (
            (path := Path(self.view.file_name() or "")).name.startswith(".")
            and (syntax := self.view.syntax())
            and syntax.name == "Plain Text"
        ):
            return

        if new_syntax := sublime.find_syntax_for_file(f"_{path.name}"):
            self.view.assign_syntax(new_syntax)

Also shameless promotion: https://packagecontrol.io/packages/AutoSetSyntax

0 Likes