Hacking my shell prompt so I make fewer mistakes working with Xcode projects

Making mistakes is how we learn. But not all mistakes are equal, and making the same one over and over is not learning.

Often I run two or more different Xcode builds on the same machine, either because a client project can’t yet build on the latest Xcode release, or because we’re in a new Xcode beta period. Of course I forget which one I am running, especially if switching between projects multiple times in the same day. Xcode 9.4 is here today, and Xcode 10 beta is around the corner at next week’s WWDC 2018! It’s arguably the worst time of year for this problem.

Typically I end up running carthage update and get a compiler error, or a fastlane build command that then builds with an older Xcode version. The worst part is how much time this can waste before you realise what is going on. What’s particularly pernicious about this is that if you have multiple shell sessions open at once, it is easy to forget that changes to the Xcode toolchain affect other previously open sessions.

Yesterday I decided to do something about it. I’ve never customised my shell prompt before but I knew it was possible and I suspected it would be simple to show the current version of Xcode toolchain that the shell is using (which is controlled by xcode-select). The intention is that this will hopefully make me continuously aware of the Xcode version the shell is using when I run commands, and at worst if things go wrong I can immediately see why. Note that “Hey it looks like you built this with the wrong version of Xcode” is never the error you see when these problems occur.

What does it look like?

Screenshot of Terminal app showing change from Xcode 9.2 to 9.3 and git branch information.

By default it is set up to show your host and working dir, Xcode version — yep that’s the bit with the hammer 🔨 — and following that your git branch and status, if any. I don’t know about you but I often make mistakes where I am on the wrong branch too, so “sending two fascists to one prison”1 seems like a good plan.

The git parts came from a handy site called http://ezprompt.net for which I take no credit. The Xcode version parsing is done in a cool way thanks to the ever-helpful @danielpunkass who spent the time to speed up my original solution of running xcodebuild -version which took ~100ms every time on my Late 2016 MBP. He replaced it with a call to xcode-select -p and plutil to extract the bundle version from the Info.plist. This gets it down to under 40ms each time on my machine, which is great and does not affect shell usage at all… that’s ~2.5 screen updates at 60FPS so good luck typing faster than that.

To do this yourself all you need to do is add the following code to your ~/.bash_profile file, assuming you use bash. Other shells, YMMV. Let me know @marcpalmerdev if you have instructions for doing the same with other shells and I’ll update this post.

That’s it.

It seems like a small thing but we are all human. “Developer ergonomics” really matter to me. When we cut ourselves on the sharp edges of the tools we use, we should find ways to save ourselves the pain in the future. Many small changes add up to a much better experience.

  1. Me trying to avoid the unfortunate un-vegetarian colloquialism “Killing two birds with one stone”. It is sad how many of these old phrases come from brutality to animals or people, but they were different times. See Animal friendly alternatives for common phrases

Posted by

Marc Palmer (@marcpalmerdev) is a consultant and software engineer specialising in Apple platforms. He created the Flint open source framework. He writes native apps like the music practice app Soundproof for iOS devices for his company Montana Floss Co.. He can also do a pretty good job of designing products. Don't ask him to draw anything, because that's just embarrassing. You can find out more about him here.

Comments are closed.