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.

  1. How to Launch GUI Applications from the Terminal ↩︎

  2. 这里需要根据自己使用的shell进行修改,我用的zsh,所以这里修改~/.zshrc 文件 ↩︎

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值