Skip to content

Instantly share code, notes, and snippets.

@gxfxyz
Created November 16, 2018 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gxfxyz/d6b2ea1f30503659ef3df15eddbf8efb to your computer and use it in GitHub Desktop.
Save gxfxyz/d6b2ea1f30503659ef3df15eddbf8efb to your computer and use it in GitHub Desktop.
Sublime Text 3 commands to turn on/off/toggle trim_trailing_white_space_on_save
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Sublime Text 3 commands to turn on/off/toggle trim_trailing_white_space_on_save.
Only temporarily affect current file (view).
How to use:
Save to your "/Sublime Text 3/Packages/User" directory.
"""
import sublime
import sublime_plugin
class TrimtrailingspaceEnableCommand(sublime_plugin.TextCommand):
def run(self, edit):
to_enable = True
settings = self.view.settings()
enabled = settings.get('trim_trailing_white_space_on_save') or None
settings.set('trim_trailing_white_space_on_save', to_enable)
old_status = "enabled" if enabled else "disabled"
new_status = "enabled" if to_enable else "disabled"
message = "Trim Trailing Space:\n\n{} -> {}"
message = message.format(old_status, new_status)
sublime.message_dialog(message)
class TrimtrailingspaceDisableCommand(sublime_plugin.TextCommand):
def run(self, edit):
to_enable = False
settings = self.view.settings()
enabled = settings.get('trim_trailing_white_space_on_save') or None
settings.set('trim_trailing_white_space_on_save', to_enable)
old_status = "enabled" if enabled else "disabled"
new_status = "enabled" if to_enable else "disabled"
message = "Trim Trailing Space:\n\n{} -> {}"
message = message.format(old_status, new_status)
sublime.message_dialog(message)
class TrimtrailingspaceToggleCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = self.view.settings()
enabled = settings.get('trim_trailing_white_space_on_save') or None
to_enable = not enabled
settings.set('trim_trailing_white_space_on_save', to_enable)
old_status = "enabled" if enabled else "disabled"
new_status = "enabled" if to_enable else "disabled"
message = "Trim Trailing Space:\n\n{} -> {}"
message = message.format(old_status, new_status)
sublime.message_dialog(message)
"""
Old way:
Change global setting, which affects all files.
"""
# class TrimtrailingspaceTurnonCommand(sublime_plugin.ApplicationCommand):
# def run(self):
# s = sublime.load_settings("Preferences.sublime-settings")
# old = s.get('trim_trailing_white_space_on_save')
# old = 'On' if old else 'Off'
# s.set('trim_trailing_white_space_on_save', True)
# sublime.save_settings("Preferences.sublime-settings")
# message = "TrimTrailingSpace: Now {}, was {}".format('On', old)
# sublime.status_message(message)
# class TrimtrailingspaceTurnoffCommand(sublime_plugin.ApplicationCommand):
# def run(self):
# s = sublime.load_settings("Preferences.sublime-settings")
# old = s.get('trim_trailing_white_space_on_save')
# old = 'On' if old else 'Off'
# s.set('trim_trailing_white_space_on_save', False)
# sublime.save_settings("Preferences.sublime-settings")
# message = "TrimTrailingSpace: Now {}, was {}".format('Off', old)
# sublime.status_message(message)
[{
"caption": "Trim Trailing Space: Enable",
"command": "trimtrailingspace_enable"
}, {
"caption": "Trim Trailing Space: Disable",
"command": "trimtrailingspace_disable"
}, {
"caption": "Trim Trailing Space: Toggle",
"command": "trimtrailingspace_toggle"
}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment