macos 如何在 OSX 中从命令行启动 GUI Emacs?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10171280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to launch GUI Emacs from command line in OSX?
提问by Johan Walles
How do I launch GUI Emacs from the command line in OSX?
如何从 OSX 中的命令行启动 GUI Emacs?
I have downloaded and installed Emacs from http://emacsformacosx.com/.
我已经从http://emacsformacosx.com/下载并安装了 Emacs 。
I'll accept an answer fulfilling all of the following criteria:
我会接受满足以下所有条件的答案:
- The emacs window opens in front ofmy terminal window.
- Typing "emacs" launches a GUI Emacs window. Finding files in that window will default to looking in the directory from where I started Emacs.
- Typing "emacs foo.txt" when foo.txt exists launches a GUI Emacs window with foo.txt loaded.
- Typing "emacs foo.txt" when foo.txt does notexist launches a GUI Emacs window with an empty text buffer named "foo.txt". Doing ^X^S in that buffer will save foo.txt in the directory from where I started Emacs.
- emacs 窗口在我的终端窗口前面打开。
- 键入“emacs”会启动一个 GUI Emacs 窗口。在该窗口中查找文件将默认在我启动 Emacs 的目录中查找。
- 当 foo.txt 存在时输入“emacs foo.txt”会启动一个加载了 foo.txt 的 GUI Emacs 窗口。
- 键入“emacs的foo.txt的”当foo.txt的确实不存在启动与空文本GUI的Emacs窗口缓冲区名为“foo.txt的”。在该缓冲区中执行 ^X^S 会将 foo.txt 保存在我启动 Emacs 的目录中。
回答by David Caldwell
Call the following script "emacs" and put it in your PATH
somewhere:
调用以下脚本“emacs”并将其放在您的PATH
某个位置:
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@"
That covers #2, #3, and #4.
这涵盖了 #2、#3 和 #4。
For #1, put this somewhere in your .emacs file:
对于 #1,把它放在你的 .emacs 文件中:
(x-focus-frame nil)
The emacsformacosx.comsite now has a How-To page, which is where the top snippet came from. There's more info there about running emacsclient
and hooking Emacs up to git mergetool
.
该emacsformacosx.com网站现在有一个操作方法页,这哪里是顶片段的来源。那里有更多关于运行emacsclient
和连接 Emacs 的信息git mergetool
。
回答by Chris McMahan
In your shell, alias the command 'emacs' to point to the OSX emacs application
在您的 shell 中,将命令“emacs”作为别名以指向 OSX emacs 应用程序
In my shell (running the default bash), I have the following (in my .bashrc)
在我的 shell(运行默认 bash)中,我有以下内容(在我的 .bashrc 中)
alias emacs='open -a /Applications/Emacs.app '
Then, typing emacs on the command line starts the emacs application.
然后,在命令行上输入 emacs 启动 emacs 应用程序。
I would, however, recommend that you open a copy of emacs and just keep it up and running. If that's the case, and you want to load a file into an existing copy of emacs, you can use the emacsclient by placing the following in your .bashrc:
但是,我建议您打开 emacs 的副本并保持它正常运行。如果是这种情况,并且您想将文件加载到 emacs 的现有副本中,则可以通过在 .bashrc 中放置以下内容来使用 emacsclient:
alias ec='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient'
Then add the following to your .emacs file to start the emacs server (which receives the emacsclient calls)
然后将以下内容添加到您的 .emacs 文件中以启动 emacs 服务器(接收 emacsclient 调用)
;;========================================
;; start the emacsserver that listens to emacsclient
(server-start)
Then you can type
然后你可以输入
ec .bashrc
to load a copy of .bashrc into an existing emacs session!
将 .bashrc 的副本加载到现有的 emacs 会话中!
回答by David J.
This improves on David Caldwell's answerby starting Emacs in the background:
这通过在后台启动 Emacs改进了David Caldwell 的回答:
#!/bin/sh
$(/Applications/Emacs.app/Contents/MacOS/Emacs "$@") &
As stated in the other answer, this covers #2, #3, and #4. For #1, put this somewhere in your .emacs file: (x-focus-frame nil)
.
正如另一个答案中所述,这涵盖了 #2、#3 和 #4。对于1号,把这个地方你.emacs文件:(x-focus-frame nil)
。
Note that the following does notwork for me -- it does not start Emacs in a directory specified on the command line (e.g. emacs .
)
请注意,以下就不是对我来说有效-它不会在命令行上指定的目录启动Emacs(例如emacs .
)
# NOT RECOMMENDED
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@" &
回答by The Dave
I assume you either:
我假设你要么:
- Start the emacs daemon on login
- Have (server-start) in your .emacs
- Don't mind having lots of separate copies of emacs running
- 登录时启动 emacs 守护进程
- 在您的 .emacs 中有 (server-start)
- 不要介意运行大量独立的 emacs 副本
If so, then I think this satisfies the original four criteria, plus one more:
如果是这样,那么我认为这满足最初的四个标准,再加上一个:
- The emacs window opens in front of my terminal window.
- emacs 窗口在我的终端窗口前面打开。
it will always open to the foreground (with x-focus-frame).
它将始终向前景打开(使用 x-focus-frame)。
- Typing "emacs" launches a GUI Emacs window. Finding files in that window will default to looking in the directory from where I started Emacs.
- 键入“emacs”会启动一个 GUI Emacs 窗口。在该窗口中查找文件将默认在我启动 Emacs 的目录中查找。
It will open an existing emacs window in dired mode.
它将以直接模式打开现有的 emacs 窗口。
- Typing "emacs foo.txt" when foo.txt exists launches a GUI Emacs window with foo.txt loaded.
- 当 foo.txt 存在时输入“emacs foo.txt”会启动一个加载了 foo.txt 的 GUI Emacs 窗口。
If emacs is already running and has a server, then it will open in the existing window and come to the foreground.
如果 emacs 已经在运行并且有一个服务器,那么它将在现有窗口中打开并出现在前台。
- Typing "emacs foo.txt" when foo.txt does not exist launches a GUI Emacs window with an empty text buffer named "foo.txt". Doing ^X^S in that buffer will save foo.txt in the directory from where I started Emacs.
- 当 foo.txt 不存在时键入“emacs foo.txt”会启动一个带有名为“foo.txt”的空文本缓冲区的 GUI Emacs 窗口。在该缓冲区中执行 ^X^S 会将 foo.txt 保存在我启动 Emacs 的目录中。
Correct.
正确的。
One extra:
额外的一项:
Control returns to the terminal session immediately after typing the command.
输入命令后,控制立即返回到终端会话。
~/bin/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
回答by mforbes
On Mountain Lion, I am using Yamamoto Mitsuharu's port https://github.com/railwaycat/emacs-mac-portwith the following alias:
在 Mountain Lion 上,我使用的是 Yamamoto Mitsuharu 的端口https://github.com/railwaycat/emacs-mac-port,别名如下:
alias emacs=/Applications/Emacs.app/Contents/MacOS/Emacs
and it satisfies all of your criteria.
它满足您的所有标准。
回答by ocodo
Simple solution...
简单的解决方案...
A lot of very complex solutions to this problem are posted here. That's fair because it seems non-trivial.
这个问题的很多非常复杂的解决方案都张贴在这里。这很公平,因为它看起来很重要。
However, this solution works really well for me.
但是,这个解决方案对我来说非常有效。
ec() {
emacsclient -n $@ 2> /dev/null
if [[ $? == 1 ]]; then
open -a Emacs.app -- $@
fi
}
Usage
用法
ec file [...]
Let's unpack what's happening:
让我们解开正在发生的事情:
- pass all the
ec
arguments to emacsclient and don't (-n
) wait for emacs before continuing.- If Emacs is already running, we're all done and you're editing.
- swallow up the error message posted by emacsclient when there's no emacs running. (
2> /dev/null
) - Manually handle the exit code
1
([[ $? == 1 ]]
)- open Emacs.app and pass file arguments to it (paths will be correctly opened.)
- You're all done, and Emacs has opened your files.
- 将所有
ec
参数传递给 emacsclient 并且-n
在继续之前不要 ( ) 等待 emacs。- 如果 Emacs 已经在运行,我们就完成了,您正在编辑。
- 当没有 emacs 运行时,吞下 emacsclient 发布的错误消息。(
2> /dev/null
) - 手动处理退出代码
1
([[ $? == 1 ]]
)- 打开 Emacs.app 并将文件参数传递给它(路径将被正确打开。)
- 大功告成,Emacs 已经打开了您的文件。
回答by Yuriy Skvortsov
Just built emacs with homebrew package manager according to this guide: http://www.emacswiki.org/emacs/EmacsForMacOSwith brew install --cocoa emacs After that one should launch the .app version to get gui, which in my case was /usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs
刚刚根据本指南使用自制软件包管理器构建了 emacs:http: //www.emacswiki.org/emacs/EmacsForMacOS with brew install --cocoa emacs 之后应该启动 .app 版本以获取 gui,在我的情况下是/usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs
回答by sbecker11
Further improving on David James' response the following works for me:
进一步改进大卫詹姆斯的回答对我有用:
Per instructions to open a file from a terminal found at http://www.emacswiki.org/emacs/EmacsForMacOS#toc20
根据从位于http://www.emacswiki.org/emacs/EmacsForMacOS#toc20的终端打开文件的说明
open -a /Applications/Emacs.app <file-name>
combining this with David Jame's response I've created the following emaxbash script and placed it in my path at ~/bin
将此与 David Jame 的响应相结合,我创建了以下emaxbash 脚本并将其放置在我的路径中~/bin
#!/bin/bash
(open -a /Applications/Emacs.app "$@") &
Caveat: in order to get emacs to open the current directory in Dired by namemode, you need to use
警告:为了让 emacs 在Dired by name模式下打开当前目录,你需要使用
emax .
Environment:
环境:
- OS X Yosemite Version 10.10.2
- GNU Emacs 24.4.2 (x86_64-apple-darwin14.0.0, NS apple-appkit-1343.14) of 2014-11-13
- OS X 优胜美地版本 10.10.2
- GNU Emacs 24.4.2 (x86_64-apple-darwin14.0.0, NS apple-appkit-1343.14) of 2014-11-13
回答by Hymankamm
The other answers here didn't quite work for me. In particular, on my machine, the bash script
这里的其他答案对我来说不太适用。特别是在我的机器上,bash 脚本
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@"
always opens emacs in the home directory. To get it to open in the current working directory, I had to do
总是在主目录中打开 emacs。要让它在当前工作目录中打开,我必须这样做
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$PWD/$@"
instead.
反而。
回答by Koren.H
This is my script for open emacs/emacsclient on osx.
这是我在 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