在Sublime Text 2中将默认语法设置为不同的文件类型

本文翻译自:Set default syntax to different filetype in Sublime Text 2

How do I set a default filetype for a certain file extension in Sublime Text 2? 如何在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. 具体来说,我希望* .cfg文件默认具有Ini语法高亮,但我似乎无法弄清楚如何创建此自定义设置。


#1楼

参考:https://stackoom.com/question/VmTO/在Sublime-Text-中将默认语法设置为不同的文件类型


#2楼

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: 例如,我的Makefiles无论其扩展名如何,第一行如下:

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

This is typical practice for other editors such as vim. 这是其他编辑器(如vim)的典型做法。

However, for this to work you need to modify the Makefile.tmLanguage file. 但是,为此,您需要修改Makefile.tmLanguage文件。

  1. Find the file (for Sublime Text 3 in Ubuntu) at: 在以下位置找到该文件(对于Ubuntu中的Sublime Text 3):

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

Note, that is really a zip file. 注意,这实际上是一个zip文件。 Copy it, rename with .zip at the end, and extract the Makefile.tmLanguage file from it. 复制它,最后用.zip重命名,然后从中提取Makefile.tmLanguage文件。

  1. Edit the new Makefile.tmLanguage by adding the "firstLineMatch" key and string after the "fileTypes" section. 通过在“fileTypes”部分之后添加“firstLineMatch”键和字符串来编辑新的Makefile.tmLanguage 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. <string>部分包含正则表达式,该表达式将为与第一行匹配的文件启用语法突出显示。 This expression recognizes two patterns: " -*-Makefile-*- " and " vim:syntax=make ". 该表达式识别两种模式:“ -*-Makefile-*- ”和“ 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: 将修改后的Makefile.tmLanguage放在用户设置目录中:

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

All the files matching the first line rule should turn the syntax highlighting on when opened. 与第一行规则匹配的所有文件应在打开时打开语法高亮显示。


#3楼

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: 转到一个Packages/User ,创建(或编辑)一个.sublime-settings文件,该文件以你希望添加扩展名的语法命名,在你的情况下是Ini.sublime-settings ,然后写一下这样的东西:

{
    "extensions":["cfg"]
}

And then restart Sublime Text 然后重新启动Sublime Text


#4楼

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. 在Sublime Text 2(Build:2139)的当前版本中,您可以使用菜单栏中的选项设置特定文件扩展名的所有文件的语法。 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] . 打开一个文件,其中包含您要为其设置默认值的扩展名,然后浏览以下菜单: 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. 更新2012-06-28: Sublime Text 2的最新版本(至少自Build 2181开始)允许通过单击窗口右下角的当前语法类型来设置语法。 This will open the syntax selection menu with the option to Open all with current extension as... at the top of the menu. 这将打开语法选择菜单,并在菜单顶部选择Open all with current extension as...的选项。

Updated 2016-04-19: As of now, this also works for Sublime Text 3. 更新2016-04-19:截至目前,这也适用于Sublime Text 3。


#5楼

In ST2 there's a package you can install called Default FileType which does just that. 在ST2中,您可以安装一个名为Default FileType的软件包。 More info on https://github.com/spadgos/sublime-DefaultFileType . 有关https://github.com/spadgos/sublime-DefaultFileType的更多信息。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章