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.
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'))
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.
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
.
For changing the default file extension for a syntax, it requires changing the .sublime-syntax
file to re-order the file extensions set, so that the default one is first in the list.
(Using the default_extension
referenced earlier is unreliable.)
So, for Markdown, you could do the following to change the default from .mdown
to .md
:
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
Change it so that md
is at the top:
file_extensions:
- md
- mdown
- markdown
- markdn
Save the file
Then, when you create a new tab, set the syntax to Markdown and save it, it will default to the .md
file extension.
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 maintainer's reaction.