A herbstluftwm scratchpad

May 27, 2021 – 100 days to offload countdown #99

The idea of a scratchpad like app is appealing to me, but I never got used to i3’s scratchpad feature for one reason or another. Recently I stumbled upon herbstluftwm juggling, and the scratchpad section caught my attention. They use a small shellscript to have a wandering terminal window with the help of xdotool and herbstclient. I adapted the script to start konsole instead of urxvt, and start an emacsclient with the *scratch* buffer open – I have a terminal running tmux on $Mod-3, and that muscle memory will not go away. I had to alter the code a bit, I guess because of changes to herbstluftwm since the original post was written. Setup this way, I can take notes or paste code snippets, or json data into the emacs scratch buffer in a simple way. I’ll see if I can get used to that.

  #!/bin/bash
  scratchpad=/tmp/herbstluftwm:scratchpad
  if xdotool search --onlyvisible --name 'scratchpad' ; then 
      if [[ $(herbstclient list_monitors | grep '[FOCUS]' | cut -d'"' -f2) = $(herbstclient attr clients.$(cat $scratchpad) | grep 's - - tag' | awk '{ print $6 }' | cut -d'"' -f2) ]]; then 
          xdotool search --onlyvisible --name 'scratchpad' windowunmap 
          exit 
      fi 
  fi 
  if [[ -f "${scratchpad}" ]]; then
        if ! herbstclient chain . bring $(cat "${scratchpad}") . jumpto $(cat "${scratchpad}") ; then
          xdotool search --name 'scratchpad' windowmap && exit
      fi
  fi
  if ! xdotool search --name 'scratchpad' windowmap; then
      konsole -p Name=scratchpad -p tabtitle=scratchpad --hide-menubar --hide-tabbar -e scratch &
      xdotool search --sync --onlyvisible --name 'scratchpad' windowsize 66% 75%
      herbstclient attr clients.focus.winid > "${scratchpad}"
  fi
  alias scratch="emc -nw -c -e '(switch-to-buffer \"*scratch*\")'"
  # scratchpad console
  hc keybind $Mod-Return spawn ${HOME}/.config/herbstluftwm/scratchpad
  hc rule class=konsole title~'^scratchpad.*$' floating=on floatplacement=center