Faster Terminal Navigation with Tmux and Fuzzy finder
As a developer, I just love using the terminal. It makes you feel powerful; it's just so fast. But I've faced with one problem.. The Problem: Too Many Terminal Windows This blog is about exactly solving this. Let's get started. Introducing Tmux: The Terminal Multiplexer Tmux is a powerful tool that creates sessions on top of your terminal, allowing you to manage multiple windows and sessions efficiently. Installing Tmux To get started with Tmux, install it using your favourite package manager. For Ubuntu or Debian-based systems, for example: sudo apt-get install tmux 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. So what exactly happened? Tmux runs as a server, so even after you close the terminal, your sessions remain active. Let's explore 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 Press either p (previous) or n (next) to switch windows Let's Take It to the Next Level #!/usr/bin/env 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" These 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//session.sh\n" Replace 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 a faster workflow now and really love using this. You can check out my Tmux config here. Sources The Primeagen's Dev Productivity Shell
As a developer, I just love using the terminal. It makes you feel powerful; it's just so fast.
But I've faced with one problem..
The Problem: Too Many Terminal Windows
This blog is about exactly solving this. Let's get started.
Introducing Tmux: The Terminal Multiplexer
Tmux is a powerful tool that creates sessions on top of your terminal, allowing you to manage multiple windows and sessions efficiently.
Installing Tmux
To get started with Tmux, install it using your favourite package manager. For Ubuntu or Debian-based systems, for example:
sudo apt-get install tmux
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.
So what exactly happened?
Tmux runs as a server, so even after you close the terminal, your sessions remain active.
Let's explore 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
- Press either
p
(previous) orn
(next) to switch windows
Let's Take It to the Next Level
#!/usr/bin/env 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"
These 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//session.sh \n"
Replace
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 a faster workflow now and really love using this.
You can check out my Tmux config here.
Sources
What's Your Reaction?