在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的更多信息。

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