Using the terminal is one of my favourite things, its just so fasst and its the jam of us linux nerds.
Buut I've faced with one problem..
The Problem: Too Many Terminal Windows
Yep, In this blog, we'll tackle exactly this. Though there are many workarounds and tools for this but here is what worked for me
Introducing Tmux: The Terminal Multiplexer
Gosh I just love this thing! So Tmux is a powerful tool that creates sessions on top of your terminal. Let's just see this in practice
Installing Tmux
To get started with Tmux, install it using your favourite package manager. For our lovely linux community just do this:
sudo apt-get install tmux
Well windows users can open up WSL i guess..
Basic Tmux Usage
Let's walk through the basics of using Tmux:
-
Open a terminal and type
tmux
The green bar at the bottom indicates that you're in a Tmux session.
-
Run the below Python command
python -c "import time; print('Running...'); time.sleep(300)"
This command just shows Running... for 300 seconds
-
Now close the Terminal
Everything seems to be gone, right? Now type the below command
tmux attach
And Voila! We're back in the previous session, exactly where we left off.
What the heck happened?
Tmux runs as a server, so even after you close the terminal, your sessions remain active.
Some basic Tmux commands:
-
List active sessions
tmux list-sessions
-
Create a new detached session
tmux new-session -d
Running tmux list-sessions
again: You'll now see two sessions listed.
Once you're in a session, try tmux new-window
. Notice how the asterisk moves to the new window number.
You can create multiple windows this way.
Let's Dive into Tmux Navigation—the Real Power of This Tool!
The default Tmux prefix key is Ctrl+B
. Here's how to use it:
- Press
Ctrl+B
-
RELEASE BOTH KEYS and then press either
p
(previous) orn
(next) to switch windows
You have no idea how many times i have failed to do this, releasing the prefix key is so, so important.
I have spent a lot of time cursing tmux and figuring out why in the world it was not working because of this dumb mistake.
Let's Take It to the Next Level
#!/bin/bash
session=$(find ~/.config ~/personal ~/courses -mindepth 1 -maxdepth 1 -type d | fzf)
session_name=$(basename "$session" | tr . _)
if ! tmux has-session -t "$session_name" 2>/dev/null; then
tmux new-session -s "$session_name" -c "$session" -d
fi
tmux switch-client -t "$session_name"
For my zsh folks, the first line might be
#!/usr/bin/env bash
So are my three frequently used folders – personal, courses, and .config – our bash script utilizes fzf (fuzzy finder) to quickly find the desired folder and then creates a new Tmux session if one doesn't already exist.
Integrate with Your Terminal
Add a shortcut in your shell configuration (e.g., .zshrc
or .bashrc
) so that you can run the script quickly:
bindkey -s ^f "/home/<username>/session.sh\n"
Replace <username>
with your actual username. This example binds the script to Ctrl+F, giving you instant access to your Tmux sessions.
Wrapping Up
I personally found this very helpful. I have like a super fast workflow now and really love using this.
Thank you so much for reading. You're Awesome!
You can check out my Tmux config here.
Top comments (0)