Open XCode workspace or project from terminal

When working on multiple projects and modules, I had the need to open XCode from the current directory in my terminal.

I created a small shell function that did the trick for me. It will scan the current directory and checks if a xcworkspace is available to open. If not, it will open the xcodeproj. By default, it will always use the latest XCode version.

A small update I did is to handle an input parameter to open a specific version of XCode. This requires ofcourse that you downloaded some extra version of XCode with specific naming convention in mind.

The function

# Open XCode workspace or project with latest version
xc() {
  myarray=(`find ./ -maxdepth 1 -name "*.xcworkspace"`)
  if [ ${#myarray[@]} -gt 0 ]; then
    open -a "/Applications/Xcode$1.app" *.xcworkspace
  else
    open -a "/Applications/Xcode$1.app" *.xcodeproj
  fi
}

How to use?

Add this function to your shell profile

Since I use zsh, I added this to my ~/.zshrc.

To open the latest version

Just type the following command in your current directory:

xc

To open a specific version

  • Download the version you want from Apple Developer Program, for example: 11.2.1
  • Name it Xcode11.2.1
  • Move it to the Applications folder

Now we can use the same command to open the workspace or project with the XCode version of your choice. Enjoy!

xc 11.2.1