4

When I run Emacs on macOS, it runs a new instance of the app. Is there a way that I can only ever run one instance?

If I double-click on the Emacs app icon (for example), and it's already running, it would just bring the existing instance to the front.

I'm on macOS 10.13.1 and Emacs 25.2 (9.0).

6
  • Are you calling Emacs from the command line and that is why you have more than one instance? What is wrong with using the Emacs.app icon to pull up an existing instance, or launch a new one if there is no Emacs process running already?
    – lawlist
    Nov 29, 2017 at 4:45
  • Either when I enter emacs on the command line, or double-click on the Emacs icon in Finder, it happens. There is nothing wrong with opening two (or more copies), but keeping with how all other apps work on the Mac, opening one instance of apps is the norm. Nov 29, 2017 at 5:03
  • 1
    The pre-built binary from emacsformacosx.com should bring to the foreground the existing Emacs instance if you click-on it in the Finder or use Command+down. Same thing if Emacs has been added/dragged to the dock and you type Control-F3 to place focus to the dock, and type Emacs quickly, and then press the enter key. If you build Emacs from source using the --with-ns option, the same is true. [Building from source requires current versions of autoconf and automake and the command-line Developer utilities.]
    – lawlist
    Nov 29, 2017 at 5:16
  • 1
    Also keep in mind that many versions of OSX ship with an outdated Emacs at /usr/bin/emacs, which has caused me problems in the past. And, I recently spent a bunch of time because etags ships with OSX and is also outdated. I'm glad you found the issue. Please feel free to write up an answer with the solution, and rephrase anything in the question or title so that the question/answer is helpful to someone who Googles a similar problem and/or searches this forum directly. You will be able to accept your own answer in something like 24 or 36 or 48 hours.
    – lawlist
    Nov 29, 2017 at 6:16
  • 1
    Same thing started happening to me after I upgraded from OS X 10.12 to 10.13.
    – Heikki
    Nov 29, 2017 at 14:54

2 Answers 2

2

I also find that the emacs 25.3 from https://emacsformacosx.com/ starts a new emacs process each time I do

$ open /Applications/Emacs.app

from the command line (MacOS 10.13.1)

As far as I can tell, this is new with MacOS 10.13

One fix: homebrew emacs raises the window instead of launching a new process:

$ brew install emacs --with-cocoa
$ brew linkapps emacs
1

If you launch emacs from the command line, here is a solution I've been using.

First, install emacs and emacsclient from homebrew cask.

Then, write a shell script:

#!/bin/sh
#  "[E]emacs" prevents grep from finding grep process itself.
if ps -e | grep '[E]macs.app' > /dev/null; then
  # Look for homebrew path: unnecessary if it's already in PATH.
  if test -x /opt/homebrew/bin/brew; then
    brewtopdir="/opt/homebrew"
  elif test -x /usr/local/bin/brew; then
    brewtopdir="/usr/local"
  fi
  $brewtopdir/bin/emacsclient -n "$@"
else
  open -a "Emacs" --args --chdir "$PWD" "$@"
fi
# The --chdir argument is necessary if we want to pass relative paths to Emacs.

Let us call this script emacs and place its path before the real emacs command. Then

$ emacs firstfile.txt # -> Launches the GUI emacs
$ emacs secondfile.txt # -> calls emacsclient

I wish I knew how to turn this shell script into a double-clickable "app" . . .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.