DEV Community

Siddarth
Siddarth

Posted on • Edited on

Faster Terminal Navigation with Tmux and Fuzzy finder

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..

a lot of terminal windows

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
Enter fullscreen mode Exit fullscreen mode

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.

    tmux bar

  • Run the below Python command

    python -c "import time; print('Running...'); 
    time.sleep(300)"
    

    This command just shows Running... for 300 seconds

    tmux bar

  • 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.

tmux sessions

Once you're in a session, try tmux new-window. Notice how the asterisk moves to the new window number.

tmux new window

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:

  1. Press Ctrl+B
  2. RELEASE BOTH KEYS and then press either p (previous) or n (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"
Enter fullscreen mode Exit fullscreen mode

For my zsh folks, the first line might be

#!/usr/bin/env bash
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Replace <username> with your actual username. This example binds the script to Ctrl+F, giving you instant access to your Tmux sessions.

Image description

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.

Sources

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay