Justin Duke

Removing trailing whitespace from Swift files on your Mac

As I was getting Barback 4 ready to ship, I started doing some polish on the actual code since it’d be a little bit before I picked up the codebase again and I wanted to make sure it was relatively hospitable when I came back. (Nobody likes coming back from vacation to a messy house.)

I installed the lovely tool SwiftLint to help clean the codebase, and I discovered that I had a boatload of trailing whitespace everywhere. (I blame Xcode – for most things, but this in particular.)

Thankfully, a little shell magic made this painless to clean up. I made sure everything was committed (just in case), and then ran:

find . -name '*.swift' | xargs -I{} sed -i '' 's/[[:space:]]*$//g' {}

in the root source directory.

Presto chango! It’s all good to go, and I’m down from around 1200 lint warnings to a mere 500.

(This approach naturally works just as well with non-Swift files: just replace the *.swift with *.py or *.js or whatever.)

Liked this post? You should subscribe to my newsletter and follow me on Twitter.

(I've got an RSS feed, too, if you'd prefer.)