It's all about pro[...]

I was fighting trailing whitespaces in my code with Visual Studio. I’m fighting them in Xcode.

And it’s not only me bothered: CodingHorror shows the value of whitespace. This post gave me an idea how to solve the problem.

Here is what we need:

1. Regular expression: [ \t]+$.

2. Command-line tool: sed ‘s/[ ^I]*$//’.

(it turns out that ‘\t’ works inappropriate in sed, so ‘^I’ is used as the same thing)

Next, we can use User Script to launch our small code in Xcode. Go to the user scripts list through the menu:

Create a new one by clicking “+”, selecting “New Shell Script” and putting script code as following:


echo -n "%%%{PBXSelection}%%%"
sed 's/[ ^I]*$//' <&0
echo -n "%%%{PBXSelection}%%%"

Set up a hotkey to make this automation handy. You can also change Input and Output to “Entire Document” and “Replace Document Contents” to work not only for selected text.

Note: This approach is still in testing so use it on your own risk.

And as always reading Coding Horror is a pleasure because of strings like this precise observations on programmers and their job:

[…] what it’s like to spend every freaking minute of every freaking day agonizing over the tiniest details of the programs you write. Not because we want to, you understand, but because the world explodes when we don’t.

P.S. I tried to make a complete automation by using Build Phases and Build Rules to launch similar script for changed files but I didn’t find any suitable approach. So I’m still in search of it.

Blog comments powered by Disqus