Sublime Forum

Set file type automatically

#1

In VIM I have the following configuration

au BufRead,BufNewFile /some/path/somefile set ft=sh

It means when I open /some/path/somefile, it automatically set the file type to SH

What’s the equivalent in sublime?

0 Likes

#2

The Sublime way of doing something like this would be to use an event listener in a plugin that watches for files in a particular location and assigns a different syntax to them than the one that would normally be applied based on other rules (extension, etc).

Such a plugin would look something like the following:

import sublime
import sublime_plugin


class FileTypeListener(sublime_plugin.EventListener):
    def on_load_async(self, view):
        if view.file_name().startswith("/home/tmartin/local/bin/"):
            view.assign_syntax("Packages/ShellScript/Bash.sublime-syntax")

This assigns the Bash syntax to any file opened from my ~/local/bin/ folder, regardless of the type of file or what syntax would otherwise be used. You can extend this as needed for multiple paths with extra if statements, etc.

To use this, you would select Tools > Developer > New Plugin... and replace the stub code with this and then save the file in the default location with a memorable name. If you’re not sure what syntax you want to apply, you can open the Sublime console with Ctrl+` while you have a file focused and enter view.settings().get("syntax") to determine what syntax it’s using.

There is probably a package for this on PackageControl that does this in a more configurable way but I’m not familiar with it off the top of my head if so (I tend to just scratch my own itch when it comes to Sublime); I did a bit of searching but didn’t find anything appropriate.

The Project Specific Syntax Settings package does something similar to this on a project-by-project basis, assuming you were doing something like this to change the types of files in certain projects.

1 Like

#3

Package Default File Type may do what you want.

0 Likes

#4

Awesome :slight_smile:

Works great for me.

0 Likes

#5

To get a similar effect without hardcoding paths, you could install this:

…and put a commented line such as this within the first 5 lines of the file:

# -*- mode: bash -*-

Obviously, all these kinds of things are a last resort reserved for when Sublime’s normal approach (based on file-extensions and shebang-detection) isn’t giving the result you want.

0 Likes

#6

Problem with this is that there’s no way to tell Sublime to index un-opened files in the project according to a custom syntax… Is it? So basically it precludes symbol navigation.

0 Likes

#7

Yeah as far as I’m aware indexing only happens based on the syntax that Sublime would choose on it’s own, so this isn’t a solution if you want to use something like symbol navigation to open the file. I can’t think of a way to achieve that with any plugin without resorting to using an external indexer of some kind like ctags.

I don’t think that’s something that vim would do either, though; or at least as far as I’m aware you need to use something like ctags to navigate symbols that way.

0 Likes