Skip to content

Instantly share code, notes, and snippets.

@caub caub/sublimetext.md
Last active Jul 4, 2019

Embed
What would you like to do?
VSCode's Format on save for SublimeText

requirement: TypeScript package should be installed

Step 1: Create a new plugin (Tools > Developer > new plugin) with the following, save it as format-on-save.py for example:

import sublime
import sublime_plugin

class FormatTypescriptOnSave(sublime_plugin.EventListener):
  def on_pre_save(self, view): 
    if "TypeScript" in view.settings().get("syntax") or "JavaScript" in view.settings().get("syntax"):
      view.run_command("typescript_format_document")

source: https://stackoverflow.com/a/42237769/3183756

Step 2: Customize additional formatting settings in ~/.config/sublime-text-3/Packages/TypeScript/typescript/libs/editor_client.py, here I added "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": False

    def set_features(self):
        host_info = "Sublime Text version " + str(sublime.version())
        # Preferences Settings
        format_options = {
            "tabSize": self.tab_size,
            "indentSize": self.indent_size,
            "convertTabsToSpaces": self.translate_tab_to_spaces,
            "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": False,
            "insertSpaceAfterFunctionKeywordForAnonymousFunctions": True, # False by default on SublimeText TypeScript
        }
        self.service.configure(host_info, None, format_options)

source: https://github.com/Microsoft/TypeScript-Sublime-Plugin/issues/321#issuecomment-127380975

create a .vscode/settings.json file with:

{
    "javascript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": false,
    "editor.formatOnSave": true, // of course
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.