94

如何从 OSX 的命令行启动 GUI Emacs?

我已经从http://emacsformacosx.com/下载并安装了 Emacs 。

我会接受满足以下所有条件的答案:

  1. emacs 窗口在我的终端窗口前面打开。
  2. 键入“emacs”会启动一个 GUI Emacs 窗口。在该窗口中查找文件将默认在我启动 Emacs 的目录中查找。
  3. 当 foo.txt 存在时键入“emacs foo.txt”会启动一个加载了 foo.txt 的 GUI Emacs 窗口。
  4. 当 foo.txt不存在时键入“emacs foo.txt”会启动一个 GUI Emacs 窗口,其中包含一个名为“foo.txt”的空文本缓冲区。在该缓冲区中执行 ^X^S 会将 foo.txt 保存在我启动 Emacs 的目录中。
4

14 回答 14

116

调用以下脚本“emacs”并将其放在您的PATH某个地方:

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

这涵盖了#2、#3 和#4。

对于 #1,将其放在 .emacs 文件中的某个位置:

(x-focus-frame nil)

emacsformacosx.com站点现在有一个How-To 页面,这是顶部代码段的来源。那里有更多关于运行emacsclient和连接 Emacs的信息git mergetool

于 2013-05-29T09:20:00.210 回答
71

在您的 shell 中,将命令“emacs”别名为指向 OSX emacs 应用程序

在我的 shell(运行默认 bash)中,我有以下内容(在我的 .bashrc 中)

alias emacs='open -a /Applications/Emacs.app $1'

然后,在命令行输入 emacs 启动 emacs 应用程序。

但是,我建议您打开一份 emacs 副本并保持其正常运行。如果是这种情况,并且您想将文件加载到现有的 emacs 副本中,则可以通过在 .bashrc 中放置以下内容来使用 emacsclient:

alias ec='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient'

然后将以下内容添加到您的 .emacs 文件中以启动 emacs 服务器(它接收 emacsclient 调用)

;;========================================
;; start the emacsserver that listens to emacsclient
(server-start)

然后你可以输入

ec .bashrc

将 .bashrc 的副本加载到现有的 emacs 会话中!

于 2012-04-16T17:31:27.530 回答
10

通过在后台启动 Emacs,这改进了David Caldwell 的回答

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

如另一个答案所述,这涵盖了#2、#3 和#4。对于 #1,将其放在 .emacs 文件中的某个位置:(x-focus-frame nil).

请注意,以下内容对我不起作用——它不会在命令行上指定的目录中启动 Emacs(例如emacs .

# NOT RECOMMENDED
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@" &
于 2014-10-25T18:17:07.247 回答
5

我假设你要么:

  • 登录时启动 emacs 守护进程
  • 在您的 .emacs 中有 (server-start)
  • 不要介意运行大量单独的 emacs 副本

如果是这样,那么我认为这满足了最初的四个标准,再加上一个:

  1. emacs 窗口在我的终端窗口前面打开。

它将始终向前景打开(使用 x-focus-frame)。

  1. 键入“emacs”会启动一个 GUI Emacs 窗口。在该窗口中查找文件将默认在我启动 Emacs 的目录中查找。

它将以 dired 模式打开现有的 emacs 窗口。

  1. 当 foo.txt 存在时键入“emacs foo.txt”会启动一个加载了 foo.txt 的 GUI Emacs 窗口。

如果 emacs 已经在运行并且有服务器,那么它将在现有窗口中打开并进入前台。

  1. 当 foo.txt 不存在时键入“emacs foo.txt”会启动一个 GUI Emacs 窗口,其中包含一个名为“foo.txt”的空文本缓冲区。在该缓冲区中执行 ^X^S 会将 foo.txt 保存在我启动 Emacs 的目录中。

正确的。

一个额外的:

输入命令后控制立即返回到终端会话。

~/bin/emacs

#!/bin/bash
EMACSPATH=/Applications/Emacs.app/Contents/MacOS

# Check if an emacs server is available 
# (by checking to see if it will evaluate a lisp statement)

if ! (${EMACSPATH}/bin/emacsclient --eval "t"  2> /dev/null > /dev/null )
then
    # There is no server available so,
    # Start Emacs.app detached from the terminal 
    # and change Emac's directory to PWD

    nohup ${EMACSPATH}/Emacs --chdir "${PWD}" "${@}" 2>&1 > /dev/null &
else
    # The emacs server is available so use emacsclient

    if [ -z "${@}" ]
    then
        # There are no arguments, so
        # tell emacs to open a new window

        ${EMACSPATH}/bin/emacsclient --eval "(list-directory \"${PWD}\")"
    else    
        # There are arguments, so
        # tell emacs to open them

        ${EMACSPATH}/bin/emacsclient --no-wait "${@}"
    fi

    # Bring emacs to the foreground

    ${EMACSPATH}/bin/emacsclient --eval "(x-focus-frame nil)"
fi
于 2016-07-08T22:50:15.650 回答
4

在 Mountain Lion 上,我使用的是 Yamamoto Mitsuharu 的端口https://github.com/railwaycat/emacs-mac-port,别名如下:

alias emacs=/Applications/Emacs.app/Contents/MacOS/Emacs

它满足您的所有标准。

于 2013-05-05T03:41:39.773 回答
4

刚刚根据本指南使用自制软件包管理器构建了 emacs: http://www.emacswiki.org/emacs/EmacsForMacOS 使用 brew install --cocoa emacs 之后应该启动 .app 版本以获取 gui,在我的情况下是/usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs

于 2014-02-17T13:58:50.313 回答
4

进一步改进大卫詹姆斯的回应,以下对我有用:

根据从http://www.emacswiki.org/emacs/EmacsForMacOS#toc20找到的终端打开文件的说明

open -a /Applications/Emacs.app <file-name>

将此与 David Jame 的响应相结合,我创建了以下emax bash 脚本并将其放置在我的路径中~/bin

#!/bin/bash
(open -a /Applications/Emacs.app "$@") &

警告:为了让 emacs 在Dired by name模式下打开当前目录,您需要使用

emax .

环境:

  • OS X 优胜美地版本 10.10.2
  • 2014-11-13 的 GNU Emacs 24.4.2 (x86_64-apple-darwin14.0.0, NS apple-appkit-1343.14)
于 2015-03-27T00:38:24.610 回答
4

简单的解决方案...

此处发布了许多非常复杂的解决此问题的方法。这是公平的,因为它看起来不平凡。

但是,这个解决方案对我来说非常有效。

ec() {
  emacsclient -n $@ 2> /dev/null
  if [[ $? == 1 ]]; then
    open -a Emacs.app  -- $@
  fi
}

用法

ec file [...]

让我们解开正在发生的事情:

  1. 将所有ec参数传递给 emacsclient 并且不要 ( -n) 在继续之前等待 emacs。
    1. 如果 Emacs 已经在运行,那么我们就完成了,您正在编辑。
  2. 当没有 emacs 运行时,吞下 emacsclient 发布的错误消息。( 2> /dev/null)
  3. 手动处理退出代码1( [[ $? == 1 ]])
    1. 打开 Emacs.app 并将文件参数传递给它(路径将被正确打开。)
    2. 你已经完成了,Emacs 已经打开了你的文件。
于 2018-04-05T08:10:39.457 回答
1

这里的其他答案对我来说不太适用。特别是,在我的机器上,bash 脚本

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

总是在主目录中打开 emacs。要让它在当前工作目录中打开,我必须这样做

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

反而。

于 2015-05-22T16:13:33.867 回答
0

按照以下步骤编译 Emacs:

./configure --with-x --prefix=/usr
make
sudo make install

你完成了!下载和安装 XQuartz 可能会有所帮助,但这只是我的意见。

于 2013-08-20T17:21:54.607 回答
0

这是我在 osx 上打开 emacs/emacsclient 的脚本。

#!/bin/bash

# Ensure (server-start) is added in your emacs init script.

EMACS=/Applications/MacPorts/Emacs.app/Contents/MacOS/Emacs
EMACSCLIENT=/Applications/Macports/Emacs.app/\
Contents/MacOS/bin/emacsclient

# test if client already exsit.
$EMACSCLIENT -e "(frames-on-display-list)" &>/dev/null

# use emacsclient to connect existing server.
if [ $? -eq 0 ]; then
    $EMACSCLIENT -n "$@"
# open emacs.app instead.
else
    `$EMACS "$@"` &
fi
于 2017-12-04T03:34:55.047 回答
0

在以上所有使用“open”时 - 确保使用“--args”选项

不要这样做:

alias emacs='open -a /Applications/Emacs.app $1'

取而代之的是:

alias emacs='open -a /Applications/Emacs.app --args $1'

--args选项可防止“打开”使用用于 Emacs 的各种选项。

于 2019-12-25T22:09:44.653 回答
0

最佳答案很好,但我希望 emacs 进程在后台运行,这样我仍然可以使用我的 shell。这个答案似乎做了我想要的,但没有在正确的目录中启动 emacs,这意味着需要绝对路径(或者 hack 附加pwd到在所有情况下都不起作用的路径)。此外,简单地使用&意味着如果我杀死终端,emacs 也会被杀死。

我决定使用screenbash 函数,以下解决方案适用于我安装的 macOS 10.15.6 和 emacs 26.2 brew

function emacs() {
    screen -d -m /Applications/Emacs.app/Contents/MacOS/Emacs "$@"
}

对于-d -m命令行标志的含义,它们一起使用时具有特殊的含义,因此本质上可以认为是一个命令行标志。解释在手册页中:

以“分离”模式启动屏幕。这会创建一个新会话,但不会附加到它。这对于系统启动脚本很有用。

于 2020-09-11T23:55:37.217 回答
0
open_emacs() {
    num=$(ps aux | grep -E "[E]macs-x86_64-10_14 --|[e]macs --" | wc -l)
    if [ $num -eq 0 ]; then
        echo "## starting emacs"
        # Run in a subshell to remove notifications and close STDOUT and STDERR:
        (&>/dev/null emacsclient -t -q &)
    fi
}

alias e="open_emacs"

(&>/dev/null emacsclient -t -q &)如果 emacs 守护程序不在后台运行,以下行将启动它。

  • EmacOS 可能已经定义了以(ex: )开头的应用程序名称Emacs-x86_64-10_14.app,基于此您可以检查守护程序是否在emacs后台运行。
于 2021-01-24T10:40:59.350 回答