Sublime Forum

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

#1

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

Thanks,

Eric

0 Likes

#2

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'
                view.set_syntax_file(your_syntax)
0 Likes

#3

you could also try

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

#4

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.

0 Likes

#5

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:

2 Likes

#6

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.

0 Likes

#7

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: https://stackoverflow.com/questions/60950790

0 Likes

#8

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