How to launch GUI Emacs from command line in OSX?

2019-01-12 17:40发布

How do I launch GUI Emacs from the command line in OSX?

I have downloaded and installed Emacs from http://emacsformacosx.com/.

I'll accept an answer fulfilling all of the following criteria:

  1. The emacs window opens in front of my terminal window.
  2. Typing "emacs" launches a GUI Emacs window. Finding files in that window will default to looking in the directory from where I started Emacs.
  3. Typing "emacs foo.txt" when foo.txt exists launches a GUI Emacs window with foo.txt loaded.
  4. 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.

11条回答
手持菜刀,她持情操
2楼-- · 2019-01-12 18:20

On Mountain Lion, I am using Yamamoto Mitsuharu's port https://github.com/railwaycat/emacs-mac-port with the following alias:

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

and it satisfies all of your criteria.

查看更多
太酷不给撩
3楼-- · 2019-01-12 18:21

This is my script for open emacs/emacsclient on osx.

#!/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
查看更多
SAY GOODBYE
4楼-- · 2019-01-12 18:23

Compile Emacs according to the following steps:

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

And your done! It may help to download and install XQuartz, but that's just my opinion.

查看更多
Root(大扎)
5楼-- · 2019-01-12 18:24

Call the following script "emacs" and put it in your PATH somewhere:

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

That covers #2, #3, and #4.

For #1, put this somewhere in your .emacs file:

(x-focus-frame nil)

The emacsformacosx.com site 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.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-12 18:24

This improves on David Caldwell's answer by starting Emacs in the background:

#!/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).

Note that the following does not work for me -- it does not start Emacs in a directory specified on the command line (e.g. emacs .)

# NOT RECOMMENDED
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@" &
查看更多
登录 后发表回答