I recently switched from GNU screen to tmux, so I’m discovering all of these awesome little features. This morning, I found out (accidentally) that you can remotely script sessions, and here’s how it came about:
I added a new back-up disk and back-up scheme (more on that later), and decided to do a little cleaning while I was at it. I’ve got 10 years of digital photography in one drive, much of which isn’t very well organized. There’s a lot of duplication resulting from copying and moving things over the years, so I ran fdupes on that directory and it came back with more than 1,000 duplicate sets. These files are fairly large, and there are a few home videos mixed in there which are even larger, and so fdupes itself took a while to run; when it was done, I had found myself in a situation where I didn’t want to press “1” a thousand times, but I also didn’t want to re-run fdupes just to pipe
yes n to it. Luckily, I remembered coming across a
send-key command in the tmux man page, and that proved to be the solution.
It turns out that you can send any tmux command to any tmux session using the the command line, and this allows you to script any interaction you might want to have with tmux or any process that is running within tmux. You can take a (text) snapshot of a window buffer, and you can also send input events to any window. In my particular case, it ended up being the little shell script:
for x in `seq 1 1000`; do tmux send-key -t 0:3 1 C-m done
The tmux command says, “send, to session #0, window #3, the key ‘1’ followed by Ctrl-M”. This is really powerful; you can completely script any tmux session, including (for example) a start-up tmux script that runs processes, creates windows, and splits windows into panes.
tmux still lacks the ability to attach different clients to different windows within a session (or, if it allows that, I haven’t yet discovered how to do it), which makes running tmux in an environment such as XMonad a little less pleasant than screen; however, this ability to do emergency, post-hoc scripting is a killer feature, and has sold me on tmux.