Sublime Forum

Markdown as default syntax

#1

I’ve looked around here and can’t find my answer. Seems like a simple question, but maybe not.

How can I set my default syntax to Markdown? Ideally, every time I open a new ST instance, the blank file before me will be Markdown, not Plain Text.

Short of that, I’d like every new file I create to automatically be a markdown file. I thought the DefaultFileType package might be what I was looking for, but no, that creates new files in the desired syntax only if they’re invoked with ctrl-n. I don’t use ctrl-n; I use the sidebar enhancement package and I’m always right-clicking in there to create new files (in my desired sub-directory) and DefaultFileType doesn’t work on those.

And short of that, if I could at least put “change this file’s syntax to markdown” on a key-binding, that would be better than nothing. But I can’t find any instrux on how to do that. This twelve-year-old thread seemed promising at first, but didn’t work. I’ve tried recording my own macro for it, but it doesn’t work (I gather that’s because it’s a window-level command?).

I’m now doing almost everything in ST using Markdown, and one of the above three functionalities would be helpful. Any hints appreciated.

0 Likes

#2

For ST 4, you can have following simple plugin.

Packages/User/assign_default_syntax.py

from typing import Any, List, Union

import sublime
import sublime_plugin


def get_setting(key: str, default: Any) -> Any:
    return sublime.load_settings("Preferences.sublime-settings").get(key, default)


def get_syntaxes(syntax_like: Union[str, sublime.Syntax]) -> List[sublime.Syntax]:
    if isinstance(syntax_like, sublime.Syntax):
        return [syntax_like]
    if syntax_like.startswith("scope:"):
        return sublime.find_syntax_by_scope(syntax_like[6:])
    return sublime.find_syntax_by_name(syntax_like)


class AssignDefaultSyntax(sublime_plugin.EventListener):
    def on_new(self, view: sublime.View) -> None:
        default_syntax = str(get_setting("default_syntax", ""))
        if syntaxes := get_syntaxes(default_syntax):
            view.assign_syntax(syntaxes[0])

And then in preferences, set "default_syntax" to "Markdown" or "scope:text.html.markdown".

1 Like

#3

That plugin implements a function that implements a version of new_file that uses a different syntax. The menu entry that you’re using rom the the sidebar enhancements package is using the normal new_file command, which is why it doesn’t work.

One way to do what you want is to use a plugin which listens for the event that says that a new tab has been created and trigger the change there:

import sublime
import sublime_plugin


class DefaultMarkdownSyntax(sublime_plugin.EventListener):
    def on_new(self, view):
        view.assign_syntax('Packages/Markdown/Markdown.sublime-syntax')

If you put that in place, any new tab that’s created is Markdown no matter how. That may or may not be a issue if there is ever a situation where a newly created tab is not supposed to be markdown though.

In which case, something like this might be more the thing:

import sublime
import sublime_plugin


class SetMarkdownSyntaxCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.assign_syntax('Packages/Markdown/Markdown.sublime-syntax')

That one implements a set_markdown_syntax command that would flip the current file to be markdown.

If you’re not sure how to use a plugin: https://youtu.be/lBgDilqulxg

1 Like

#4

Thanks to you both–it’s much appreciated. I’m off to experiment!

0 Likes

#5

I tried each of these this morning, and they all work great, just as advertised. I’m probably going to go with the EventListener version, mostly because it’s simple enough for me to sort of understand. :slight_smile: Thanks again, jfcherng and OdatNurd.

0 Likes