Random ramblings about Mac, Python, TeX, programming, and more  |     |          |     |  


Emacs on macOS

December 29, 2023  |  tools, programming, macos

I do almost all my programming and scientific writing in Emacs, and I am a Mac user running a recent version of macOS (usually the latest release available). I try to install most of the third-party software I use with Homebrew. It makes it much easier to install the software I need (including Emacs, BSD make, OpenSSL, Python, and Qt) and keep the software updated (regularly running the command «brew update && brew upgrade» in the Terminal).

I also prefer to use the Terminal with the command prompt (shell prompt) a lot, including launching Emacs to edit a file:

> startemacs mysourcecode.py

To ensure that this work you need to install Emacs start helpers. This can be done in many different ways with its pros and cons. To dig more into such scripts and apps, the Homebrew Emacs port on Github has a page describing the different options: Emacs Start Helpers. My current Emacs start helper script startemacs is this:

#!/bin/sh

emacs_app=/usr/local/opt/emacs-mac/Emacs.app

if [ ! -x "$emacs_app" ]; then
  echo "Emacs.app not found" >&2
  exit 1
fi

/usr/bin/osascript -e "tell application \"$emacs_app\" to activate" &

if [ $# -gt 0 ]; then
  tempfiles=()

  while IFS= read -r filename; do
    if [ ! -f "$filename" ]; then
      tempfiles+=("$filename")
      /usr/bin/touch "$filename"
    fi

    file=$(echo $(cd $(dirname "$filename") && pwd -P)/$(basename "$filename"))
    /usr/bin/osascript -e "tell application \"$emacs_app\" to open POSIX file \"$file\""
  done <<< "$(printf '%s\n' "$@")"

  for tempfile in "${tempfiles[@]}"; do
    [ ! -s "$tempfile" ] && /bin/rm "$tempfile"
  done
fi &

I have written several blog posts about related topics earlier. See for example Qt for Python (on Mac) and Emacs on Mac.

Last updated: December 29, 2023