如何从终端打开 Xcode?

我注意到我当前的 bash 文件有 < code > export PATH = $PATH:/Applications/MAMP/library/bin 我放在那里是为了建立对 Mamp 的终端访问。我一直在尝试编译 -a MyApp a.xcodeprojopen a.xcodeproj,但我不知道该使用哪一个,两者都不起作用。在建议 $ alias xcode="open -a Xcode"之后,我还把这个添加到 bash 文件中,它仍然不起作用。我需要一个路径/终端专家来帮助我配置一种方法,从终端运行 Xcode,因为我正在尝试使用可可豆荚。

95891 次浏览

I think your current directory is wrong. Move to the directory which contains MyApp.xcodeproj file.

If open .xcodeproj doesn't work, then you can use the following to force Xcode to open via terminal.

Step 1.

Open Terminal. I am assuming you know how to do this, because your question was how to open Xcode in the terminal.

Step 2.

Type the following line in terminal. This will open your .bash_profile with vim (a terminal text editor). The ~/ means that it will open it in your home directory. So your current location doesn't matter.

vim ~/.bash_profile

Step 3.

When using vim you will need to go into insert mode, which basically means you can start typing into the file. To do this you will just need to hit the i key.

i      // <- this will get you into insert mode

Step 4.

Then type the following on it's own line in .bash_profile. This tells bash, to set an alias up, the alias's name will be xcode, and the alias value will be open -a Xcode. Make sure you do not have any spaces on the left or right of the equals sign (=).

alias xcode="open -a Xcode"

Step 5.

Since we went into insert mode by using the i key, you need to hit the ESC to exit insert mode. then hit the :wqreturn key to escape, write, and quit.

ESC    // <- this will exit insert mode
:wq    // <- writes and quit the file

Step 6.

This will need to reload your bash profile in bash, after making changes to it. The . will basically run your .bash_profile again.

. ~/.bash_profile

Step 7.

Using the alias.

Make sure you are in the same directory as the name.xcodeproj, check this by using ls. If you see it do the following:

xcode name.xcodeproj

obviously you want to replace name with the file name

You are in wrong directory. Consider 'a' folder on desktop that contains a.xcodeproj and other files. Navigate to 'a' directory in terminal.

MACBOOK-Users: macbook$ cd Users/macbook/Desktop/a

Now, macbook$ open a.xcodeproj on terminal. This opens 'a' project in Xcode.

Old thread, but I just recently researched if there's a way to open Xcode from the terminal myself, and was not satisfied when discovering the overly verbose $ open -a Xcode projname.xcodeproj command. You could alias half the command like Arian Faurtosh's answer, but if you're going to edit a bash script, a function can serve you much better.

My solution:

# Function to open Xcode projects from the command line, call with $ xcode
function xcode {


proj=$(ls -d *.xcodeproj/ 2>/dev/null)


if [ -n "$proj" ]; then
# Omit -beta if you're not using beta version
open -a Xcode-beta "$proj"
else
echo "No Xcode project detected."
fi


}

Save above code to whatever file your shell sources each session. Now you can use $ xcode and it will launch Xcode as long as your current directory contains a .xcodeproj dir.

xed does this and ships with xcode. Run

xed .

man xed for more info.

Very simple:

Get into the directory of the project; you can tell if your in the proper directory by typing "ls" (short for 'list') into terminal and if you see the .xcodeproj suffix on your project name then you're in the right spot.

open projectname.xcodeproj

The project will then open into Xcode

Shortcut: on a macbook you can many times type just the first letter or two of your projectname and if you hit 'tab' it will autocomplete it. So you could type the above code like this...

open pr[tab] then you'd see... open projectname then you'd type... open projectname.xc[tab] and it would finish that too to end up like this... open projectname.xcodeproj

Run this in your terminal:

echo "alias xca='cd ~/path/to/project/directory && xed .'" >> ~/.zshrc
source ~/.zshrc

Open a new terminal and run:

xca

Your terminal will take you to your project directory (useful for committing code changes) and it will open your project

Open specific Xcode

  1. Choose specific Xcode version you want to open with.
  2. Copy the Xcode developer directory filepath, e.g. /Applications/Xcode-13.2.1.app/Contents/Developer.
  3. Run xcode-select -s with the filepath. e.g. sudo xcode-select -s /Applications/Xcode-13.2.1.app/Contents/Developer
  4. Navigate to folder of Xcode project using cd, or alternatively copy it directly to step 5.
  5. xed . / open *.xcodeproj

Et voila! With this approach we can open projects using multiple versions of Xcode from the command line.

I prefer using xed rather than open because the tool is the "Xcode Editor" tool and has been specifically designed to open Xcode projects. It will choose the xcworkspace over xcodeproj automatically and open package projects too. :D