dotfiles/tmux/.config/sh/alias.d/tmux.sh
Marty Oehme 92376839a4 Add tmux fzf session chooser
Added tmux session chooser. Aliased to `tm`, calling `tmux_attach_start`
(the original tm script).

When called without arguments displays a fzf list of currently running
tmux sessions, with a preview to their respective open panes. A session
can be chosen in fzf which tmux will attach itself to. When creating a
query in fzf which does not have a valid target and confirming, tmux
will automatically create that session and attach itself to it.

When called with an argument, tmux will attach itself or create a
session of the same name. If called with the name of a session file, as
before, tmux will automatically execute that session file and attach
itself to it.
2020-02-02 23:44:23 +01:00

50 lines
1 KiB
Bash

#!/usr/bin/env sh
exist() { type "$1" >/dev/null 2>&1; }
# show a list of running tmux sessions
alias tl='tmux list-sessions -F "#{session_name}" 2>/dev/null'
# fzf
if exist fzf; then
# fzf select a tmux session to connect to, with pane preview
alias tm='fzf_tmux'
fi
fzf_tmux() {
if [ -z "$1" ]; then
result=$(
tl | fzf \
--layout=reverse \
--height=50% \
--border \
--prompt="Session> " \
--preview="tmux_pane_tree {}" \
--preview-window=right:99% \
--print-query
)
case "$?" in
0)
# found a session, attaching
tmux_attach_start "$(echo "$result" | tail --lines=1)"
;;
1)
# did not find a session, creating
result=$(echo "$result" | head --lines=1)
# if . was only thing entered, create one for current dir
if [ "$result" = "." ]; then
tmux_attach_start
# create for query name
else
tmux_attach_start "$result"
fi
;;
esac
else
tmux_attach_start "$1"
fi
}
unset choice
unset -f exist