From 92376839a4d3666c58c68b72bd74767318d87934 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sun, 2 Feb 2020 23:44:23 +0100 Subject: [PATCH] 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. --- tmux/.config/sh/alias.d/tmux.sh | 49 +++++++++++++++++++ tmux/.config/tmux/sessions/dot.session | 2 +- tmux/.config/tmux/tmux.conf | 2 +- .../tm => tmux/.local/bin/tmux_attach_start | 0 tmux/.local/bin/tmux_pane_tree | 20 ++++++++ 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 tmux/.config/sh/alias.d/tmux.sh rename scripts/.local/bin/tm => tmux/.local/bin/tmux_attach_start (100%) create mode 100755 tmux/.local/bin/tmux_pane_tree diff --git a/tmux/.config/sh/alias.d/tmux.sh b/tmux/.config/sh/alias.d/tmux.sh new file mode 100644 index 0000000..6ec57a6 --- /dev/null +++ b/tmux/.config/sh/alias.d/tmux.sh @@ -0,0 +1,49 @@ +#!/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 diff --git a/tmux/.config/tmux/sessions/dot.session b/tmux/.config/tmux/sessions/dot.session index 3461ea5..179377b 100644 --- a/tmux/.config/tmux/sessions/dot.session +++ b/tmux/.config/tmux/sessions/dot.session @@ -7,6 +7,6 @@ select-pane -t 1 split-window -v watch -t -n 1 -c 'cd ~/.dotfiles; git -c color.ui=always log --graph --date=short --decorate --oneline --all --remotes' select-pane -t 4 new-window -n code -send-keys "cd ~/.dotfiles; vim ." C-m I +send-keys "cd ~/.dotfiles; v ." C-m I new-window -n test select-window -t 1 diff --git a/tmux/.config/tmux/tmux.conf b/tmux/.config/tmux/tmux.conf index c8445bc..598908a 100644 --- a/tmux/.config/tmux/tmux.conf +++ b/tmux/.config/tmux/tmux.conf @@ -58,7 +58,7 @@ set -g @plugin 'Morantron/tmux-fingers' set -g @plugin 'christoomey/vim-tmux-navigator' set -g @plugin 'tmux-plugins/tmux-resurrect' -if "test ! -d ${TMUX_PLUGIN_MANAGER_PATH}/tpm" \ +if "test ! -z ${TMUX_PLUGIN_MANAGER_PATH} && test ! -d ${TMUX_PLUGIN_MANAGER_PATH}/tpm" \ "run 'git clone https://github.com/tmux-plugins/tpm {TMUX_PLUGIN_MANAGER_PATH}/tpm && {TMUX_PLUGIN_MANAGER_PATH}/tpm/bin/install_plugins'" run -b "${TMUX_PLUGIN_MANAGER_PATH}/tpm/tpm" diff --git a/scripts/.local/bin/tm b/tmux/.local/bin/tmux_attach_start similarity index 100% rename from scripts/.local/bin/tm rename to tmux/.local/bin/tmux_attach_start diff --git a/tmux/.local/bin/tmux_pane_tree b/tmux/.local/bin/tmux_pane_tree new file mode 100755 index 0000000..19f38f8 --- /dev/null +++ b/tmux/.local/bin/tmux_pane_tree @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# create a tree-style view +# of all open tmux panes +# and the command they are currently running + +tmux ls -F'#{session_id}' | while read -r s; do + S=$(tmux ls -F'#{session_id}#{session_name}: #{T:tree_mode_format}' | grep ^"$s") + session_info=${S##$s} + session_name=$(echo "$session_info" | cut -d ':' -f 1) + if [[ -n "$1" ]] && [[ "$1" == "$session_name" ]]; then + echo -e "\033[1;34m$session_info\033[0m" + tmux lsw -t"$s" -F'#{window_id}' | while read -r w; do + W=$(tmux lsw -t"$s" -F'#{window_id}#{T:tree_mode_format} - #{pane_current_command}' | grep ^"$w") + H=$(tmux lsw -t"$s" -F'#{window_id}#H' | grep ^"$w") + echo " ﬌ ${W##$w}" | sed "s/\"${H##$w}\" //" + done + else + echo -e "\033[1m$session_info\033[0m" + fi +done