如何从 OSX 的命令行启动 GUI Emacs?
我从 http://emacsformacosx.com/ 下载并安装了 Emacs。
我会接受满足以下所有条件的答案:
emacs 窗口在我的终端窗口前面打开。键入“emacs”会启动一个 GUI Emacs 窗口。在该窗口中查找文件将默认在我启动 Emacs 的目录中查找。当 foo.txt 存在时键入“emacs foo.txt”会启动一个加载了 foo.txt 的 GUI Emacs 窗口。当 foo.txt 不存在时键入“emacs foo.txt”会启动一个 GUI Emacs 窗口,其中包含一个名为“foo.txt”的空文本缓冲区。在该缓冲区中执行 ^X^S 会将 foo.txt 保存在我启动 Emacs 的目录中。
emacsclient -c -a "" "$@"
command
调用以下脚本“emacs”并将其放入您的 PATH
某处:
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@"
这涵盖了#2、#3 和#4。
对于 #1,将其放在 .emacs 文件中的某个位置:
(x-focus-frame nil)
emacsformacosx.com 网站现在有一个 How-To page,这是顶部代码段的来源。那里有关于运行 emacsclient
和将 Emacs 挂接到 git mergetool
的更多信息。
在您的 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 会话中!
这通过在后台启动 Emacs 对 David Caldwell's answer 进行了改进:
#!/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 "$@" &
我假设你要么:
登录时启动 emacs 守护进程
在您的 .emacs 中有 (server-start)
不要介意运行大量单独的 emacs 副本
如果是这样,那么我认为这满足了最初的四个标准,再加上一个:
emacs 窗口在我的终端窗口前面打开。
它将始终向前景打开(使用 x-focus-frame)。
键入“emacs”会启动一个 GUI Emacs 窗口。在该窗口中查找文件将默认在我启动 Emacs 的目录中查找。
它将以 dired 模式打开现有的 emacs 窗口。
当 foo.txt 存在时键入“emacs foo.txt”会启动一个加载了 foo.txt 的 GUI Emacs 窗口。
如果 emacs 已经在运行并且有服务器,那么它将在现有窗口中打开并进入前台。
当 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
在 Mountain Lion 上,我使用 Yamamoto Mitsuharu 的端口 https://github.com/railwaycat/emacs-mac-port,别名如下:
alias emacs=/Applications/Emacs.app/Contents/MacOS/Emacs
它满足您的所有标准。
刚刚根据本指南使用自制软件包管理器构建了 emacs:http://www.emacswiki.org/emacs/EmacsForMacOS 使用 brew install --cocoa emacs 之后应该启动 .app 版本以获取 gui,在我的情况下是 /usr/local/Cellar/emacs/24.3 /Emacs.app/Contents/MacOS/Emacs
进一步改进大卫詹姆斯的回应,以下对我有用:
根据从位于 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)
简单的解决方案...
此处发布了许多非常复杂的解决此问题的方法。这是公平的,因为它看起来不平凡。
但是,这个解决方案对我来说非常有效。
ec() {
emacsclient -n $@ 2> /dev/null
if [[ $? == 1 ]]; then
open -a Emacs.app -- $@
fi
}
用法
ec file [...]
让我们解开正在发生的事情:
将所有 ec 参数传递给 emacsclient 并且在继续之前不要(-n)等待 emacs。如果 Emacs 已经在运行,那么我们就完成了,您正在编辑。当没有 emacs 运行时,吞下 emacsclient 发布的错误消息。 (2> /dev/null) 手动处理退出代码 1 ([[ $? == 1 ]]) 打开 Emacs.app 并将文件参数传递给它(路径将被正确打开。)你已经完成了,并且Emacs 已打开您的文件。
这里的其他答案对我来说不太适用。特别是,在我的机器上,bash 脚本
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@"
总是在主目录中打开 emacs。要让它在当前工作目录中打开,我必须这样做
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$PWD/$@"
反而。
按照以下步骤编译 Emacs:
./configure --with-x --prefix=/usr
make
sudo make install
你完成了!下载和安装 XQuartz 可能会有所帮助,但这只是我的意见。
这是我在 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
在以上所有使用“open”时 - 确保使用“--args”选项
不要这样做:
alias emacs='open -a /Applications/Emacs.app $1'
取而代之的是:
alias emacs='open -a /Applications/Emacs.app --args $1'
--args
选项可防止“打开”使用用于 Emacs 的各种选项。
top answer 很好,但我希望 emacs 进程在后台运行,这样我仍然可以使用我的 shell。 This answer 似乎做了我想做的事,但没有在正确的目录中启动 emacs,这意味着需要绝对路径(或者将 pwd
附加到在所有情况下都不起作用的路径上)。此外,简单地使用 &
意味着如果我杀死终端,emacs 也会被杀死。
我决定使用 screen
和 bash 函数,以下解决方案适用于安装了 brew
的 macOS 10.15.6 和 emacs 26.2:
function emacs() {
screen -d -m /Applications/Emacs.app/Contents/MacOS/Emacs "$@"
}
对于 -d -m
命令行标志的含义,它们一起使用时具有特殊含义,因此基本上可以将其视为一个命令行标志。解释在手册页中:
以“分离”模式启动屏幕。这会创建一个新会话,但不会附加到它。这对于系统启动脚本很有用。
screen
把我吓跑了,现在我更仔细地阅读,我意识到你是对的。我很抱歉@gsingh2011。
screen -d -m
对这种情况的影响?
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"
如果 emacs 守护程序不在后台运行,则以下行 (&>/dev/null emacsclient -t -q &)
将启动它。
macOS 可能已经定义了以 E 开头的应用程序名称(例如:Emacs-x86_64-10_14.app),基于此您可以检查 emacs 守护程序是否在后台运行。
~/foo
中并想要打开bar.txt
,我键入gmacs bar.txt
,但它会在~/bar.txt
处创建一个新文件,而不是打开~foo/bar.txt
。难道我做错了什么?