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

Joh*_*les 86 macos emacs command-line

如何从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. 键入"emacs的foo.txt的"当foo.txt的确实存在与推出名为"foo.txt的"空文本缓冲的GUI Emacs窗口.在该缓冲区中执行^ X ^ S会将foo.txt保存在我启动Emacs的目录中.

Dav*_*ell 111

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

#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@"
Run Code Online (Sandbox Code Playgroud)

这包括#2,#3和#4.

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

(x-focus-frame nil)
Run Code Online (Sandbox Code Playgroud)

emacsformacosx.com网站现在有一个操作方法页,这哪里是顶片段的来源.有关于运行emacsclient和挂钩Emacs 的更多信息git mergetool.

  • 在Mavericks上如果我在文件夹`〜/ foo`并想要打开`bar.txt`我输入`gmacs bar.txt`但它在`〜/ bar.txt`创建一个新文件而不是打开`~foo/bar.txt`.难道我做错了什么? (2认同)
  • .emacs文件在哪里? (2认同)

Chr*_*han 70

在shell中,将命令"emacs"别名为OSX emacs应用程序

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

alias emacs='open -a /Applications/Emacs.app $1'
Run Code Online (Sandbox Code Playgroud)

然后,在命令行上键入emacs将启动emacs应用程序.

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

alias ec='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient'
Run Code Online (Sandbox Code Playgroud)

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

;;========================================
;; start the emacsserver that listens to emacsclient
(server-start)
Run Code Online (Sandbox Code Playgroud)

然后你可以输入

ec .bashrc
Run Code Online (Sandbox Code Playgroud)

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

  • 一个小小的机智在回答问题方面有很长的路要走.谷歌搜索也是如此. (5认同)
  • 嗨克里斯!您的第一个答案未通过标准2和4.您的第二个答案未通过标准2.我正在寻找满足所有编号标准的解决方案.问候/约翰 (4认同)
  • @tylerthemiler,完全解决问题的答案在哪里?这个答案中的两个答案都不符合标准2,第一个答案也失败了4. (3认同)
  • 如果您仅仅使用emacs本身来加载文件,而不是每次都依赖命令行来启动新实例,那么您的所有问题似乎都可以解决.我通常在一天开始时启动emacs(如果它还没有运行),并使用它来加载文件,更改目录,创建新文件以及我想要的任何其他内容,而无需触摸终端窗口. (2认同)

Dav*_* J. 8

通过在后台启动Emacs,这改善了David Caldwell的答案:

#!/bin/sh
$(/Applications/Emacs.app/Contents/MacOS/Emacs "$@") &
Run Code Online (Sandbox Code Playgroud)

如其他答案所述,这涵盖#2,#3和#4.对于#1,将它放在.emacs文件中:(x-focus-frame nil).

请注意,下面就不是为我工作-它不会在命令行上指定的目录启动Emacs(例如emacs .)

# NOT RECOMMENDED
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@" &
Run Code Online (Sandbox Code Playgroud)


小智 5

我假设你要么:

  • 登录时启动emacs守护程序
  • 在.emacs中有(服务器启动)
  • 不介意有很多单独的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的目录中.

正确.

一个额外的:

键入命令后,控制立即返回终端会话.

〜/斌/ 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
Run Code Online (Sandbox Code Playgroud)