Trim trailing spaces in Xcode
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
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
-
Install via homebrew-cask:
brew cask install swimat
Download the App directly:
https://github.com/Jintin/Swimat/releases/download/v1.3.5/Swimat.zipClone
extension
branch and archive to Mac App.
Usage
Once installed, you can run Swimat in Xcode via Editor -> Swimat -> Format
.
Comments
-
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 over 14 yearsAh, yes, so simple... Thanks. Is there any way to associate this with file save?
-
Nikolai Ruhe over 14 yearsEdited to answer why workarounds are unsatisfying in this case
-
Alexander Gladysh over 14 yearsThanks. 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 over 13 yearsI'm using this plugin as well. It does what it says.
-
deepwell over 12 yearsWould be perfect, unfortunately it does not work on Xcode 4 at the moment :(
-
dmaclach over 12 yearsWe now have some basic Xcode 4 support. See our new plugin
-
jhasse about 12 yearsWhere can I find the "Correct whitespace on save" parameter?
-
Kristofer Sommestad almost 12 yearsI 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 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 almost 12 yearsNo, 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 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 almost 12 yearsI 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 over 11 yearsTo 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 over 11 yearsThis answer is obsolete as of Xcode 4.4. See the answer by Martin Stolz.
-
ma11hew28 almost 11 yearsThis 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 over 9 yearsWhy on earth is this not the default? If you have an existing file with trailing whitespace, just mark all, cut, save, paste, save.
-
Urda about 9 yearsThis needs to override the hacky script solution on this answer.
-
Alexis C. over 8 yearsUsing this +
ctrl-i
on all existing files takes care of trailing whitespaces -
Sabrina about 5 yearsthis tool dont remove blank lines.
-
Blastfurnace over 4 yearsDoes this only remove trailing spaces as requested in the question?
-
Pranav Kasetti about 3 yearsRight idea @H2zee, just need to use RegEx instead (check out my answer above).
-
IlyaEremin about 3 years@Sabrina this answer is about removing trailing spaces, not blank lines.
-
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 about 2 yearsThe 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.