Skip to main content

Tailing Whitespace

Here's how to get rid of trailing whitespace from all your files:
# Strip trailing whitespace from files that end in .as.
# Change this to match your programming language.
# I'm not sure if the -i flag is supported by all OSs.
find . -name '*.as' | xargs sed -i 's/ *$//g'

# See if the changes look reasonable.
git diff

# There should be no changes that aren't whitespace changes.
git diff -w


Eric said…
Thanks JJ,

Here's my tweaks for use outside of repositories where undoing changes isn't so easy:


find . -name "*$file_ext" | xargs sed -i_$(date +%Y%m%d%H%M) 's/ *$//g'

Where file_ext is the extension of the files you want to operate on. _$(date +%Y%m%d%H%M) will ask sed to make a backup of the original file with a timestamp appended to the name.

Yep, very good.