tmux + fzf

Contents

tmux

If you are unfamiliar with tmux, you can read more about it here. But the best way to get started is to try it out yourself.

tl;dr: What is tmux?

tmux is a terminal multiplexer, it lets you:

One huge perk of tmux is the ability to create different sessions running in a same terminal. This allows us to navigate between sessions without disrupting the process of other sessions.

Furthermore, a session can have different windows (or tabs in a familiar-term) that allow us to have different processes running (think of them as browser’s tabs). Also, a window can contain multiple panes. You can split the terminal vertically or horizontally.

These 3 things combined are what makes tmux so powerful and productive in everyday work. All of this, e.g: create sessions, windows, split panes,…, can be controlled entirely via the keyboard for speed and ergonomic. Better yet, tmux is highly customizable - keybindings and behaviors can be defined in a config file, which you can version control and reuse across machines. Pretty neat!. Plus it looks cool.

tmux

fzf

fzf stands for fuzzy-find. You can read more about it here.

tl;dr: What is fzf?

Combining

You may ask what’s the connection between these two? You can actually utilize fzf to extend tmux’s session creation to supercharge directory searching and opening a session to it.

For example, you’re working on <project>/ and always navigate to that directory. However, navigating to that directory manually seems cumbersome since you need to type cd lots of times! tmux+fzf will simplify that process and make it even faster

Setting up tmux

Setting up a custom script

Prerequisites:

After that, follow these steps:

#!/usr/bin/env bash

if [[ $# -eq 1 ]]; then
    selected=$1
else
    # you can change this to you prefered folder, mine is ~/personal and ~/work
    # mindepth and maxdepth = 1 mean you dont search for subfolders but only direct child
    # it then pipes the result into fzf, which then you can search
    selected=$(find ~/ ~/work ~/personal -mindepth 1 -maxdepth 1 -type d | fzf)
fi

if [[ -z $selected ]]; then
    exit 0
fi

selected_name=$(basename "$selected" | tr . _)
tmux_running=$(pgrep tmux)

# when you select a folder, tmux will create a session to that folder.
if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then
    tmux new-session -s $selected_name -c $selected
    exit 0
fi

if ! tmux has-session -t=$selected_name 2> /dev/null; then
    tmux new-session -ds $selected_name -c $selected
fi

# attach or switch to session
# this prevents creating duplicate session and instead attach to it
if [[ -z $TMUX ]]; then
    tmux attach-session -t $selected_name
else
    tmux switch-client -t $selected_name
fi

tmux switch-client -t $selected_name

Congratulations, now you can use this script to navigate folder and open session even faster! Simply run tms in your terminal or press prefix + f if you’re already in a tmux session.

My configs

You can checkout my tmux config and other awesome stuff here. They’re curated to fit my style so you might need to adjust them.