meain/blog

Sep 15, 2020 . 2 min

Floating scratch terminal in tmux

Hi, Just another one off blog. I have been using tmux for a while and one main thing I always wanted to have in tmux is a floating scratch terminal. I got so used to this during my time with i3 and wanted to replicate it with just tmux.

screenshot

Check out what I am talking about on Youtube.
Code used here: script and tmux-config

A while back, tmux actually got foating window support and I am using it for a lot of things. I had by this time had a floating terminal setup however and so did not really think about using this initially.

Today, I thought I would actually try getting this can be done in tmux. Just for the heck of it.

Floating windows in tmux #

Basically you can get a floating window in tmux using the following command:

tmux popup -R "ping meain.io"

This will start a ping to 'meain.io' in a floating window. Step one complete.

Now if you want to run something like a shell, you can use:

tmux popup -KER zsh

K & R is so that you can get input in to the process in the floating window
E is so that after clean exit, we return back

Persisting session #

Now that we have a terminal, we can technically use the tool that we have been using to persist stuff to persist the session. When creating a popup, we can start a tmux session and attach to it on further invocations. To quit out of the popup, we can just detach from it.

tmux popup -KER "tmux attach -t popup || tmux new -s popup"

The above script with attach to a session called popup, or create one if it does not exist. We are half way there, but I don't wanna be pressing two different keys for showing and hiding the popup terminal.

For starters, let us create a script called popuptmux and put what we have in it.

pouptmux

tmux popup -KER "tmux attach -t popup || tmux new -s popup"

Now call this script with a tmux keybinding:

bind-key j run-shell 'popuptmux'

Now when you press <prefix>j it opens the session named popup in a floating window. Now to make our script a bit more intelligent.

pouptmux

if [ "$(tmux display-message -p -F "#{session_name}")" = "popup" ];then
tmux detach-client
else
tmux popup -KER "tmux attach -t popup || tmux new -s popup"
fi

With this script, when you are in a session called popup(which you are when you have the floating window open) we detach, otherwise we create a popup with a session named popup and attach to it.

Btw, if you want a bigger floating window, you can always just ask tmux.

pouptmux

width=${2:-80%}
height=${2:-80%}
if [ "$(tmux display-message -p -F "#{session_name}")" = "popup" ];then
tmux detach-client
else
tmux popup -d '#{pane_current_path}' -xC -yC -w$width -h$height -K -E -R "tmux attach -t popup || tmux new -s popup"
fi

Checkout the discussion on reddit, have a great workflow with floating terminals by /u/KevinHwang91 in there.

All good, now we can just go on hitting <prefix>j to open and close the floating window. Although we do have this now, the performance ain't that good. So at the end of the day if you open and close it a lot, this might not be for you.

← Home