Vivek Haldar

Emacs as a login shell

About the only thing I use a regular shell for these days is to fire up an Emacs client against the daemon that is almost always running. That’s also the first thing I do when I ssh or log into a machine. As for shells, I’ve been exclusively using shells inside Emacs for quite a while now.

So I decided to script it up into my bash profile so it happens automatically when I log in. There’s no magic here, it’s quite straightforward. If the daemon is not already running, it starts that. Then depending whether you’re graphical or not, it starts the appropriate emacs client, putting you in a shell buffer titled “@login”.

# Start the emacs daemon if not already running.
daemon_running=`ps ux | grep "[e]macs --daemon"`
if [[ $daemon_running ]]; then
    echo "Emacs daemon already running."
    echo "Emacs daemon not running. Starting it."
    emacs --daemon

Check whether we’re graphical or not.

if [[ $DISPLAY ]]; then emacsclient -c -n -e “(shell "@login")” else emacsclient -t -n -e “(shell "@login")” fi