Sublime Forum

Auto-format settings file

#1

ST sometimes auto-formats my Preferences.sublime-settings file (which means sorting by keys and indenting nested entries).

This auto-formatting is per se actually not bad but it happens unpredictable and I can’t see a way to trigger it manually. This is especially bad if you have the settings file tracked with a VCS and sometimes just everything has changed due to auto-format (example commit: github.com/schlamar/ST3User/com … 7447510fc6). So:

  • Is there a trigger for this auto-format?
  • If not, can we have a command and/or settings to trigger this manually?

Thanks

0 Likes

#2

AFAIK the autoformat is triggered every time you do a settings without changing the file manually. E.g.: changing theme, changing font-size, etc.

Not sure how configurable is this though…

0 Likes

#3

Oh, thanks for pointing me in the right direction. In fact auto-format is triggered by sublime.save_settings.

So always triggering auto-format when a settings file is saved is pretty easy:

[code]import os

import sublime
import sublime_plugin

class AutoFormatSettings(sublime_plugin.EventListener):

def on_post_save(self, view):
    name = view.file_name()
    if not name:
        return

    base = os.path.basename(name)
    _, ext = os.path.splitext(base)
    if ext == '.sublime-settings':
        sublime.save_settings(base)

[/code]

0 Likes

#4

Oh this implementation is actually bad because it overrides the settings before Sublime was able to reload them :smile:

Fixed version is at: github.com/schlamar/ST3User/blo … _format.py

0 Likes

#5

Feel free to use it :smile:

0 Likes

#6

This autoformatting is troubling because it wipes out my comments… which are usually there because I’m storing old settings to revert to, like font names.

0 Likes

#7

so don’t comment them - give the settings a different key/name like old_font_face instead. or, for easier finding while sorted, font_face_old1, font_face_old2 etc.

0 Likes

#8

Thanks. I had already figured that one out. A very hackish way to go but it works.

0 Likes