3

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!

1
  • The regex you specified for a rule does not match what you want to do. Try PRE_.*$. Note the .. What you specified maps to PRE, PRE_, PRE__, etc. Basically adding more underscores. Commented May 14, 2013 at 22:33

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.