如何在不阻塞的情况下从命令行启动 Emacs For Mac OS X?

How to start Emacs For Mac OS X from the command line without blocking?

A tip 来自 Emacs 的网站 For Mac OS X 建议执行以下脚本以从命令行启动它:

#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@"

但是,这将阻止命令行,直到 Emacs.app 存在。

有没有办法在不阻塞的情况下从命令行启动它?

我终于明白了:

#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs --chdir $PWD "$@" &

或者:

#!/bin/sh
open -a Emacs --args --chdir $PWD "$@"

如果我们想将相对路径传递给 Emacs,--chdir 参数是必需的。

虽然我不知道是否有更多惯用的方法来做到这一点。

这是我使用多年的 shell 函数。如果 Emacs 尚未 运行,它将启动 Emacs,否则它将使用 Emacs 服务器编辑您在现有 Emacs 副本中传递给它的任何文件。它的工作方式与其他 Mac 编辑器(如 BBEdit、TextMate 和 Sublime Text)的命令行工具类似。

e () {
    (
        (( $# == 0 )) && emacsclient -ne 1 || emacsclient -n $@ >&/dev/null
    ) || (
        launch -b ~/Applications/Emacs.app && until {
                emacsclient -n $@ >&/dev/null
            }
        do
            sleep 1
        done
    )
    export EDITOR=emacsclient
}

一些注意事项:

如果你没有安装 launch(我写的,所以我有点偏见 :-) 你可以使用 open -g 而不是 launch -b — 这个想法是在后台启动 Emacs,直到它可以使用。

如果您没有将 Emacs 服务器设置为随 Emacs 自动启动,您可以将 (server-start) 添加到 ~/.emacs 或自定义 server-mode.

您需要使用与您的 Emacs 匹配的 emacsclient 版本,例如我的是 ~/Applications/Emacs.app/Contents/bin/emacsclient。 (我刚刚将 ~/Applications/Emacs.app/Contents/bin 添加到我的 $PATH)。