如何在Sublime Text 2中为某个文件扩展名设置默认文件类型?具体来说,我想有*.cfg文件默认有Ini语法高亮显示,但我似乎不知道如何创建这个自定义设置。


转到Packages/User,创建(或编辑)一个.sublime-settings文件,该文件以你想添加扩展名的语法Ini命名。在你的情况下,崇高的设置,然后写这样的东西:

{
    "extensions":["cfg"]
}

然后重新启动Sublime Text

在当前版本的Sublime Text 2 (Build: 2139)中,您可以使用菜单栏中的选项为某个文件扩展名的所有文件设置语法。打开一个具有您想要设置的默认扩展名的文件,并通过以下菜单导航:View -> Syntax ->打开所有具有当前扩展名as…->[你的语法选择]。

2012-06-28更新:Sublime Text 2的最新版本(至少从Build 2181开始)允许通过单击窗口右下角的当前语法类型来设置语法。这将打开语法选择菜单,选项open all with current extension as…在菜单的最上面。

更新2016-04-19:截至目前,这也适用于Sublime Text 3。

在ST2中,你可以安装一个名为Default FileType的包,它就是这样做的。

更多信息请点击这里。

您可以根据文件的内容开启语法高亮显示。

例如,我的Makefiles不管它们的扩展名是什么,第一行如下:

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

这是其他编辑器(如vim)的典型实践。

但是,要使其工作,您需要修改 Makefile。tmLanguage文件。

找到这个文件(Ubuntu中的Sublime Text 3): / opt / sublime_text /包/ Makefile.sublime-package

注意,这实际上是一个zip文件。复制它,在末尾重命名为.zip,然后提取Makefile。tmLanguage文件。

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> Place the modified Makefile.tmLanguage in the User settings directory: ~/.config/sublime-text-3/Packages/User/Makefile.tmLanguage

所有匹配第一行规则的文件在打开时都应该打开语法高亮显示。