Skip to content

Instantly share code, notes, and snippets.

@gma
Last active September 12, 2021 21:07
Show Gist options
  • Save gma/2ecafca0697c09903320cf3b71f3cd8e to your computer and use it in GitHub Desktop.
Save gma/2ecafca0697c09903320cf3b71f3cd8e to your computer and use it in GitHub Desktop.
tm script for launching tmux via fzf
#!/bin/sh
TM_ROOT="${TM_ROOT:-$HOME/Projects}"
TM_DEPTH=${TM_DEPTH:-4}
TM_PROJECT_CONTAINS="${TM_PROJECT_CONTAINS:-.git}"
TM_CRITERIA="${TM_CRITERIA:--type d -name $TM_PROJECT_CONTAINS}"
TM_FILTER="${TM_FILTER:-cat}"
TM_EDITOR="${TM_EDITOR:-vi -c 'set number relativenumber'}"
## Functions
err()
{
echo "ERROR: $(basename $0): $@" 1>&2
exit 1
}
running_within_tmux()
{
[ -n "$TMUX" ]
}
## Main program
DIR=$(
find "$TM_ROOT" -maxdepth $TM_DEPTH $TM_CRITERIA 2>/dev/null | \
sed "s,^$TM_ROOT/,,; s,/${TM_PROJECT_CONTAINS}$,," | \
$TM_FILTER | \
fzf
)
FZF_RC=$?
if [ -z "$DIR" ]; then
if [ $FZF_RC -eq 1 ]; then
err "no matching path"
else
exit $FZF_RC
fi
fi
SESSION="$(basename "$DIR" | tr '[:punct:]' '-')"
if ! tmux has-session -t "$SESSION" 2>/dev/null; then
tmux new-session -d -s "$SESSION" -c "$TM_ROOT/$DIR" -n editor "$TM_EDITOR"
fi
if running_within_tmux; then
tmux switch-client -t "$SESSION"
else
tmux attach-session -t "$SESSION"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment