Set default syntax to different filetype in Sublime Text 2

Sublimetext2Text EditorSublimetext

Sublimetext2 Problem Overview


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.

Sublimetext2 Solutions


Solution 1 - Sublimetext2

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.

Solution 2 - Sublimetext2

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

Solution 3 - Sublimetext2

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

More info here.

Solution 4 - Sublimetext2

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.

Solution 5 - Sublimetext2

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkeiththompsView Question on Stackoverflow
Solution 1 - Sublimetext2Colin RView Answer on Stackoverflow
Solution 2 - Sublimetext2kizuView Answer on Stackoverflow
Solution 3 - Sublimetext2trejoxView Answer on Stackoverflow
Solution 4 - Sublimetext2elomageView Answer on Stackoverflow
Solution 5 - Sublimetext2ArcView Answer on Stackoverflow