Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Start from CLI
# Emacs starter for Emacs mac port
# Thanks to Aquamacs Project and David Reitter
my $args = "";
my $tmpfiles = "";
for my $f (@ARGV) {
$args .= '"'.$f.'" ';
$tmpfiles .= '"'.$f.'" ' if (! -e $f);
system("touch $args") if ($tmpfiles);
system("open -a /Applications/ $args");
# delay deletion because AE drag&drop doesn't work with non-existing documents
system("(sleep 3; rm $tmpfiles) &") if ($tmpfiles);
Copy link

lolh-linc-zz commented Jun 13, 2017

This will create buffers of any number of existing or non-existing files with or without spaces in their names in a new instance of emacs_starter file1 file2 file3 ...

#! /usr/bin/env perl
local $" = qq{" "};
my (@have, @havenot);
for (@ARGV) {-e && {push @have, $_} || push @havenot, $_}
system qq{open -a "@have" --args "@havenot"};

If you have an emacs server running, then typing emacsclient file1 file2 file3 ... will use the server.

Copy link

worace commented Aug 24, 2017

Anyone know if it's possible to use emacsclient with emacs-mac in a way that opens a new cocoa frame with the -c flag? I've started emacs server using a system plist and am able to connect with /usr/local/opt/emacs-mac/bin/emacsclient, but only in a terminal session.

Interestingly it looks like some people seem to have a bin directory inside of /usr/local/opt/emacs-mac/ but I do not.

ls -la /usr/local/opt/emacs-mac/
total 24912
drwxr-xr-x  4 horace  admin       136 Aug 23 12:38 .
drwxr-xr-x  6 horace  admin       204 Aug 23 12:33 ..
-rwxr-xr-x  1 horace  admin  12747496 Aug 23 12:38 Emacs
-rwxr-xr-x  1 horace  admin      1650 Jul 26 21:44

If it's useful to anyone here's the plist I'm using to start the emacs daemon automatically on login:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
    <string>Gnu Emacs Daemon</string>

Then I have a couple utility functions for restarting it or stopping it as needed:

function emrestart {
    if pgrep "emacs.*daemon" > /dev/null
        echo "killing emacs daemon process"
        $EMACS_BIN_DIR/emacsclient -e "(kill-emacs)"
  launchctl unload "$HOME/Library/LaunchAgents/emacsserver.plist" &&
  launchctl load "$HOME/Library/LaunchAgents/emacsserver.plist"

function emstop {
    if pgrep "emacs.*daemon" > /dev/null
        echo "killing emacs daemon process"
        $EMACS_BIN_DIR/emacsclient -e "(kill-emacs)"

Would love to know if anyone has figured out the emacsclient thing though. That's the only thing I seem to be able to do with the default homebrew emacs setup that I haven't figured out with emacs-mac.

Copy link

tonylotts commented Sep 14, 2017

@worace If you start the server in a running GUI Emacs instance, emacsclient will connect to it.
It would be ideal to be able to start Emacs as a daemon process, and have GUI Emacs connect to it, but unfortunately I haven't gotten that to work.

Copy link

johnhamelink commented Sep 19, 2017

@tonylotts do you know if it's possible to have the GUI emacs client connect to an already existing emacs daemon instance?

Copy link

ChillingHsu commented May 26, 2018

@worace @johnhamelink
My solution to run GUI emacs through emacsclient is checking whether running. The only configuration you should do is add (server-start) to your emacs init.el, you can see details at my gist

BG_RED=`tput setaf 1`
BG_GREEN=`tput setaf 2`
BOLD=`tput bold`
RESET=`tput sgr0`


DEFAULT_EVAL='(switch-to-buffer "*scratch*")'

function run_client(){
    if [ $# -ne 0 ]
        ${EMACS_CLIENT} ${NO_WAIT} $@
        ${EMACS_CLIENT} ${NO_WAIT} ${DEFAULT_ARGS} "${DEFAULT_EVAL}" &> /dev/null

echo -e "Checking Emacs server status...\c"
if pgrep Emacs &> /dev/null
    echo "${BOLD}${BG_GREEN}Active${RESET}"
    echo -e "Connecting...\c"
    run_client $*
    echo "${BOLD}${BG_GREEN}DONE${RESET}"
    echo "${BOLD}${BG_RED}Inactive${RESET}"
    echo -e "Emacs server is starting...\c"
    open -a ${EMACS}
    echo "${BOLD}${BG_GREEN}DONE${RESET}"

    echo -e "Trying connecting...\c"
    until run_client $* &> /dev/null;[ $? -eq 0 ]
        sleep 1
    echo "${BOLD}${BG_GREEN}DONE${RESET}"

Copy link

Vvkmnn commented Jul 12, 2018

Tested out @ChillingHsu's method from above across a few different builds on macOS High Sierra 10.13 with Emacs 26, and found the following:

  • emacsclient works fine if you use emacs-plus.
  • emacsclient also works fine with Aquamacs (normal version installed via brew cask install emacs)
  • emacsclient doesn't work with emacs-mac, which doesn't install a /Applications/, and is therefore not available with or without the (server-start) parameter.

I like emacs-mac the most, since it works with my window manager. I currently run it by making EDITOR='open -a /Applications/, which opens the file in the existing window; sort of like having a persistent server.

Copy link

vemv commented Aug 6, 2018

How to place a hook for when one opens a file via the terminal?

(Using plain - no client/server)

Update: I can confirm either of these works:

(add-hook 'find-file-hook 'xxx)
(advice-add 'pop-to-buffer-same-window :after 'xxx)

Copy link

nasyxx commented Jan 27, 2019


  • emacsclient doesn't work with emacs-mac, which doesn't install a /Applications/, and is therefore not available with or without the (server-start) parameter.

Hello Vivek, I tried to change /Applications/ to /usr/local/bin/emacsclient (emacs-mac). After that, this script would work well.

Copy link

SolidStill commented Mar 18, 2020

Hi, would anyone happen to know the current functioning approach for the brew cask install emacs-mac installation?

The default script installed by brew at /usr/local/bin/emacs (without args) doesn't open a background instance of emacs by default, as is my preferred behaviour. I know I could do $ alias emacs='emacs &', but I'm basically a novice in this whole game and:

  1. I don't know what negative consequences that might have, and,
  2. I'm keen to get my hands on a working current solution as a case-study, and see why the other methods weren't working (or where I'm going wrong) for the sake of education in the ways of the shell.

Maybe someone knows whether me using the Spacemacs distribution or the Zsh shell would break these approaches?
(RE Spacemacs, when I attempted to use @ChillingHsu's approach I tried placing the (server-start) line in init.el, as well as every possible section of my .spacemacs file without luck)

Thanks very much

Copy link

aidanscannell commented Apr 4, 2020

Maybe someone knows whether me using the Spacemacs distribution or the Zsh shell would break these approaches?
(RE Spacemacs, when I attempted to use @ChillingHsu's approach I tried placing the (server-start) line in init.el, as well as every possible section of my .spacemacs file without luck)

Hi SolidStill, as a fellow spacemacs user I also faced some issues getting this setup.

I've written a short blog post about my emacs/spacemacs installation and setup which hopefully might help you. I used a combination of the code snippets from here but had to change a few things. You will also need to disable all of the spacemacs server variables in your .spacemacs file. I detail this in the post though.

Best of luck.

Copy link

SolidStill commented May 7, 2020

Just scanned thorugh your blog post @aidanscannell; thanks v much for taking the time to reply - your post touches on a few key additional topics I've been trying to develop a decent understanding of: Latex authoring in emacs on macos, version-controlling emacs setups/configurations.

Funnily enough, I actually graduated from Bristol with a Maths BSci in 2010, never used that education in any meaningful way, and am now training myself up to get a job in data analysis. I'm looking to redevelop a technical mindset/mentality by getting into the guts of (and really understanding) all the tools/methods/theory I encounter along that journey. Your post really nails the kind of efficiently-verbose and well-reasoned exposition that is crucial in really developing understanding in a concept, as opposed to just becoming familiar with a concept.

In all honesty, the temptation to pester you with emacs-related technical support questions is probably going to be too strong to resist. I'm truly glad that I eventually noticed this post, "being less amateur with Github notifications" is def going on my learning-list. Thanks for the inspiration and kudos for helping deliver Skynet to mankind 😛

Copy link

willbchang commented Dec 4, 2020

I wrote an Alfred workflow which supports emacs:

Copy link

duhd1993 commented Dec 10, 2020

Hi @mwolson, thanks for the update. I believe your starter script works for Nextstep port (although I haven't verify that by myself), but it not works for Mac port. Because Emacs Mac port lacks multi-tty support, which means, emacsclient from mac port can only able to create tty frames but not GUI frames.

@railwaycat This seems outdated. I'm able to open GUI emacs from emacsclient now. Could you update instructions for this? However, the Emacs server in GUI app won't run in the background. C-x C-x the last window closes the app and also the server. This behavior is different from the official Emacs build. Their can run without any window.

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