Skip to content

Instantly share code, notes, and snippets.

@alexandru
Last active January 13, 2024 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexandru/150132216611f011cf800667aa256725 to your computer and use it in GitHub Desktop.
Save alexandru/150132216611f011cf800667aa256725 to your computer and use it in GitHub Desktop.

Emacs Setup

Installation

My preferred installation on MacOS:

brew cask install emacs

This installs:

App '/Applications/Emacs.app'.
Binary '/usr/local/bin/emacs'.
Binary '/usr/local/bin/ebrowse'.
Binary '/usr/local/bin/emacsclient'.
Binary '/usr/local/bin/etags'.

GOTCHA: make sure these are on the system path and have priority over the system's Emacs.

MacOS Service

Create ~/Library/LaunchAgents/my.emacs.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>my.Emacs</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Applications/Emacs.app/Contents/MacOS/emacs</string>
      <string>--fg-daemon</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

The service can then be loaded via:

launchctl load -w ~/Library/LaunchAgents/my.emacs.plist

Or unloaded via:

launchctl unload -w ~/Library/LaunchAgents/my.emacs.plist

Script for running EmacsClient

Placed in ~/bin/run-emacsclient:

#!/usr/bin/env bash

cd ~

if [ -z "$EMACSCLIENT_OPTS" ]; then
    EMACSCLIENT_OPTS="-nc -a ''"
fi    

if [ $# -eq 0 ]; then
    COMMAND='/usr/local/bin/emacsclient '$EMACSCLIENT_OPTS' -e "(if (display-graphic-p) (x-focus-frame nil))"'
else
    COMMAND='/usr/local/bin/emacsclient '$EMACSCLIENT_OPTS' "$@"'
fi

if [ -z "$(shopt | grep login_shell)" ]; then
    echo "$COMMAND" | exec bash --login -s "$@"
else
    eval "exec $COMMAND"        
fi

Bash Settings

# Default editor
export EDITOR="run-emacsclient -t -a '/usr/local/bin/emacs -nw'"
export VISUAL="$EDITOR"
export ALTERNATE_EDITOR="vim"

# Editor aliases
alias e="EMACSCLIENT_OPTS='-t -a '\''/usr/local/bin/emacs -nw'\''' run-emacsclient"
alias ew="run-emacsclient"
alias notes='run-emacsclient -e "(if (display-graphic-p) (x-focus-frame nil))" -e "(deft)" | grep -v nil'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment