Creating a Tmux Keybinding for Pop-up Sessions

Created Updated
3 min read 598 words

Tmux is a powerful terminal multiplexer, allowing you to manage multiple terminal sessions within a single window. One particularly useful feature is the ability to create popup windows, which can host new tmux sessions without disrupting your main workspace.

In this article, we will walk through how to create a keybinding in tmux that triggers a popup window for a new session or attaches to an existing one.

Prerequisites

Before we get started, make sure you have the following:

  1. Tmux installed on your system. You can install tmux using your package manager:
sudo pacman -S tmux
  1. Basic familiarity with shell scripting and tmux.

Step 1: Creating the popup.sh Script

Start by creating a script called popup.sh that will handle creating or attaching to a popup session in tmux.

  1. Create the file and make it executable:
mkdir -p ~/.tmux/plugins/tmux/custom
touch ~/.tmux/plugins/tmux/custom/popup.sh
chmod +x ~/.tmux/plugins/tmux/custom/popup.sh
  1. Open popup.sh in your favorite editor and add the following code:
#!/bin/bash

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 -E "tmux attach -t popup || tmux new -s popup"
fi

This script works as follows:

  • It sets the popup window’s default width and height to 80% of your screen, though you can change these values.
  • If you are currently in a tmux session named popup, it detaches you from that session.
  • If not, it either attaches to an existing session named popup or creates a new one.

Step 2: Adding a Keybinding to Your Tmux Config

Now that the script is ready, let’s bind a key in tmux to trigger it. For this example, we’ll bind the j key to run the popup.sh script.

  1. Open your tmux configuration file (.tmux.conf):
vim ~/.tmux.conf
  1. Add the following line to bind the key j to the popup.sh script:
bind-key j run-shell '~/.tmux/plugins/tmux/custom/popup.sh'

This will bind the j key (triggered after your tmux prefix, usually Ctrl+B) to run the popup.sh script. When pressed, it will either open a popup session or detach you from it if you’re already inside.

  1. Save the .tmux.conf file, and reload the tmux configuration:
tmux source-file ~/.tmux.conf

Step 3: Testing the Keybinding

Once you’ve set everything up, it’s time to test your new keybinding:

  1. Open a tmux session.
  2. Press <prefix> + j (with <prefix> usually being Ctrl+B).

If there’s no existing session named popup, tmux will create a new session inside a popup window. If a popup session already exists, it will attach to that session. Pressing the keybinding again will detach you from the session.


Step 4: Customizing the Popup Script

You can easily modify the popup.sh script to suit your preferences:

  • Changing the popup size: Modify the width and height variables in popup.sh to change the popup dimensions. For example:
width=${2:-90%}
height=${2:-50%}
  • Using different session names: If you want to name your sessions something other than popup, replace every instance of popup with your desired session name:
tmux attach -t my_popup || tmux new -s my_popup

Conclusion

By following these steps, you’ve successfully created a tmux keybinding that launches a popup session. This setup allows you to quickly create new tmux sessions or reattach to existing ones, all within a convenient popup window. Experiment with the script to customize your workflow even further!

Links

Add comment below...

Please refresh the page if commenting system is not visible.
Further reading