Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Setting up Emacs daemon on OS X

Setting up Emacs daemon on OS X

Tired of waiting for emacs to start on OS X? This step by step guide will teach you how to install the latest version of emacs and configure it to start in the background (daemon mode) and use emacsclient as your main editor.

Install Homebrew

First you'll need to install the Homebrew package manager if yo haven't already. It is amazing.

Install Cocoa Emacs

brew install emacs --cocoa
open $(brew --prefix)/Cellar/emacs/HEAD/

Drag the Emacs Application to your Applications folder

Create the daemon application

Open the AppleScript editor and paste the following:

tell application "Terminal"
do shell script "/Applications/Emacs.app/Contents/MacOS/Emacs --daemon"
endtell

Save this as an Application called "Emacs Daemon" in your Applications folder

Add as a login item

Goto System Preferences > Accounts

  • With your user name selected, click on the Login Items tab
  • Click on the plus sign
  • Browse to your applications folder and find "Emacs Daemon"
  • Don't worry about the "hide" checkbox, since it the dock icon disappears naturally

Create the client application

Open the AppleScript editor and paste the following:

tell application "Terminal"
do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c"
endtell

Save this as an Application called "Emacs Client" in your Applications folder.

Giving the client application a proper icon

Using the Finder, navigate to your Application folder and right click on your Emacs application (not the client or daemon), and click on Show Package Contents Do the same for the Emacs Client application

Copy the Emacs.icns file from Contents/Resources of the Emacs application to the Contents/Resources of the Emacs Client application Delete applet.icns and rename Emacs.icns to applet.icns in the Emacs Client application.

Hiding the dock icon for Emacs while Emacs Client is running

  • Again, navigate to your Application folder and right click on your Emacs application (not the client or daemon), and click on Show Package Contents
  • Open the Info.plist using the Propertly List Editor
  • Click Add Child
  • Set the key NSUIElement
  • Set the value to 1
@offbyone

It's not clear how this works with opening files from the finder... does it support that? When I tried, it opened a new browser tab instead.

@algal

The following applescript (from https://github.com/brianm/emacs-client-mac/blob/master/emacs-client-mac.scpt ) has some nice additions. It opens a frame if none is open, and it starts the daemon if the daemon's not already started:

tell application "Terminal"
    try
        -- we look for <= 2 because Emacs --daemon seems to always have an entry in visibile-frame-list even if there isn't
        set frameVisible to do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -e '(<= 2 (length (visible-frame-list)))'"
        if frameVisible is not "t" then
            -- there is a not a visible frame, launch one
            do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n"
        end if
    on error
        -- daemon is not running, start the daemon and open a frame     
        do shell script "/Applications/Emacs.app/Contents/MacOS/Emacs --daemon"
        do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n"
    end try
end tell

-- bring the visible frame to the front
tell application "Emacs" to activate
@gnarmis

I had to s/endtell/end tell/

@sprig

Why wrap everything with 'tell application "Terminal"?

It works just as well without it...

@fedorn

Anyone knows how to fix that you can't switch to emacs frame using Cmd-Tab or clicking icon in dock?

@MaDeuce

Yes, I'm late to the party. @algal, thanks for the pointer; it was helpful. However, unless I'm missing something, it looks like there's a bug. Specifically, emacsclient is never called if frameVisible is "t". I believe there should be an 'else' which calls emacsclient without '-c'.

@IvanMalison

@fsqcds I was experiencing similar issues until I used the reattch-to-user-namespace (which is brew installable) program to launch emacs. You can check out https://github.com/IvanMalison/dotfiles/blob/master/dotfiles/lib/shellrc/emacs.sh to see how I have things set up.
I don't use the apple script approach since I see no reason to use an OSX specific method of starting emacs when a more generic one can work just as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.