Implementing Editor Scrollback in tmux
Zellij 0.30.0 release introduced an intriguing feature: opening pane contents in an editor as a regular file. I'm a tmux user, but I found it to be a very useful feature.
Although I deeply admire Zellij, I understand that tmux is heavily integrated into my workflow and has been more than enough for my needs, so It didn't make sense to make the effort to switch to Zellij just to get this feature. This led me to a little research where I found out that implementing the feature in TMUX was actually pretty straight forward, so I managed to do it with a 5 line bash script:
Implementing the scrollback feature
First, let's create the script that does the heavy
lifting. Don't forget to run chmod +x path/to/script so that
the script has execute permissions. We start by checking
whether we're in tmux or not, just in case:
#!/bin/bash
[ -z "$TMUX" ] && echo "Not inside tmux" && exit 1Then, we create a temporary file to hold the scrollback and tell tmux to save the contents there:
tmpfile=$(mktemp /tmp/tmux-buffer-XXXXXX);
tmux capture-pane -p > $tmpfile;Now we just need to tell tmux to open the temporary file in our favorite editor:
tmux send-keys "$EDITOR $tmpfile" C-mAnd that's it! We can test our little script by going into a tmux session and running it.
Binding the script to a keyboard shortcut
Let's go ahead and add a binding to run the script in our tmux configuration:
bind-key C-Space run-shell "path/to/my/script"
Now we can reload our tmux configuration and use Ctrl+Space to automatically open a file with the buffer contents in our favorite editor.