ChatGPT解决这个技术问题 Extra ChatGPT

How to customise file type to syntax associations in Sublime Text?

I'd like Sublime 2 editor to treat *.sbt files (to highlight syntax) as Scala language, same as *.scala, but I can't find where to set this up. Do you happen to know?


A
Air

In Sublime Text (confirmed in both v2.x and v3.x) there is a menu command:

View -> Syntax -> Open all with current extension as ...


do you have any idea how this is reflected in the sublime text config files? I'm trying to achieve this with an automated chef recipe and I can't figure out what to set in the settings JSON.
@brad Open syntax specific settings (Scala.sublime-settings in Users folder), and add to them: { "extensions": ["scala", "sbt"]}
Yet another command misteriously absent from the command palette (ctrl/cmd-shift-P)
Also doesn't work as expected for filenames with double extension. Editing the *.sublime-settings does work.
How do you set a default syntax for files opened that have no extension?
I
Ivan

I've found the answer (by further examining the Sublime 2 config files structure):

I was to open

~/.config/sublime-text-2/Packages/Scala/Scala.tmLanguage

And edit it to add sbt (the extension of files I want to be opened as Scala code files) to the array after the fileTypes key:

<dict>
  <key>bundleUUID</key>
  <string>452017E8-0065-49EF-AB9D-7849B27D9367</string>
  <key>fileTypes</key>
  <array>
    <string>scala</string>
    <string>sbt</string>
  <array>
  ...

PS: May there be a better way, something like a right place to put my customizations (insted of modifying packages themselves), I'd still like to know.


it's now in /Library/Application Support/Sublime Text 2/Packages/Scala/Scala.tmLanguage
I think is the user library rather than the system library: ~/Library/Application Support/Sublime Text 2/Packages/Scala/Scala.tmLanguage
@Eric, you should read more careful.. It opens all files with that specific extension with the specified syntax. Most of the time, this is what you want.
s
squeegee

I put my customized changes in the User package:

*nix: ~/.config/sublime-text-2/Packages/User/Scala.tmLanguage
*Windows: %APPDATA%\Sublime Text 2\Packages\User\Scala.tmLanguage

Which also means it's in JSON format:

{
  "extensions":
  [
    "sbt"
  ]
}

This is the same place the

View -> Syntax -> Open all with current extension as ...

menu item adds it (creating the file if it doesn't exist).


Note that this is the only method (at least in ST2) that allows to set specific syntax for files with double extensions (for example, whatever.twig.html), as the menu method only takes the last one!
I found this works with Sublime Text 3. I used View -> Syntax -> Open all with current extension as ... to create the language file Markdown.sublime-settings in ~/Library/Application Support/Sublime Text 3/Packages/User/, and then edited this file to add extra file extensions.
G
General Grievance

For ST3

$language = "language u wish"

If exists, open ~/.config/sublime-text-3/Packages/User/*$language*.sublime-settings

else just create it.

And set

{
    "extensions":
    [
        "*yourextension*"
    ]
}

This way allows you to enable syntax for composite extensions (e.g. sql.mustache, js.php, etc ... )


So to associate .js.php files with JavaScript, I should create a file called Javascript.sublime-settings filled with { "extensions": [ "js.php" ] } ?
Yes if the file not exists, if already exists just add the extension you desire on "extensions" array.
ST now has a menu item for this: Settings > Settings -- Syntax Specific.
K
Kaushik Gopal

There's an excellent plugin called ApplySyntax (previously DetectSyntax) that provides certain other niceties for file-syntax matching. allows regex expressions etc.


U
Ulf Gjerdingen

There is a quick method to set the syntax: Ctrl+Shift+P,then type in the input box

ss + (which type you want set)

eg: ss html +Enter

and ss means "set syntax"

it is really quicker than check in the menu's checkbox.