Sublime Forum

FR - - "trim_empty_lines_on_save": true`?

#1

Subject : Feature Request – Native “Remove Empty Lines on Save” Setting

Currently, Sublime Text offers trim_trailing_white_space_on_save to remove spaces at the end of lines, but it does not remove entirely empty lines (blank interlines).

So without installing third-party plugins like DeleteBlankLines or using manual Regex replacements every time.

Suggestion: why not a "trim_empty_lines_on_save": true ?

This option would automatically remove consecutive blank lines or single empty lines upon saving, similar to how trailing spaces are handled. This would streamline the workflow, improve code cleanliness by default, and reduce the dependency on external plugins for basic formatting tasks.

Thank you for considering this improvement!

So maybe it’s not really “modern” idea ( i am a newbe), but in all case thanks for this useful modern soft clear and solid .
(i don’t give my name because i don’t want to be “searchable” without real dialog and i don’t want to be member of a community where i know nobody personaly and deliberatly so i am modern of this modernity …)

0 Likes

#2

trim_trailing_white_space_on_save does remove white space on empty lines. However the trim_only_modified_white_space setting - which is enabled by default - makes it so that only white space you added with Sublime Text is removed. Perhaps that is the behavior you’re seeing.

0 Likes

#3

Not all empty lines are equal. For instance, in .MD files, blank lines convey semantic value, and are used to delineate paragraphs, the start and end of lists, etc.

I’ve written my own ST plugins to clean up various syntaxes. In MD, it collapses consecutive empty lines to one, to shrink vertical whitespace, but retain semantic meaning. In my case, I also want to collapse any empty lines WITHIN a list block, to avoid li > p when output to html.

It is not pretty, and those more skilled in .py would call it clumsy, but it works. It is very specifically scoped for MD.


def normalise_markdown_empty_lines(text):
    """Normalise empty lines while keeping Markdown list blocks compact.

    This is an extract of CleanMD's final spacing pass. It does four things:

    - collapses runs of empty lines to a single empty line between paragraphs
    - ensures an empty line before the first item in a list block
    - removes empty lines between consecutive list items
    - ensures an empty line after a list block before normal paragraph text

    It intentionally treats indented list items as list items too, because
    `line.strip()` is used before matching the marker.
    """
    lines = text.splitlines()
    normalised_lines = []

    inside_list = False
    found_empty_line = False

    for line in lines:
        stripped = line.strip()
        is_list_item = re.match(r"^([-*+]|\d+\.)\s+", stripped)

        if is_list_item:
            # The first item in a list block should be separated from the
            # preceding paragraph. Further list items stay adjacent.
            if not inside_list:
                normalised_lines.append("")

            normalised_lines.append(line)
            inside_list = True
            found_empty_line = False
            continue

        if stripped == "":
            # Defer emitting empty lines until we know what follows. This lets
            # us collapse repeated empties and discard trailing empty lines.
            found_empty_line = True
            continue

        # For ordinary content, restore one empty line if the source had one
        # or if we are leaving a list block.
        if found_empty_line or inside_list:
            normalised_lines.append("")

        normalised_lines.append(line)
        inside_list = False
        found_empty_line = False

    return "\n".join(normalised_lines)

Here are the results:
BEFORE:

# Heading

paragraph text

second paragraph




- list item one

- list item two


Trailing paragraph

AFTER

# Heading

paragraph text

second paragraph

- list item one
- list item two

Trailing paragraph
0 Likes

#4

thanks for celerity , in fact i have found the package trimmer for do that : collapse a line and more.

If you want to translate this package into another language, don’t edit the .sublime-package file directly.
The .sublime-package is just a ZIP archive used as the default package.
Instead (for windows):

  1. Extract Trimmer.sublime-package.
  2. Create the folder:
    Data/Packages/Trimmer/
  3. Copy all extracted files into that folder.
  4. Edit Main.sublime-menu (and any other files you want to translate).
  5. it’s hot-reloaded (but better to reload ) Sublime Text menu is changed .

**

Most text-based resources in Sublime Text support automatic hot reloading, so changes take effect immediately after saving.
No restart, no reload, no frustration.
**

Sublime **Text always gives priority to the unpacked package inData/Packagesover the archived packag**e in Data/Installed Packages, so your translated menu will be used automatically without modifying the original archive.
a good tips no ? :innocent:

The perfect prompt for ChatGPT was:
"You are a pedagogical software engineer with expertise in Sublime Text and plugin development."
:upside_down_face:

0 Likes

#5

Trimmer is excellent, and I have been using it for years. It can be a little heavy-handed at times (eg in .MD files), hence the homegrown solution above.

0 Likes