delete trailing whitespace

37 views
Skip to first unread message

John Gabriele

unread,
Aug 19, 2019, 7:47:25 PM8/19/19
to niceeditor
How could I make a macro to delete trailing whitespace?

I was thinking something along the lines of:

~~~
AtomicUndo +
PushPrefs
MoveSOF
SearchBack 1
MoveEOL
FindRegExp [^     ]
MoveRight
DeleteEOL
LineDown
PopPrefs
AtomicUndo -
~~~

but that's not right, as it only does one line... (How do I get it to work for the whole file? Also, maybe set a bookmark so I can get the cursor back to where it started after the operation is complete?)

Or maybe it's a better idea to only delete-trailing-whitespace the current selection?

Thanks!
-- John

John Gabriele

unread,
Aug 20, 2019, 1:13:08 AM8/20/19
to niceeditor
One thing I came up with is to create ~/.ne/delete-trailing-whitespace:

~~~
PushPrefs

SearchBack 1
MoveEOL
FindRegExp [^     ]
MoveRight
DeleteEOL
LineDown
PopPrefs
~~~

and bind that to a key combo (`Alt-]`), then delete that trailing whitespace line-by-line. Not practical for a while file, but nice enough for a small area where I think I left some trailing whitespace.

-- John

todd_...@unc.edu

unread,
Aug 20, 2019, 6:28:39 AM8/20/19
to niceeditor
I delete trailing whitespace a lot, especially as the project I'm most involved in at work passes all commits through a linter and it rejects trailing whitespace!
Funny though -- as much as I do this, it never occurred to me to make a macro for it.

The way I've done it for ever (my fingers might break if I try a different way) is:
  ^j 1          # To get to the top of the file
  ^a            # to get to the left edge
  Ctrl-Shift-_  # bring up a regex search
  [ I]+$        # Note: the "I" is a literal Tab character. Hit the Tab key on the prompt. Took me 10 years to figure that out.

The "$" matches the end-of-line. The "[ I]+" matches a string of one or more spaces and tabs.

If it finds any lines with trailing whitespace, then hit ^R to replace. Make sure the response to the prompt is empty, and hit Enter.

Or better yet, hit ^k to get to the command line, then enter
  RA ""
That's the ReplaceAll command with a quoted empty string as the parameter. It should come back with a message like "2 replacements made."

Making a macro that puts you back where you were will involve setting a bookmark, so you'd want a convention for your bookmarks that reserves one for macros like this. Maybe. Or if you don't use many bookmarks and you don't care, then no big deal. Maybe the best option is to use the automatic bookmark "-" so you don't mess up any of your numbered bookmarks.

It's important to make a macro that isn't going to fail half-way throug. If you do a FindRegEx for trailing whitespace, and there isn't any, then the macro is going to stop right there and not return you to your bookmarked starting place.

The solution to that problem is to ensure there's some trailing whitespace! The downside, if you care, is that the document will always end up being marked as changed after running this macro. So, here's my cut at a TWS (Trailing White Space) macro:

PushPrefs
SearchBack 0
SetBookmark -
MoveEOL
InsertString " "
GotoLine 1
MoveSOL
FindRegExp [   ]+$
ReplaceAll ""
GotoBookmark -
PopPrefs

Again, inside the BindRegExp brackets are a single space and a literal Tab character.

Give that a try and see how you like it.

John Gabriele

unread,
Aug 20, 2019, 10:25:56 AM8/20/19
to niceeditor
On Tuesday, August 20, 2019 at 6:28:39 AM UTC-4, todd_...@unc.edu wrote:
 

  [ I]+$        # Note: the "I" is a literal Tab character. Hit the Tab key on the prompt. Took me 10 years to figure that out.


(within the FindRegExp prompt) Whoa! Didn't know that. Nice!
 
{snip}

Maybe the best option is to use the automatic bookmark "-" so you don't mess up any of your numbered bookmarks.
 
Good idea.
 
It's important to make a macro that isn't going to fail half-way throug. If you do a FindRegEx for trailing whitespace, and there isn't any, then the macro is going to stop right there and not return you to your bookmarked starting place.

The solution to that problem is to ensure there's some trailing whitespace!

Hehe. :)
 
The downside, if you care, is that the document will always end up being marked as changed after running this macro. So, here's my cut at a TWS (Trailing White Space) macro:

PushPrefs
SearchBack 0
SetBookmark -
MoveEOL
InsertString " "
GotoLine 1
MoveSOL
FindRegExp [   ]+$
ReplaceAll ""
GotoBookmark -
PopPrefs


Ah, ReplaceAll! The key here for running through the whole file.

Any reason you use "GotoLine 1, MoveSOL" instead of MoveSOF (Alt-a)?
 

Give that a try and see how you like it.

Works great! Thank you! Only minor complaint is that it doesn't report the number of replacements --- after running the macro, the status line reports the results of PopPrefs, but that's fine.

jason404

unread,
Jan 24, 2020, 5:10:51 PM1/24/20
to niceeditor
I found another way.  From putting bits together I found through google, I've come up with a function that removes all trailing whitespace (tabs and/or spaces and combinations of those), any whitespace in otherwise empty lines, trailing newlines and then adds a newline at the end.  I put it in .menus, under MENU "Edit":

ITEM "CleanUp WSpace " Through awk '{sub(/[ \t]+$/,""); print}' | sed -Ez '$ s/\n+$//' | sed -e '$a\'


I've also added a couple of functions which includes that as well as turns 4 consecutive spaces into tabs and vice versa:

ITEM "Spaces2Tabs    " Through unexpand --tabs=4 | awk '{sub(/[ \t]+$/,""); print}' | sed -Ez '$ s/\n+$//' | sed -e '$a\'
ITEM "Tabs2Spaces    " Through expand --tabs=4 | awk '{sub(/[ \t]+$/,""); print}' | sed -Ez '$ s/\n+$//' | sed -e '$a\'

Cheers
Reply all
Reply to author
Forward
0 new messages