ke20
ke20

Reputation: 665

How to define default syntax on files without extension on Sublime Text 2?

I've read many posts dealing with this problem, but none has an answer to my question.

As said in the title, I would like to define a default syntax for all files which have no extension. In my case I would like to use the Shell syntax.

I've tried "View/Syntax/Open all with current extension as..." but for all files, I have to make again the manipulation.

I've tried the package "applySyntax" but it not seem to work with this configuration:

{
    "name": "ShellScript/Shell-Unix-Generic",
    "rules": [
        {"file_name": "PRE_*$"}
    ]
}

All my files start with "PRE_[something]", someone know how to resolve this problem?

Thx!

Upvotes: 3

Views: 586

Answers (1)

dusan
dusan

Reputation: 9273

I've found a Gist with a plugin to set the syntax based on the file name, I've modified it a bit to match files starting with PRE_:

import sublime_plugin
import os

class DetectFileTypeCommand(sublime_plugin.EventListener):
  def on_load(self, view):
    filename = view.file_name()
    if not filename: # buffer has never been saved
      return

    name = os.path.basename(filename)
    if name.startswith("PRE_"):
      set_syntax(view, "Shell-Unix-Generic", "ShellScript")


def set_syntax(view, syntax, path=None):
  if path is None:
    path = syntax
  view.settings().set('syntax', 'Packages/'+ path + '/' + syntax + '.tmLanguage')
  print "Switched syntax to: " + syntax

You can go to Preferences->Browse Packages, and save it there ending with .py, I recommend creating a directory for it (e.g. DetectFileType/detect_file_type.py).

Upvotes: 1

Related Questions