Sublime Forum

Whitespace handlers could play better together

#1

I use SublimeText2 with the following two settings:

“ensure_newline_at_eof_on_save”: true,
“trim_trailing_white_space_on_save”: true,

However, if I attempt to save a file that ends in \n\t, it gets updated and saved ending instead in \n\n. An extra newline has been added that I then need to remove.

I suspect this is because first ensure_newline_at_eof_on_save is invoked (so the file ends in \n\t\n), then trailing whitespace is removed.

Could it be that the order of these two operations could be swapped, effectively solving the problem, without any adverse side-effects? If so, it would save me repeatedly encountering this annoyance.

If not, are there any other suggestions?

0 Likes

#2

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"]
0 Likes

#3

RegReplace now supports on_pre_save events. You can read more about it here RegReplace Plugin

0 Likes

#4

Thanks for this great plugin!

The regex you had for the trim_excessive_newlines was still leaving a line for me (You may have wanted that). This will remove ALL lines at the end of the file:

"trim_excessive_newlines":
{
    "find":  "\\s]*$(?!\\w\\W])",
    "replace": ""
}

best,
M

0 Likes

#5

[quote=“superfake123”]Thanks for this great plugin!

The regex you had for the trim_excessive_newlines was still leaving a line for me (You may have wanted that). This will remove ALL lines at the end of the file:

"trim_excessive_newlines":
{
    "find":  "\\s]*$(?!\\w\\W])",
    "replace": ""
}

best,
M[/quote]

That was by design. That is why it said trim “excessive” newlines; I want one at the end of every file. It is common requirement to have at least one new line at the end of a file. Our compilers give us warnings if there is not. That is why I also have a rule to add a new line if there were no newlines: “ensure_newline_at_file_end”.

0 Likes