Eric Radman : a Journal

Repeatable Workspaces

In software engineering a repeatable configuration is critical to testing, but a repeatable environment is not a clean environment. A test that tries too hard to eliminate environmental differences may also be allowing the software to make assumptions that don't hold when deployed in the wild. Therefore a repeatable test harness allows the same software to be reliably tested in a variety of platforms and environments.

A similar, but not equivalent benefit exists for creating a workspace that can be re-instantiated. tmux gives you the capability to recreate the session you work in so that you can get back to work after a reboot.

Edit, Preview

tmux(1) plus other tools such as entr(1) can be used to interactively generate output from text processors such as haml or haml. Start by creating a tmux session

#!/bin/sh
session=haml
tmux new-session -s "$session" -d

window=$session:0
filename=${1:-/tmp/comments.haml}
touch $filename

Next send commands to pull up an editor and monitor a file for changes

tmux send-keys -t $window "vim $filename" C-m
tmux split-window -t $window -v -p 66
tmux send-keys -t $window "find $filename | entr -c haml26 $filename" C-m
tmux select-pane -t $window.0

Finally, attach to the new session

tmux -2 attach-session -t "$session"

This immediately puts me an environment where I can start typing HAML which will crank out HTML that I can copy-and-paste. We can do the same thing for generating HTML from Markdown.

Creating A Series of Windows

Creating an workspace with an arbitrary number of windows will follow the same pattern: start a disconnected session, send it commands, and finally connect

for n in 0 1; do
window=$session:$n
case $n in
    0)
        tmux rename-window -t $window "radnet"
        ;;
    1)
        tmux new-window -t $window -n "eradman.com"
        ;;
esac
done

# Switch to first window and attach to session
tmux select-window -t $session:0
tmux -2 attach-session -t "$session"

I create a new window for each major project or major operation that I'm in working on. All of the tmux commands such as rename-window and split-window that work from a script provided you specify the session name and window number.

    0)
        tmux rename-window -t $window "radnet"
        tmux send-keys -t $window "ssh 172.16.0.2" C-m
        tmux send-keys -t $window "cd ~/git/radnet" C-m
        tmux split-window -t $window -h -p 50
        tmux send-keys -t $window "ssh 172.16.0.3" C-m
        tmux send-keys -t $window "cd ~/git/radnet" C-m
        tmux select-pane -t $window.0
        ;

To assist with the testing of the script itself you may want to destroy the window after detaching

tmux -2 attach-session -t "$session"
# user exited, destroy
tmux kill-session -t "$session"

Last updated on July 08, 2021