Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 Cocoa Emacs

Download the latest pretest version of [Emacs for Mac OS X]: http://emacsformacosx.com/builds

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"
end tell

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"
try
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
do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c -n"
end if
on error
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
tell application "Emacs" to activate

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.

@andresilva

This comment has been minimized.

Copy link

@andresilva andresilva commented Mar 29, 2012

This is great! There's only one thing I haven't been able to solve so far. When the Emacs daemon starts running it doesn't show up on the dock (which is good). But after I run Emacs Client for the first time, even when I close the last window (or frame) the Emacs icon still shows up on the dock.

Do you know of any way to make the Emacs icon disappear of the dock (and most importantly, the cmd-tab menu) when no GUI client is running?

Cheers.

@redinger

This comment has been minimized.

Copy link
Owner Author

@redinger redinger commented Mar 29, 2012

@andresilva

This comment has been minimized.

Copy link

@andresilva andresilva commented Mar 29, 2012

@redinger

This comment has been minimized.

Copy link
Owner Author

@redinger redinger commented Mar 29, 2012

@andresilva

This comment has been minimized.

Copy link

@andresilva andresilva commented Mar 29, 2012

Oh okay, got it ;-) Thanks for your help.

@mxi1

This comment has been minimized.

Copy link

@mxi1 mxi1 commented Dec 5, 2017

Thanks for sharing this gist. I have one question against the .emacs configuration file, I find the emacs client didn't read the .emacs configuration file when it starts. Do you aware of this issue or not? I guess most emacs users have got a lot of personized configurations, which are important. If they are not loaded automatically by the emacs client, it will be very pity.

@angelotrivelli

This comment has been minimized.

Copy link

@angelotrivelli angelotrivelli commented Jan 27, 2018

Starting with Yosemite, "Applescript Editor" became "Script Editor" and in order to create an Application package, you have to create a new "Cocoa-AppleScript Applet." Otherwise it will only create a scpt file instead of an app folder with an icon file, etc.

screen shot 2018-01-27 at 12 55 34 pm

You can also add the icon from within the script editor if you want (see here).

@ipstone

This comment has been minimized.

Copy link

@ipstone ipstone commented Feb 23, 2018

https://github.com/Tristan79/iBrew/blob/master/source/distro/mac/Info.plist#L5

  • Add this line to the info.plist file for the Emacs app, now the icon disappeared.
  • Additional note: this gist created 6 years ago, I was dubious of it's accuracy, but it's still working for me today 2018-02-23!
@x61

This comment has been minimized.

Copy link

@x61 x61 commented Mar 25, 2018

Great instruction. My only issue is if Emacs Daemon is already running, the script causes an error. Is there a way to include a condition to not run the deamon if it is already running?

@kevinjfoley

This comment has been minimized.

Copy link

@kevinjfoley kevinjfoley commented Apr 19, 2018

I put this together so I only need one script. It looks for emacsclient to throw an error and if it does then it starts emacs --daemon and then runs emacsclient. Should solve your issue @x61.

try
	tell application "Terminal"
		do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c"
	end tell
on error
	tell application "Terminal"
		do shell script "/Applications/Emacs.app/Contents/MacOS/Emacs --daemon"
		do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c"
	end tell
end try
@brorbw

This comment has been minimized.

Copy link

@brorbw brorbw commented Sep 12, 2018

I put this together so I only need one script. It looks for emacsclient to throw an error and if it does then it starts emacs --daemon and then runs emacsclient. Should solve your issue @x61.

try
	tell application "Terminal"
		do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c"
	end tell
on error
	tell application "Terminal"
		do shell script "/Applications/Emacs.app/Contents/MacOS/Emacs --daemon"
		do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c"
	end tell
end try

@kevinjfoley Why not use the -a option for emacsclient?

@koojagyum

This comment has been minimized.

Copy link

@koojagyum koojagyum commented Jun 6, 2019

Thanks for sharing this and all your discussion.
I had the problem Emacs daemon was not terminated so that I had to manually kill it.
The reason was I had put the following code in my init.el to confirm saving buffers before quit Emacs.

(setq confirm-kill-emacs 'yes-or-no-p)

After removing the code, this not-terminating issue was gone!
Currently I turn on the confirm code when Emacs is started by non-daemon mode.

(if (daemonp)
    nil
  (setq confirm-kill-emacs 'yes-or-no-p))
@p1mps

This comment has been minimized.

Copy link

@p1mps p1mps commented Dec 19, 2019

It works, the only thing is that there is a typo in the scripts:

endtell -> end tell

@sainathadapa

This comment has been minimized.

Copy link

@sainathadapa sainathadapa commented Jan 22, 2020

I add an additional argument while launching emacsclient: --create-frame to create a new frame whenever i call the application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment