Sublime Forum

Anyway to force Sublime to start up with a specific syntax highlighting selected?


I’m using Sublime to temporary files generated by tools like chef. Since the temporary files have no particular extension associated to them, Sublime simply opens the file with standard text syntax highlighting.

Is there a way I can specify to the sublime command line to open the file with a specific syntax highlighting?

Right now I’m opening the file as follows:

subl.exe -n -w  %1

Is there any way to do something like:

subl.exe -n -w  --syntax-color:json %1





If your loaded files all come from a certain directory, you can try this:

import sublime
import sublime_plugin

class OpenFileListener(sublime_plugin.EventListener):

    def on_load(self, view):

        if view.settings().get('syntax') == "Packages/Text/Plain text.tmLanguage":
            # escape backslashes if on Windows
            dir = "your_files_root_dir"
            if dir in view.file_name():
                your_syntax = 'Packages/Javascript/JSON.sublime-syntax'


you could also try

subl -n -w %1 --command "set_file_type {\"syntax\": \"Packages/JavaScript/JSON.sublime-syntax\"}"


Keith, do you know if there’s something similar to that which can do font settings?

I use Sublime, with User package synced, on computers with very different screen DPIs.

Currently, in my .bash_profile script, I additionally generate a .sublime-settings file (in Packages/Gen/). That settings file contains only font settings, which are appropriate given the current hostname.

If I could stop doing that and just create a bash alias for subl per-host that would be better.



as far as I know, there is no built in command to set the font size, but this might help:

but, as it is not built in, if ST is not already running, you may encounter this problem:



Thank you - The problem with writing “dynamic” values into User/Preferences.sublime-settings is that the file will then be “dirty” all the time as far as git is concerned. Which leads to forgetting to commit meaningful modifications.



Keith, I use the following code in my cmd.exe:

sublime_text.exe -n -w C:\test\readme --command "set_file_type {\"syntax\": \"Packages/JavaScript/JSON.sublime-syntax\"}"

But the language of the README file is still “Plain Text”, not JSON. What I’m doing wrong?

Edit: Keith answered me on Stack Overflow:



Is Sublime already running when you do it? As noted above, if Sublime’s not running, the --command doesn’t work because it delivers the command immediately but the plugin host needs time to start up and load everything before it’s ready to accept commands.

1 Like