Skip to content

Instantly share code, notes, and snippets.

@idhamperdameian
Last active August 29, 2015 14:14
Show Gist options
  • Save idhamperdameian/ee57f17d80ed8abe5749 to your computer and use it in GitHub Desktop.
Save idhamperdameian/ee57f17d80ed8abe5749 to your computer and use it in GitHub Desktop.
Strip Trailing Whitespace in Xcode 4+

How to use

By default Xcode not remove trailing whitespace from code, this gist will do it to you.

Copy remove-whitespaces.sh anywere you like in your Mac, and be sure to give it the execute permission. Open XCode preferences go to behaviors:

alt text

Use + sign to add new custom behavior name it with Strip Trailing Spaces, feel free to name it as what you want. You also can create key binding for this custom behavior, i would like make it with ⌃⌥⌘S. Make Run checkbox checked, browse where you place remove-whitespaces.sh file.

Go to menu XCode > Behaviors > Strip Trailing Spaces or press ⌃⌥⌘S to run script in your current project.

Enjoy

#!/bin/sh
# Please note this script only filter .m, .h, .js, and .html files. You can add more extensions by separate them with pipe line pipe ( | ).
git ls-files --modified | awk '/^.*(m|h|js|html)$/' | xargs sed -i '.bak' 's/[[:space:]]*$//'
# If you don't need backup of original file, just uncoment line below
# find . -name *.bak | xargs rm -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment