在 Sublime Text 2 中为不同文件类型设置默认语法
如何在 Sublime Text 2 中为特定文件扩展名设置默认文件类型?具体来说,我希望 *.cfg 文件默认具有 Ini 语法突出显示,但我似乎无法弄清楚如何创建此自定义设置。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在当前版本的 Sublime Text 2(内部版本:2139)中,您可以使用菜单栏中的选项设置特定文件扩展名的所有文件的语法。打开带有您想要设置默认扩展名的文件,然后浏览以下菜单:
View ->语法->以当前扩展名打开所有内容... ->[您的语法选择]
。更新于 2012-06-28: Sublime Text 2 的最新版本(至少自 Build 2181 起)允许通过单击窗口右下角的当前语法类型来设置语法。这将打开语法选择菜单,并在菜单顶部提供
以当前扩展名打开所有内容...
选项。更新于 2016-04-19:到目前为止,这也适用于 Sublime Text 3。
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.
转到
Packages/User
,创建(或编辑)一个.sublime-settings
文件,该文件以要添加扩展的语法Ini 命名。 sublime-settings
在你的情况下,然后写下这样的内容:然后重新启动 Sublime Text
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:And then restart Sublime Text
在 ST2 中,您可以安装一个名为 Default FileType 的软件包,它就是这样做的。
更多信息此处。
In ST2 there's a package you can install called Default FileType which does just that.
More info here.
您可以根据文件的内容打开语法突出显示。
例如,我的 Makefiles 无论其扩展名如何,第一行如下:
这是其他编辑器(例如 vim)的典型做法。
但是,要使其工作,您需要修改
Makefile.tmLanguage
文件。在以下位置找到该文件(适用于 Ubuntu 中的 Sublime Text 3):
注意,这实际上是一个 zip 文件。复制它,在末尾用 .zip 重命名,然后从中提取 Makefile.tmLanguage 文件。
通过在“fileTypes”部分之后添加“firstLineMatch”键和字符串来编辑新的
Makefile.tmLanguage
。在下面的示例中,最后两行是新的(应由您添加)。
部分包含正则表达式,它将为与第一行匹配的文件启用语法突出显示。该表达式识别两种模式:“-*-Makefile-*-
”和“vim:syntax=make
”。<前><代码>...
文件类型
<数组>
<字符串>GNUmakefile
<字符串>makefile
<字符串>Makefile
<字符串>OCamlMakefile
<字符串>make
将修改后的
Makefile.tmLanguage
放入用户设置目录中:所有符合第一行规则的文件在打开时都应打开语法高亮。
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:
This is typical practice for other editors such as vim.
However, for this to work you need to modify the
Makefile.tmLanguage
file.Find the file (for Sublime Text 3 in Ubuntu) at:
Note, that is really a zip file. Copy it, rename with .zip at the end, and extract the Makefile.tmLanguage file from it.
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
".Place the modified
Makefile.tmLanguage
in the User settings directory:All the files matching the first line rule should turn the syntax highlighting on when opened.
对我来说最好的解决方案是使用 ApplySyntax 包。
步骤如下:
CTRL + SHIFT + P
,然后输入ApplySyntax: Browse Syntaxes
。在这里找到您想要的语法,并记下显示的确切行,例如,我希望将其设置为 Markdown 编辑包中的 Markdown,所以对我来说,该行是MarkdownEditing/syntaxes/Markdown
。CTRL + SHIFT + P
并输入ApplySyntax: Settings
。"new_file_syntax": "XYZ"
行上,输入步骤 2 中的行。请参阅 此处了解更多文档。
我发现它比 DefaultFileType 包 更好,因为它不仅限于新的通过按
CTRL + N
创建的文件,并捕获通过单击打开的选项卡右侧的空白区域打开的新选项卡。我希望对提出最初问题 11 年后的人有用。
The best solution for me turned out to be to used the ApplySyntax package.
The steps are as follows:
CTRL + SHIFT + P
and enterApplySyntax: 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 wasMarkdownEditing/syntaxes/Markdown
.CTRL + SHIFT + P
and enterApplySyntax: Settings
."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. ????