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 17:58

The other answers here didn't quite work for me. In particular, on my machine, the bash script

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

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

instead.

查看更多
来,给爷笑一个
3楼-- · 2019-01-12 17:59

Just built emacs with homebrew package manager according to this guide: http://www.emacswiki.org/emacs/EmacsForMacOS with 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

查看更多
趁早两清
4楼-- · 2019-01-12 18:02

In your shell, alias the command 'emacs' to point to the OSX emacs application

In my shell (running the default bash), I have the following (in my .bashrc)

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

Then, typing emacs on the command line starts the emacs application.

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:

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)

;;========================================
;; 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!

查看更多
叛逆
5楼-- · 2019-01-12 18:03

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

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

combining this with David Jame's response I've created the following emax bash script and placed it in my path at ~/bin

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

Caveat: in order to get emacs to open the current directory in Dired by name mode, you need to use

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
查看更多
萌系小妹纸
6楼-- · 2019-01-12 18:16

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

If so, then I think this satisfies the original four criteria, plus one more:

  1. The emacs window opens in front of my terminal window.

it will always open to the foreground (with x-focus-frame).

  1. Typing "emacs" launches a GUI Emacs window. Finding files in that window will default to looking in the directory from where I started Emacs.

It will open an existing emacs window in dired mode.

  1. Typing "emacs foo.txt" when foo.txt exists launches a GUI Emacs window with foo.txt loaded.

If emacs is already running and has a server, then it will open in the existing window and come to the foreground.

  1. 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.

Correct.

One extra:

Control returns to the terminal session immediately after typing the command.

~/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
查看更多
孤傲高冷的网名
7楼-- · 2019-01-12 18:17

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:

  1. pass all the ec arguments to emacsclient and don't (-n) wait for emacs before continuing.
    1. If Emacs is already running, we're all done and you're editing.
  2. swallow up the error message posted by emacsclient when there's no emacs running. (2> /dev/null)
  3. Manually handle the exit code 1 ([[ $? == 1 ]])
    1. open Emacs.app and pass file arguments to it (paths will be correctly opened.)
    2. You're all done, and Emacs has opened your files.
查看更多
登录 后发表回答