2022-01-10 Simple tmux scripting revisited

Some time ago I wrote about my (very simple) tmux script. Since then I started to like tmux even more, and in fact I tend to create a similar script for every major project I work on. Last time, however, I noted how the sleep part is very fragile (and less than elegant). Since then I realized that I do not need any advanced scripting to run command Y in tmux window B when command X in window A finishes its job. In fact, this is very simple, and I’m ashamed I didn’t think about it earlier. The trick is to combine command X with a tmux invocation telling tmux to run command Y. So, here would be the script from the previous post updated to use this technique:

#!/bin/bash
tmux new -s work -d
tmux rename-window -t work logs
tmux send-keys -t work 'cd $BASE_DIR && vagrant up && tmux send-keys -t work:vm "cd $BASE_DIR && vagrant ssh" C-m && vagrant ssh' C-m 'pm2 log' C-m
tmux new-window -t work
tmux rename-window -t work vm
tmux new-window -t work
tmux rename-window -t work term
tmux send-keys -t work 'cd $BASE_DIR && git pull' C-m
tmux attach -t work

This way the second window “waits” with logging in tho the VM until it has properly started. Notice how we use the session:window syntax to send the keys to the correct window.

I still consider moving all that to Emacs. In fact, several Eshell windows and a saved window configuration might work in a very similar way. I guess I really need to warm to Eshell more – it has a lot of cool features I could really use. (One potential issue I have with Eshell is using it over ssh. Yes, I know I can use it with TRAMP, but I am a bit paranoid about Emacs somehow saving some data from the other end, like in some cache or a similar thing, and since I sometimes deal with rather sensitive data on a production server, it would be a very bad thing to happen. I’ll probably dive deeper into this issue one day…)

CategoryEnglish, CategoryBlog, CategoryEmacs