#!/bin/sh # Inspired by https://github.com/tmux-plugins/tmux-logging via # https://unix.stackexchange.com/questions/623054/how-to-automatically-log-all-my-tmux-sessions-to-a-file-in-directory-accordin # but massively simplified. # The tmux-logging plugin usees a different global variable for each pane (named # after the pane number) to check whether it is already logging. When the pane # is destroyed, the variable stays set. When a new pane is opened, it can # recycle the same pane number as before. The plugin will not log to any pane # with a recycled pane number! # # Luckily, tmux supports pane-local variables, which are automatically destroyed # when the pane is destroyed! So here we just use one of those. tmux_log_everything() { if ! [ "$TMUX_PANE" ] then return # We are not in tmux fi local LOGDIR LOGFILE LOGDIR=$HOME/.tmux-log-everything if ! [ -d "$LOGDIR" ] then return # User has not "requested" logging by creating log dir fi if [ "$(tmux show-option -pqv @pane-is-logging)" ] then return fi LOGFILE=$LOGDIR/$(date -Is).$$.log.gz tmux set-option -pq @pane-is-logging y tmux pipe-pane "exec gzip -c > $LOGFILE" } tmux_log_everything