747

How do I set a default filetype for a certain file extension in Sublime Text 2? Specifically I want to have *.cfg files default to having Ini syntax highlighting but I cannot seem to figure out how I could create this custom setting.

jpsecher
  • 4,461
  • 2
  • 33
  • 42
keiththomps
  • 8,005
  • 3
  • 15
  • 17

5 Answers5

1628

In the current version of Sublime Text 2 (Build: 2139), you can set the syntax for all files of a certain file extension using an option in the menu bar. Open a file with the extension you want to set a default for and navigate through the following menus: View -> Syntax -> Open all with current extension as... ->[your syntax choice].

Updated 2012-06-28: Recent builds of Sublime Text 2 (at least since Build 2181) have allowed the syntax to be set by clicking the current syntax type in the lower right corner of the window. This will open the syntax selection menu with the option to Open all with current extension as... at the top of the menu.

Updated 2016-04-19: As of now, this also works for Sublime Text 3.

Colin R
  • 17,711
  • 2
  • 20
  • 28
  • 2
    Did you try `Open all with current extension as...` or just setting the syntax via a choice in the `Syntax` menu? – Colin R Oct 25 '12 at 12:40
  • Confirmed. You need to restart Sublime for the changes to stick. Also, this doesn't change the "active" file - you can tell by looking in the bottom right at the syntax it has chosen. Restarting fixes it though. – dmackerman Nov 29 '12 at 16:10
  • 8
    Can this be done on a per-project basis? For example, for one project, I might want Mako syntax for .html files; while another might use another syntax. – Ken Kinder Dec 04 '12 at 21:19
  • 45
    This is still the method used in ST3 (as of build 3010). No restart seems to be required, and all active files with the extension are updated automatically. – tbeseda Feb 04 '13 at 18:34
  • @tb. In my ST3, all choices are disabled (grey) – Ziyuan May 05 '13 at 20:17
  • 4
    @ziyuang - Make sure you have the cursor somewhere in the open file. – Richard Marskell - Drackir May 25 '13 at 00:02
  • @ziyuang, the files you're editing need to have a file extension. – user1429980 Oct 05 '13 at 01:08
  • Supplementary question: how do I get files named `build`, even those with a `#!/bin/bash` shebang, **not** to default to `NAnt Build File` formatting? – PJSCopeland Sep 17 '15 at 22:00
  • How can I achieve the same with files without extensions (e.g. `Makefile`)? I can't modify the contents of it also, as suggested in another answer. – Vadim Kotov Oct 09 '17 at 12:21
  • so many years I used to set it by hands, and so easy it was to set it authomatically )) – Ruslan Valeev Jun 20 '20 at 02:17
150

Go to a Packages/User, create (or edit) a .sublime-settings file named after the Syntax where you want to add the extensions, Ini.sublime-settings in your case, then write there something like this:

{
    "extensions":["cfg"]
}

And then restart Sublime Text

kamleshpal
  • 13
  • 4
kizu
  • 42,604
  • 4
  • 68
  • 95
  • N.B. The syntax you want to use is case sensitive (e.g. `CSS` for css) and this will override setting it via the UI (see @Colin's post) – ForbesLindesay Jan 10 '13 at 12:32
  • @Elland I [opened an issue](https://github.com/braindamageinc/SublimeHttpRequester/issues/6) for the problem. – JJD Jan 19 '13 at 00:19
  • 29
    I find it easier with sublime text 2/3, to open your file, then select the syntax you want to use. Then click Preferences-> Settings - More -> Syntax Specific - User. And it will create that file for you. Just paste the above code in, save, and restart. – kokorohakai Jun 16 '13 at 15:54
  • Used this way because I was able to remove a settings file that was overriding my settings via the UI – turbo2oh Nov 27 '13 at 16:42
  • Is there any way to configure this for a particular project? – steinybot Oct 06 '15 at 22:34
  • NOTE: If your files have double extensions (such as `some_page.html.erb` in Ruby on Rails), make sure to add `{"extensions": ["html.erb"]}`—just adding `["erb"]` at least currently does not work. – Bryan Dimas Sep 06 '17 at 18:23
  • This works great in Sublime 3 for scss.liquid files (files with double extensions). I added SCSS.sublime-settings with content: {"extensions": ["scss.liquid"]} and wammo bammo. It's pretty! – Cliff Ribaudo Nov 01 '17 at 21:29
21

In ST2 there's a package you can install called Default FileType which does just that.

More info here.

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
trejox
  • 211
  • 2
  • 2
  • 1
    `This package sets the default file type of new files to be either the same as the current file, or a predefined default.` Exactly what I Was looking for! Thanks – Ricardo Saporta Nov 12 '12 at 17:50
  • in ST3, it also works! just need some manual work (save DefaultFileType in ST3 user path. – staticor Nov 10 '14 at 05:01
5

You can turn on syntax highlighting based on the contents of the file.

For example, my Makefiles regardless of their extension the first line as follows:

#-*-Makefile-*- vim:syntax=make

This is typical practice for other editors such as vim.

However, for this to work you need to modify the Makefile.tmLanguage file.

  1. Find the file (for Sublime Text 3 in Ubuntu) at:

    /opt/sublime_text/Packages/Makefile.sublime-package
    

Note, that is really a zip file. Copy it, rename with .zip at the end, and extract the Makefile.tmLanguage file from it.

  1. Edit the new Makefile.tmLanguage by adding the "firstLineMatch" key and string after the "fileTypes" section. In the example below, the last two lines are new (should be added by you). The <string> section holds the regular expression, that will enable syntax highlighting for the files that match the first line. This expression recognizes two patterns: "-*-Makefile-*-" and "vim:syntax=make".

    ...
    <key>fileTypes</key>
    <array>
        <string>GNUmakefile</string>
        <string>makefile</string>
        <string>Makefile</string>
        <string>OCamlMakefile</string>
        <string>make</string>
    </array>
    
    <key>firstLineMatch</key>
    <string>^#\s*-\*-Makefile-\*-|^#.*\s*vim:syntax=make</string>
    
  2. Place the modified Makefile.tmLanguage in the User settings directory:

    ~/.config/sublime-text-3/Packages/User/Makefile.tmLanguage
    

All the files matching the first line rule should turn the syntax highlighting on when opened.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
elomage
  • 4,334
  • 2
  • 27
  • 23
  • This was helpful. Nice to know it's possible, but editing every .sublime-package file for each file type that might contain such a pattern is not practical. Therefore, I probably won't use this feature. – Travis Spencer Mar 05 '16 at 16:05
2

The best solution for me turned out to be to used the ApplySyntax package.

The steps are as follows:

  1. Install the package via Package Control
  2. CTRL + SHIFT + P and enter ApplySyntax: Browse Syntaxes. Find your desired syntax here and note the exact line shown, e.g. I was looking to set it to Markdown from the Markdown Editing package, so for me the line was MarkdownEditing/syntaxes/Markdown.
  3. CTRL + SHIFT + P and enter ApplySyntax: Settings.
  4. On line "new_file_syntax": "XYZ", enter the line from Step 2.

See here for further documentation.

I found this to work better than the DefaultFileType package, because it isn't limited to just new files created by pressing CTRL + N and captured new tabs opened by clicking the empty space to the right of an open tab.

I hope is useful to someone 11 years after the original question was asked.

Arc
  • 23
  • 3