Trim trailing spaces in Xcode Trim trailing spaces in Xcode xcode xcode

Trim trailing spaces in Xcode


Starting from Xcode 4.4 whitespaces will be trimmed automatically by default, unless the line is all whitespace. You can also activate Including whitespace-only lines to fix this, which is not active by default.

Go to Xcode > Preferences > Text Editing > While editing

Xcode preferences screenshot


I'm using the Google Toolbox For Mac Xcode Plugin, it adds a "Correct whitespace on save" parameter that trim trailing whitespace on save. I missed that a lot from emacs.


You can create a script and bind it to a keyboard shortcut:

  • Select Scripts Menu > Edit User Scripts...
  • Press the + button and select New Shell Script
  • Give it a name like "Strip Trailing Spaces", and give it a shortcut like ⌃⇧R.
  • Set Input to "Selection" and Output to "Replace Selection"

Then enter the following script:

#!/usr/bin/perlwhile (<>) {    s/\s+$//;    print "$_\n";}