Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Run Emacs.app as Client/Server, for someone who likes GUI emacs and emacsclient to speed up start-up time.
#!/bin/bash
BG_RED=`tput setaf 1`
BG_GREEN=`tput setaf 2`
BOLD=`tput bold`
RESET=`tput sgr0`
EMACS='/Applications/Emacs.app'
EMACS_CLIENT='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient'
DEFAULT_EVAL='(switch-to-buffer "*scratch*")'
DEFAULT_ARGS="-e"
NO_WAIT='-n'
function run_client(){
if [ $# -ne 0 ]
then
${EMACS_CLIENT} ${NO_WAIT} $@
else
${EMACS_CLIENT} ${NO_WAIT} ${DEFAULT_ARGS} "${DEFAULT_EVAL}" &> /dev/null
fi
}
echo -e "Checking Emacs server status...\c"
if pgrep Emacs &> /dev/null
then
echo "${BOLD}${BG_GREEN}Active${RESET}"
echo -e "Connecting...\c"
run_client $*
echo "${BOLD}${BG_GREEN}DONE${RESET}"
else
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 ]
do
sleep 1
done
echo "${BOLD}${BG_GREEN}DONE${RESET}"
fi
@ChillingHsu

This comment has been minimized.

Copy link
Owner Author

@ChillingHsu ChillingHsu commented May 26, 2018

Installation

You can put this script into /usr/local/bin and add (server-start) in your emacs startup script (e.g init.el).
In my case, you can run this script by command emacs (or whatever the script name you pick)

Usage

  1. Command emacs to run emacs and switch buffer to *scratch*
  2. Command emacs FILENAME [FILENAMES ...] to run emacs with FILENAME open.

This script will first check Emacs server status and starting up Emacs.app as Server if there's no server existing.
And then run emcas by emacsclient.

@SolidStill

This comment has been minimized.

Copy link

@SolidStill SolidStill commented Mar 18, 2020

Hi, maybe you can help me out here...

I've installed emacs-mac via brew cask install emacs-mac-spacemacs-icon, as per the instructions here. I've then followed the link provided on that page to get to the CLI-related thread: "An Emacs.app CLI starter, modified from Aquamacs - https://gist.github.com/4043945". Eventually leading me here - unfortunately, I can't get this script to work.

I'm using the Spacemacs distribution so I tried adding the (server-start) line to my .spacemacs config file, in each possible section for user commands, trying one section at a time; then tried the init.el file without success. Each attempt results in the following message: Checking Emacs server status...Active Connecting...DONE.

Also,emacs -v gives Checking Emacs server status...Active Connecting...emacsclient 26.3 DONE,

and /Applications/Emacs.app/Contents/MacOS/bin/emacsclient -e '(switch-to-buffer "*scratch*")'
gives

/Applications/Emacs.app/Contents/MacOS/bin/emacsclient: can't find socket; have you started the server?
To start the server in Emacs, type "M-x server-start".
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient: No socket or alternate editor.  Please use:

	--socket-name
	--server-file      (or environment variable EMACS_SERVER_FILE)
	--alternate-editor (or environment variable ALTERNATE_EDITOR)

To clarify, I wrote the (server-start) line to file and tried:

  1. I saved the script as /usr/local/bin/emacs, and tried $ emacs,
  2. I tried chmod +x /usr/local/bin/emacs with no change in behaviour,
  3. and I tried saving the script to /usr/local/bin/emacs.sh and the command$ emacs.sh, just in case.

Any idea where I'm going wrong here? Thanks

@ChillingHsu

This comment has been minimized.

Copy link
Owner Author

@ChillingHsu ChillingHsu commented Mar 21, 2020

@SolidStill sorry for my late reply, according to your spacemacs given, it seems that you did not start emacs server successfully. I have not used spacemacs so I'm not very sure whether the (server-start) can successfully boot up your emacs server. And you can try emacs -Q and run server-start manually by M-x, and run the script again to make sure it work properly on your emacs. If it does, the question should come from your spacemacs.

@lonelykid

This comment has been minimized.

Copy link

@lonelykid lonelykid commented Dec 10, 2020

Thank you, ChillingHsu. This works with the MacPort build by YAMAMOTO Mitsuharu by changing the path of emacsclient to where it is. But there are some issues.

  1. Closing the windows of emacs closes the server by C-x C-c. Is it possible to leave the daemon running in the background?
  2. Is it possible to let GUI emacs connect to existing emacs daemon?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment