Search code examples
xcodeeditortrimdeveloper-tools

Trim trailing spaces in Xcode


Is there a way to force Xcode to trim trailing whitespaces when I save file?

I'm using version 3.1.3 if that matters.


Solution

  • 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/perl
    
    while (<>) {
        s/\s+$//;
        print "$_\n";
    }