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 AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay