Trim trailing spaces in Xcode

44,125

Solution 1

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";
}

Solution 2

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

Solution 3

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.

Solution 4

I find using the new Automatically trim trailing whitespace -> Including whitespace-only lines as suggested by @MartinStolz works well while editing but I sometimes need to do Cmd + a -> Ctrl + i and save the file multiple times with the file in focus when I'm not editing.

In case you want to clean a whole project (excluding .md files) without using scripts, you can also do a Find & Replace -> Regular Expression. Since this technique removes trailing space and tabs for documentation/comments as well, you can also try a negative lookahead for blacklisted characters to filter out single-line comments.

Find all trailing whitespace:

[\t ]+$

Find trailing whitespace without touching single-line comments:

^(?!.*\\\\)[\t ]+$

Replace:

<nothing>

So linting can also be done without swiftlint autocorrect or similar third party solutions.

Solution 5

For Xcode 8, I installed the swimat Xcode plug-in, for formatting Swift code, which removed all trailing spaces and whitespace-only lines.

Installation Methods

  1. Install via homebrew-cask:

    brew cask install swimat
    
  2. Download the App directly:
    https://github.com/Jintin/Swimat/releases/download/v1.3.5/Swimat.zip

  3. Clone extension branch and archive to Mac App.

Usage

Once installed, you can run Swimat in Xcode via Editor -> Swimat -> Format.

Share:
44,125
Alexander Gladysh
Author by

Alexander Gladysh

:-) mailto: [email protected]

Updated on October 08, 2021

Comments

  • Alexander Gladysh
    Alexander Gladysh over 2 years

    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.

  • Alexander Gladysh
    Alexander Gladysh over 14 years
    Ah, yes, so simple... Thanks. Is there any way to associate this with file save?
  • Nikolai Ruhe
    Nikolai Ruhe over 14 years
    Edited to answer why workarounds are unsatisfying in this case
  • Alexander Gladysh
    Alexander Gladysh over 14 years
    Thanks. While I'm in favor of doing it in the proper way, I'd settle for about any not-too-intrusive hack. Those extra whitespaces are really annoying.
  • Andy
    Andy over 13 years
    I'm using this plugin as well. It does what it says.
  • deepwell
    deepwell over 12 years
    Would be perfect, unfortunately it does not work on Xcode 4 at the moment :(
  • dmaclach
    dmaclach over 12 years
    We now have some basic Xcode 4 support. See our new plugin
  • jhasse
    jhasse about 12 years
    Where can I find the "Correct whitespace on save" parameter?
  • Kristofer Sommestad
    Kristofer Sommestad almost 12 years
    I finally managed to get GTM working on Xcode 4.3.2, but the installation instructions from the GTM wiki seemed to be invalid. Instead of placing the .xcplugin in ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins (as per Google's instructions) I had to put it in /Applications/Xcode.app/Contents/PlugIns and restart Xcode.
  • Desmond Hume
    Desmond Hume almost 12 years
    @KristoferSommestad Followed your instructions for the same Xcode version (build 4E2002), but couldn't find "Correct whitespace on save" anywhere in the preferences despite of the fact that "About GTM Xcode Plugin" does appear under "Xcode" in the main menu..
  • Kristofer Sommestad
    Kristofer Sommestad almost 12 years
    No, me neither Desmond. But there's an option to strip all ws in the Edit menu, iirc. And a short command for it too.
  • Maxime
    Maxime almost 12 years
    @DesmondHume unfortunately the "Correct whitespace on save" preference is only available in GTMXcodePlugin for XCode 3.x. Since XCode 4, I'm using a custom script (called by my Makefile) to remove trailing whitespaces
  • Michael Baltaks
    Michael Baltaks almost 12 years
    I installed the plugin as per the instructions on the GTM site, and it worked as soon as I restarted Xcode, using Xcode 4.3.3 on Mac OS X 10.7.
  • SMBiggs
    SMBiggs over 11 years
    To work on an entire file (which is probably what you would want to do), set Input to "Entire Document" and Output to "Replace Document Contents." However, this leaves the cursor at the bottom of the file.
  • Basil Bourque
    Basil Bourque over 11 years
    This answer is obsolete as of Xcode 4.4. See the answer by Martin Stolz.
  • ma11hew28
    ma11hew28 almost 11 years
    This setting only trims a line's trailing whitespace after the cursor has left that line. So, it still allows you to save a file with one line of trailing whitespace, if the cursor is on that line upon save.
  • friederbluemle
    friederbluemle over 9 years
    Why on earth is this not the default? If you have an existing file with trailing whitespace, just mark all, cut, save, paste, save.
  • Urda
    Urda about 9 years
    This needs to override the hacky script solution on this answer.
  • Alexis C.
    Alexis C. over 8 years
    Using this + ctrl-i on all existing files takes care of trailing whitespaces
  • Sabrina
    Sabrina about 5 years
    this tool dont remove blank lines.
  • Blastfurnace
    Blastfurnace over 4 years
    Does this only remove trailing spaces as requested in the question?
  • Pranav Kasetti
    Pranav Kasetti about 3 years
    Right idea @H2zee, just need to use RegEx instead (check out my answer above).
  • IlyaEremin
    IlyaEremin about 3 years
    @Sabrina this answer is about removing trailing spaces, not blank lines.
  • h2zee
    h2zee over 2 years
    @Blastfurnace it depends on you, whatever you want to change, you can. let me know if you have any problem
  • internetdotcom
    internetdotcom about 2 years
    The feature is still listed in 13.3 but it does not work, or at least does not work reliably. It's common to find trailing whitespace throughout files after editing.