如何在 Sublime Text 3 中设置语法的默认文件扩展名? [英] How do you set the default file extension for a syntax in Sublime Text 3?
问题描述
我不是在询问将语法与文件扩展名相关联,而是询问将文件扩展名与语法相关联.也就是说,在创建新文件后设置保存文件对话框中建议的文件扩展名,然后设置语法,然后点击保存.
I'm not asking about associating a syntax with a file extension, but about associating a file extension with a syntax. That is, setting the file extension suggested in the save file dialog after you create a new file, then set the syntax, then hit save.
您可以在保存对话框中更改扩展名,但最好不要每次都这样做.
You can change the extension in the save dialog, but it would be better not to have to do that every time.
推荐答案
对于默认保存带有 .txt
扩展名的纯文本文件,您可以使用一个小插件来实现:
For saving plain text files with a .txt
extension by default, you can achieve this with a small plugin:
import sublime
import sublime_plugin
class DefaultPlainTextExtensionEventListener(sublime_plugin.EventListener):
def update_default_extension(self, view, syntax):
if syntax == 'Packages/Text/Plain text.tmLanguage':
view.settings().set('default_extension', 'txt')
elif view.settings().get('default_extension', None) == 'txt':
view.settings().erase('default_extension')
def on_new_async(self, view):
self.update_default_extension(view, view.settings().get('syntax'))
def on_post_text_command(self, view, command_name, args):
if command_name == 'set_file_type':
self.update_default_extension(view, args['syntax'])
elif command_name in ('paste', 'paste_and_indent'):
self.update_default_extension(view, view.settings().get('syntax'))
请注意,我们不能使用 on_pre_save
事件,因为文件对话框在触发时已经向用户显示,并且文件名已选择.
Note that we can't use the on_pre_save
event because the file dialog has already been shown to the user by the point this is triggered, and the file name chosen.
这个想法是,当创建一个新选项卡或将语法更改为纯文本时,它会将 default_extension
设置设置为 txt
.如果语法更改为其他内容,它将删除 default_extension
.
The idea is that, when a new tab is created or the syntax is changed to Plain Text, it will set the default_extension
setting to txt
. If the syntax changes to something else, it will remove the default_extension
.
要更改语法的默认文件扩展名,需要更改 .sublime-syntax
文件重新排序设置在那里的文件扩展名,以便默认的扩展名在列表中的第一个.(使用前面引用的 default_extension
是不可靠的 - https://github.com/SublimeTextIssues/Core/issues/1527)
For changing the default file extension for a syntax, it requires changing the .sublime-syntax
file to re-order the file extensions set there so that the default one is first in the list.
(Using the default_extension
referenced earlier is unreliable - https://github.com/SublimeTextIssues/Core/issues/1527)
因此,对于 Markdown,您可以执行以下操作将默认值从 .mdown
更改为 .md
:
So, for Markdown, you could do the following to change the default from .mdown
to .md
:
- 安装 PackageResourceViewer(如果尚未安装)
- 打开命令面板
- 输入
PRV:O
- 选择
PackageResourceViewer:打开资源
- 选择
Markdown
- 选择
Markdown.sublime-syntax
找到它说的地方:
- Install PackageResourceViewer if it is not already installed
- Open the Command Palette
- Type
PRV: O
- Select
PackageResourceViewer: Open Resource
- Select
Markdown
- Select
Markdown.sublime-syntax
Find where it says:
file_extensions:
- mdown
- markdown
- markdn
- md
更改它以便 md
位于顶部:
file_extensions:
- md
- mdown
- markdown
- markdn
然后,当您创建新选项卡时,将语法设置为 Markdown 并保存,它将默认为 .md
文件扩展名.
Then, when you create a new tab, set the syntax to Markdown and save it, it will default to the .md
file extension.
请注意,您可以尝试在相关的 GitHub 存储库中创建一个问题/PR,如果您认为更改默认设置也可以使其他人受益并希望看到存储库维护者的反应.
Note that you could try to create an issue/PR at the relevant GitHub repo, if you believe that changing the default could benefit others too and want to see the repo's maintainer's reaction.
这篇关于如何在 Sublime Text 3 中设置语法的默认文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!