580

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?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Ivan
  • 63,011
  • 101
  • 250
  • 382
  • 6
    It's a duplicate of: http://stackoverflow.com/q/7574502/1346682 – Nicolas Malbran Oct 14 '14 at 13:19
  • Possible duplicate of [Set default syntax to different filetype in Sublime Text 2](https://stackoverflow.com/questions/7574502/set-default-syntax-to-different-filetype-in-sublime-text-2) – Sean the Bean Aug 22 '17 at 18:36

7 Answers7

1305

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

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

Air
  • 5,084
  • 5
  • 25
  • 19
TedG
  • 13,068
  • 1
  • 13
  • 6
  • 25
    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 Nov 09 '13 at 23:18
  • 14
    @brad Open syntax specific settings (Scala.sublime-settings in Users folder), and add to them: { "extensions": ["scala", "sbt"]} – omittones Sep 10 '14 at 09:46
  • 1
    Yet another command misteriously absent from the command palette (ctrl/cmd-shift-P) – Tobia Dec 18 '14 at 15:49
  • 5
    Also doesn't work as expected for filenames with double extension. Editing the `*.sublime-settings` *does* work. – MM. Dec 24 '14 at 12:19
  • 11
    How do you set a default syntax for files opened that have no extension? – cavalcade Feb 27 '15 at 23:50
  • Finally! How annoying I couldnt find this until now, had been fiddling around with an extension as suggested in another answer. – nights Nov 26 '16 at 05:43
  • 1
    And what do you do when this whole menu is greyed out / lists everything but is entirely unselectable? – frumbert Dec 14 '16 at 05:24
42

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.

Ivan
  • 63,011
  • 101
  • 250
  • 382
  • 2
    it's now in ```/Library/Application Support/Sublime Text 2/Packages/Scala/Scala.tmLanguage``` – Guillaume Massé Aug 01 '12 at 04:28
  • 2
    I think is the user library rather than the system library: `~/Library/Application Support/Sublime Text 2/Packages/Scala/Scala.tmLanguage` – seren Apr 23 '13 at 17:55
  • 4
    @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. – Vincent Ketelaars Nov 08 '13 at 19:13
29

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

squeegee
  • 762
  • 8
  • 10
  • 2
    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! – MM. Dec 24 '14 at 12:17
  • 1
    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. – Jake Rayson Apr 24 '17 at 08:30
19

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

General Grievance
  • 4,555
  • 31
  • 31
  • 45
xpeiro
  • 733
  • 5
  • 21
14

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

KG -
  • 7,130
  • 12
  • 56
  • 72
14

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.

Ulf Gjerdingen
  • 1,414
  • 3
  • 16
  • 20
Hello Wor1d
  • 453
  • 4
  • 9
3

I know this topic is old now, but let me state a new approach, some people might find this easy to understand and do.

Open Sublime Text(make sure you have Package Control installed and ready in your Sublime Text):

  1. Ctrl+Shift+P(or Cmd+Shift+P for MacOS guys)
  2. Type "PRV"(Package Resource Viewer), select the one ending with :Open Resource
  3. Type "SCALA"/"scala" and press Enter
  4. Type Scala.sublime-syntax and press Enter and press Esc to close the open list
  5. Now in Scala.sublime-syntax goto the section file_extensions: and add your file extension sbt(like - sbt) in the end of that section
  6. Save and close the file and restart Sublime Text, you'll now have Scala syntax highlighting for your custom .sbt extension. Same steps can be done with any file type like XML, PHP, HTML, JS etc.

Pretty easy to follow, right ?

Vicky Dev
  • 1,893
  • 2
  • 27
  • 62