Skip to content

Instantly share code, notes, and snippets.

@renxzen
Last active January 30, 2024 02:51
Show Gist options
  • Save renxzen/39ffdac9f56af52336bdc27c2ccd3737 to your computer and use it in GitHub Desktop.
Save renxzen/39ffdac9f56af52336bdc27c2ccd3737 to your computer and use it in GitHub Desktop.
Tmux scripts

tmux-sessionizer

description

lists your git workspaces and opens a new tmux session for the selected folder or switches if a session already exists for the workspace

author

ThePrimeagen

notes

  • adjust "max_depth" to your preference. the higher the value the slower results will appear.

script

#!/usr/bin/env bash

base_dir="$HOME/FOLDER_WHERE_YOUR_GIT_REPOS_ARE"
max_depth=3
selected=$(find $base_dir -mindepth 1 -maxdepth $max_depth -name .git -type d | sed 's/\/\.git$//' | fzf)

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

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

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

tmux switch-client -t $selected_name

repositories

description

based on sessionizer. lists all your git repositories in fzf to open on current shell and if in tmux, it renames the window.

author

renxzen

notes

  • adjust "max_depth" to your preference. the higher the value the slower results will appear.

script

#!/usr/bin/env bash

base_dir="$HOME/FOLDER_WHERE_YOUR_GIT_REPOS_ARE"
max_depth=3
selected=$(find $base_dir -mindepth 1 -maxdepth $max_depth -name .git -type d -prune | sed 's/\/\.git$//' | fzf --prompt="Select git folder:")

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

selected_name=$(basename -- "$selected" | tr . _)
cd $selected

if [ -n "$TMUX" ]; then
    tmux rename-window $selected_name
fi

tmux-switcher

description

lists your tmux sessions in fzf and switches to selected

author

renxzen

script

#!/bin/bash

selected_session=$(tmux list-sessions -F "#S" | fzf --prompt="Select tmux session:")

if [ -n "$selected_session" ]; then
    tmux switch-client -t "$selected_session"
fi

bind scripts on tmux

  • make sure the scripts are in your path
  • append to ~/.tmux.conf
bind e new-window -n (sessionizer) 'source tmux-sessionizer; exit; zsh'
bind r new-window -n (repositories) 'source repositories; zsh'
bind f new-window -n (switcher) 'source tmux-switcher; exit; zsh'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment