Figuring out my webapps problem yesterday made me ask myself if I could somehow repurpose that little xdotool script to solve another niche itch that I had.

Spoiler alert: It did.

I run Emacs as a daemon, and use EmacsClient to connect to the Emacs process.
Makes it really ease to launch, work on, sling around and close lots of Emacs windows.1

Which brings me to the hiccough. I launch EmacsClient and a window (frame) does indeed launch, but it just … stays there in the background.

Repeated attempts to launch EmacsClient are to no avail.
The pesky little windows just pile up in their corner.

Ergo, this little bit of Bash/xdotool shenanigans …

#!/usr/bin/env bash 

export EMACSWINID=$(xdotool search "\*scratch\*" | tail -n1)

if [ $EMACSWINID ]; then
    xdotool windowactivate $EMACSWINID
    sh -c 'emacsclient --create-frame --no-wait --alternate-editor="" && export EMACSWINID=$(xdotool search "\*scratch\*" | tail -n1) && xdotool windowactivate $EMACSWINID'
  • Check to see if an Emacs scratch buffer is open. If it is, switch to it.
  • Otherwise, just launch EmacsClient, get the id of the scratch buffer window (frame) and raise it, so that I can work on it. In peace.

An Emacs frame in the foreground

Cue: sounds of relief, in the background

Feedback on this post? Mail me at

P.S. Subscribe to my mailing list!
Forward these posts and letters to your friends and get them to subscribe!
P.P.S. Feed my insatiable reading habit.

  1. Frames, in Emacs parlance ↩︎