Faster Terminal Navigation with Tmux and Fuzzy finder

SiddarthSiddarth
3 min read

As a developer, I love using the terminal. It makes you feel powerful, and yes, I have the bad habit of typing the clear command again and again. However, I've struggled with managing multiple terminal windows

The Problem: Too Many Terminal Windows

This is exactly the problem we're going to solve.

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, install Tmux using your package manager. For example, on Ubuntu or Debian-based systems, you can use:

bashCopysudo apt-get install tmux

Basic Tmux Usage

Let's walk through the basics of using Tmux:

  1. Open a terminal and type tmux to start a new session. You'll see a green bar at the bottom, indicating you're in a Tmux session.

  2. Navigate to a directory you want. For example: cd ~/Desktop

  3. Open a file using your preferred editor. For instance: vim lol.txt

  4. Now, let's close the terminal. Everything seems to be gone, right?

  5. Open a new terminal and type tmux a. Voila! We're back in the previous session, exactly where we left off.

Tmux runs as a server, so even when you close the terminal window, your sessions remain active. To stop everything, use the command tmux kill-server.

Let's explore some basic Tmux commands:

  1. tmux list-sessions: Shows your active sessions.

  2. tmux new-session -d: Creates a new detached session.

  3. 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 (1). You can create multiple windows this way.

Now, 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

  3. Press either p (previous) or n (next) to switch windows

    Now, we solved a part of the problem but still taking it to the next level, we have this bash script.

#!/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 "$sessionname" 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.

Integrating the Script with Terminal

To run this script effortlessly from the terminal, you can add it as a shortcut to the .zshrc or the .bashrc file.

bindkey -s ^f "/home/<username/session.sh\n"

Replace <username> with your actual username. This binds the script to the Ctrl+F key combination, allowing you to quickly access and manage your Tmux sessions.

Sources

https://theprimeagen.github.io/dev-productivity/shell

my tmux config- https://github.com/siddarth2810/my-i3-tmux-config/blob/main/tmux.conf

0
Subscribe to my newsletter

Read articles from Siddarth directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Siddarth
Siddarth