Customizing screen and screenrc.

July 26, 2020. Filed under screen 1

While my professional development has moved to VSCode (and don't get me wrong, I write effectively zero code as a CTO), I still do my personal work in a combination of Screen, Emacs and shell commands. I recently bought a new home computer to replace my aging iMac, and decdied to try something different: what if I left auto-update enabled instead of clicking "Remind me tomorrow" every day until this computer, too, is replaced?

This has been going well, except that I usually have two to three projects that I work on once or twice a month each (for example staffeng, this blog, etc), and the screen sessions were getting blown away each time a software update forced a reboot.

Today I decided that I was done manually setting up these screen windows after each reboot, and spent some time poking around to find a way to automate the setup after reboot.

First version

For my first pass I focused on finding something that worked no matter how terrible or inelegant it was. I ended up creating two files:

~/config/staffeng.rc

escape ^Pp
startup_message off
vbell off
screen -t dev   1   zsh -c "gatsby develop"
screen -t write 2   zsh
screen -t cli   3   zsh

~/config/staffeng.sh

ORIG=`pwd`
cd ~/git/staff-eng && screen -d -R staffeng -c $ORIG/staffeng.rc

Then (after making staffeng.sh executable with chmod +x) I was able to start my desired configuration via:

cd ~/config
./staffeng.sh

These work surprisingly well! It lets me setup my working environment, and also reattaches to the existing setup if one's already open thanks to the -d -R flags.

Second version

However, I have a bunch of different projects, so I wanted to avoid repeating the contents of my ~/.screenrc in each one of these, fortunately there's a source option for Screen configs, so I simplified ~/config/staffeng.rc down to:

source ~/.screenrc
screen -t dev   1   zsh -c "gatsby develop"
screen -t write 2   zsh
screen -t cli   3   zsh

This makes the staffeng.rc about as simple as it can get, but the runner script still feels a bit messier than necessary.

Third version

Looking at the staffeng.sh file:

ORIG=`pwd`
cd ~/git/staff-eng && screen -d -R staffeng -c $ORIG/staffeng.rc

I decided to try a different pattern of dropping a .screenrc into the different repositories I was working on and then having a generalized command I would run within that directory to spin up the project's screen settings.

For example, I would create:

~/git/staff-eng/.screenrc
~/git/irrational_exuberance/.screenrc

And then ideally I'd just:

cd ~/git/staff-eng
setup-screen

Implementing setup-screen turns out to be pretty simple:

PWD=`pwd`
NAME=`basename $PWD`
screen -d -R $NAME -c ./.screenrc

Then I copied staffeng.rc into ~/git/staff-eng/.screenrc and invoked the new setup-screen.

cd ~/git/staff-eng
setup-screen

This picks up the basename of the repository as the name and rejoins any existing screen with the same name:

willl@Mac-mini staff-eng % screen -ls
There is a screen on:
    1435.staff-eng  (Attached)

Altogether, pretty excited for how well this works.


The only issue at this point is that if started processes exit, for example running gatsby develop, then it doesn't exit to the shell, but rather it closes that window entirely. Hopefully I can figure out a way to exit to zsh instead of closing.