Trim trailing spaces in Xcode

Is there a way to get Xcode to trim trailing spaces when saving a file?

I use version 3.1.3 if that matters.

+73
editor xcode trim developer-tools
Sep 07 '09 at 17:35
source share
6 answers

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

  • Choose Scripts> Edit Custom Scripts ...
  • Press the + button and select "New Shell" Script
  • Give it a name, such as Strip Trailing Spaces, and give it a shortcut, such as ^ ⇧R.
  • Set the "Select" and "Output" to "Replace selection"

Then enter the following script:

#!/usr/bin/perl while (<>) { s/\s+$//; print "$_\n"; } 
+20
Sep 08 '09 at 6:11
source share

Starting with Xcode 4.4, spaces will be automatically trimmed by default, unless the line is a space. You can also activate Including whitespace-only lines to fix this, which is inactive by default.

Go to Xcode > Preferences > Text Editing > While editing

Xcode preferences screenshot

+152
Aug 6 2018-12-12T00:
source share

I use the Google Toolbox for Mac Xcode Plugin , it adds the option “Fix space when saving”, which cuts off trailing spaces when saving, I skipped this from emacs.

+23
Apr 13 2018-10-12T00:
source share

This is not possible in Xcode 3.2

Edit:

I answered this question so briefly because there was no way to do it properly.

Of course, since this is software, you can do everything: starting with the hackers of Input Manager or other ways of injecting code into the system interception of the keyboard, you can change your local system to do anything at any time. You can configure the action of the Applescript folder (arrgh) or use the launch daemon and the FSEvents tool to view the source code files.

You can also add several scripts to Xcode (user scripts in the menu, script phases in the targets, user actions in the organizer, there is even a very unknown possibility to run the script), but all these solutions are erroneous, because they include user or user preferences on the user a computer.

I do not know a solution that just works after checking the project from SCM. I believe that settings are needed for this and similar scenarios, so I raised an error (radar 7203835, "Feature: more custom script triggers in the Xcode workflow"). I have not received any reviews yet.

Here is the full text of the radar record:

It would be helpful to have more places to run scripts in Xcode.

Examples:

  • Preliminary build scripts
    Pre-build scripts can be used to create prerequisites such as * .xcconfig files or config.h headers. This is not possible with the “Run script Build Phases”, because dependency tracking occurs before any build phase is started.

  • Post-build scripts As above, but it works after the build (including code signing, etc.). Useful for additional packaging, validation, etc.

  • Pre / post SCM scripts.
    To check the integrity of the project.

  • Save the Pre / Post Script file.
    To check / modify the file before saving. For example. run cody beautifiers

  • Custom project actions.
    I know about the ability of the organizer to determine arbitrary actions. But this is a user function (not part of the project). I would like to define actions, such as assembling or cleaning, that appear in the build menu and which are part of the project.

+1
07 Sep '09 at 19:22
source share

See here for Xcode4: http://www.wezm.net/technical/2011/08/strip-trailing-whitespace-xcode-4/

The cool Google tool for Mac now adds the "trim whitespace" option for Xcode4.

http://code.google.com/p/google-toolbox-for-mac/downloads/list

Thanks Google!

+1
Nov 09 '11 at 16:32
source share

For Xcode 8, I installed the swimat Xcode plug-in to format the Swift code, which removed all trailing spaces and single-line spaces.

Installation methods

Using

After installation, you can run Swimat in Xcode via Editor -> Swimat -> Format .

0
Aug 15 '17 at 13:19
source share



All Articles