Script to clean whitespace from otherwise empty lines

I don’t know if anyone else has this problem:

I occasionally accidentally put whitespace (tabs spaces) on what would otherwise be blank lines (I think this happens by default when you press enter but don’t create a task). This makes folding messier because it folds in the ‘blank’ line into the project, removing the gap between projects.

Before:

Folded (no gap between projects)

A shell one-liner to fix this is:

gawk -i inplace '/^(\s+)?$/ {printf "\n"} /[a-zA-Z0-9]/ {print}' <path>

The first regex ^(\s+)?$ prints out a newline for any line that has only whitespace or nothing. The other prints out whatever it encounters.

Thought that this might help someone.

3 Likes

Good thought.

Do you think the latter might need broadening outside the particular case of EN ?

(EU and Scandinavian diacritics, levantine languages, CJK etc etc)

You’re right, I just needed to brush up on my RegEx a little!

gawk -i inplace '/^(\s+)?$/ {printf "\n"} /\w/ {print}' <path>

1 Like