If you install the RegReplace plugin, you can add these to the settings file (you can modify these how you like):
// Remove trailing spaces
"remove_trailing_spaces": {
"find": " \\t]+$",
"replace": "",
"greedy": true,
"case": true
},
// Remove all new lines at the start of the file and remove newlines > 1 at end of file
"trim_excessive_newlines": {
// (new lines at end of file | new lines at start of file)
"find": "(((\\r?\\n)+)(?=(\\r?\\n){1}(?!\\s\\S\\r\\n]))|(?<!\\s\\S\\r\\n])((\\r?\\n)+))",
"replace": ""
},
// Make sure a new line is present at file end
"ensure_newline_at_file_end":{
"find": "((^\n\r])(?!\\s\\S\\r\\n]))",
"replace": "\\1\\n"
},
Then drop this simple plugin into your User folder (I called it “on_save.py”)
[code]import sublime_plugin
import reg_replace
class OnSaveListenerCommand(sublime_plugin.EventListener):
def on_pre_save(self, view):
edit = view.begin_edit()
reg_replace.RegReplaceCommand(view).run(edit, replacements=“remove_trailing_spaces”, “trim_excessive_newlines”, “ensure_newline_at_file_end”])
view.end_edit(edit)
[/code]
And that should do what you want. You would need to disable the sublime’s settings though:
"ensure_newline_at_eof_on_save": false,
"trim_trailing_white_space_on_save": false,
I might add this directly to RegReplace in the future, so you can just define an array in the reg_replace.sublime-settings file like this (but this does not exist currently):
"replace_on_save": "replacement1", "replacement2", "etc"]