天天看點

Mac下終端(Terminal)中打開某應用的技巧

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.