Thursday, September 30, 2010

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

2 comments:

Eric said...

Thanks JJ,

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

file_ext="py"

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.

Eric.

Shannon -jj Behrens said...

Yep, very good.