tmux link-window

tmux is my multiplexer of choice, and I love it, but a part of the workflow didn’t work quite right for me. I use org-mode for emacs and would keep a dedicated session open with just a single window in which I had emacs up. Context switching from other work to org (to clock in or out of a task, for example) would require Prefix s (I use C-q for my prefix) -> navigate to org session -> do org work -> Prefix s -> navigate back to work session.

My brother was recently working on writing a custom layout for xmonad with a 2D grid of windows that featured “pinned” columns. So rows would function as different contexts or workspaces, and columns within rows would be windows within that workspace, but certain columns would be shared across all rows (“pinned”), so that he could keep a chat app accessible on column 1 within all contexts, for example. The anaology to tmux would be that rows are sessions and columns are windows.

I realized that I wanted a similar “gutter” feature in tmux for org-mode - I wanted to be able to have my emacs window in window 0 of all my sessions.

Enter link-window ! tmux link-window allows a single window to be shared across multiple sessions. It accepts -s a source window and -t a destination window, which can have a session: identifier prefixed to it (with the colon to separate the session and window identifier). So if I always keep a window titled 'org' open in session 0, then I can run tmux link-window -s '$0:org' -t '0' in any other org session to link window 0 to the @org window in session 0.

Using tmux new-window you can get a little fancier and create a helper script that’ll automatically create the @org window in session 0 if it doesn’t already exist.

#!/usr/bin/env bash
# unnoficial strict mode, note Bash<=4.3 chokes on empty arrays with set -u
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
shopt -s nullglob globstar

ORG_DIR="$HOME/org-mode"

tmux has-session -t '$0' > /dev/null \
  || (echo 'no session $0 !' && exit 1)
tmux list-windows -t '$0' | grep 'org' > /dev/null \
  || tmux new-window -d -c "$ORG_DIR" -n 'org' -t '$0:0' emacs \
  || (echo 'could not create $0:org !' && exit 1)
session_id="$(tmux display-message -p '#{session_id}')"
[[ "$session_id" == '$0' ]] && echo 'in session $0, done' && exit 0
tmux list-windows | grep 'org' > /dev/null && echo '@org already exists, done' \
  || tmux link-window -s '$0:org' -t '0' 2> /dev/null \
  || (echo 'something went wrong :(' && exit 1)
Written on August 29, 2022

Leave a comment!