Skip to content

Instantly share code, notes, and snippets.

@anler
Last active February 22, 2016 12:13
Show Gist options
  • Save anler/11137890 to your computer and use it in GitHub Desktop.
Save anler/11137890 to your computer and use it in GitHub Desktop.
Edit command to launch Emacs
#!/bin/bash
# To automatically start Emacs in daemon mode if there's no daemon already
# running, set:
#
# export ALTERNATE_EDITOR=""
#
# Automatically choose how to open Emacs:
# 1. If running in interactive terminal use the same terminal.
# 2. If there's a frame created, use that frame.
# 3. Otherwise create a new frame and use it.
#
if $(tty -s); then
# Launch Emacs in the terminal if running in interactive mode
emacsclient -t -a "$ALTERNATE_EDITOR" $1
else
# When non-interactive.
# Detect if there is an Emacs frame already created.
if $(emacsclient -e "(frame-list)" | grep "<frame emacs@"); then
# If there's one, use it.
emacsclient -n -a "$ALTERNATE_EDITOR" $1
else
# Otherwise create a new one.
emacsclient -c -n -a "$ALTERNATE_EDITOR" $1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment