Mac下一般都是点击某一个应用的图标,或者在Alfred中键入应用名称打开该应用。
但有时候在终端执行某些代码,此时想打开程序,以上操作都不流畅,所以琢磨能否在终端下直接打开某些程序。比如要 编辑路径下的shell脚本文件,但又不想使用vim这样的软件进行编辑,如果在命令行中直接使用sublime 或者 pycharm 的软件直接打开,将会变得非常流畅。
以上问题的解决方案
最简单的用法:open -a ApplicationName
,将直接打开“ApplicationName”的这个应用。1
open命令还有很多其他高级用法,eg:命令行中编辑路径下的sh文件,可以采用
open -a Sublime\ Text check_file.sh
的方法直接打开Sublime编辑shell脚本,编辑完成还能在终端中马上执行。
如果想以root权限编辑sh脚本,可以在上诉语句前插入sudo
命令即可:sudo open -a Sublime\ Text check_file.sh
。
同样这样的方法对于打开python、c等文件都非常方便,open -a PyCharm show.py
将直接使用pycharm打开该文件了,不用再到pycharm中一级一级目录的找。
另外,这里的文件可以是绝对路径,获取绝对路径可以通过cmd + option + c
快捷键惊醒获取,再结合刚才的方式,效率大大的提升,也不会让自己思绪被打断。
另外一个技巧,可以在shell配置文件2 ( ~/.zshrc
或者 ~/.bashrc
)中指定别名,即可实现在命令行中直接打开应用。eg:~/.zshrc
中插入alias pycharm="open -a PyCharm"
语句,就可以在终端中键入pycharm打开PyCharm应用,或者pycharm filename
直接打开某一个文件
下面是一些open的其他用法:
Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-s <partial SDK name>][-b <bundle identifier>] [-a <application>] [filenames] [--args arguments]
Help: Open opens files from a shell.
By default, opens each file using the default application for that file.
If the file is in the form of a URL, the file will be opened as a URL.
Options:
-a Opens with the specified application.
-b Opens with the specified application bundle identifier.
-e Opens with TextEdit.
-t Opens with default text editor.
-f Reads input from standard input and opens with TextEdit.
-F --fresh Launches the app fresh, that is, without restoring windows. Saved persistent state is lost, excluding Untitled documents.
-R, --reveal Selects in the Finder instead of opening.
-W, --wait-apps Blocks until the used applications are closed (even if they were already running).
--args All remaining arguments are passed in argv to the application's main() function instead of opened.
-n, --new Open a new instance of the application even if one is already running.
-j, --hide Launches the app hidden.
-g, --background Does not bring the application to the foreground.
-h, --header Searches header file locations for headers matching the given filenames, and opens them.
-s For -h, the SDK to use; if supplied, only SDKs whose names contain the argument value are searched.
Otherwise the highest versioned SDK in each platform is used.
这里需要根据自己使用的shell进行修改,我用的zsh,所以这里修改
~/.zshrc
文件 ↩︎