Domon
Domon

Reputation: 6823

How to start Emacs For Mac OS X from the command line without blocking?

A tip from the website of Emacs For Mac OS X suggests executing the following script to start it from the command line:

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

However, this will block the command line until Emacs.app exists.

Is there a way to start it from the command line without blocking?

Upvotes: 2

Views: 1509

Answers (2)

Nicholas Riley
Nicholas Riley

Reputation: 44321

Here's a shell function I've used for years. It'll start Emacs if it's not already running, otherwise it'll use the Emacs server to edit whatever file you pass to it in the existing copy of Emacs. It works in a similar way to the command-line tools for other Mac editors like BBEdit, TextMate and Sublime Text.

e () {
    (
        (( $# == 0 )) && emacsclient -ne 1 || emacsclient -n $@ >&/dev/null
    ) || (
        launch -b ~/Applications/Emacs.app && until {
                emacsclient -n $@ >&/dev/null
            }
        do
            sleep 1
        done
    )
    export EDITOR=emacsclient
}

A few notes:

If you don't have launch installed (I wrote it, so I'm a bit biased :-) you can use open -g instead of launch -b — the idea is to start Emacs up in the background until it’s ready to be used.

If you don't have the Emacs server set to start up automatically with Emacs, you can either add (server-start) to your ~/.emacs or customize server-mode.

You need to use the version of emacsclient that matches your Emacs, for example mine is ~/Applications/Emacs.app/Contents/bin/emacsclient. (I just added ~/Applications/Emacs.app/Contents/bin to my $PATH).

Upvotes: 1

Domon
Domon

Reputation: 6823

I finally got it figured out:

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

Or:

#!/bin/sh
open -a Emacs --args --chdir $PWD "$@"

The --chdir argument is necessary if we want to pass relative paths to Emacs.

I do not know if there are more idiomatic ways to do this though.

Upvotes: 7

Related Questions