From be7baeb3222b500b17d2e9da6f439a0d4dbab79f Mon Sep 17 00:00:00 2001
From: Tony Blyler <tony.blyler@cbinsights.com>
Date: Fri, 13 May 2022 12:09:18 -0400
Subject: [PATCH] Add nice fzf methods to .tmux.conf

---
 chezmoi/dot_tmux.conf | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/chezmoi/dot_tmux.conf b/chezmoi/dot_tmux.conf
index 3714acb..f88ae80 100644
--- a/chezmoi/dot_tmux.conf
+++ b/chezmoi/dot_tmux.conf
@@ -68,7 +68,41 @@ set -g status-right "#(acpi | tr -d , | awk 'NR==1{printf \"%%s %%s < \", $3, $4
 bind C-c new-session
 
 # find session
-bind C-f command-prompt -p find-session 'switch-client -t %%'
+bind C-f new-window -n "session-switcher" "\
+    tmux list-sessions -F '#{?session_attached,,#{session_name}}' |\
+    awk 'NF>0{print}' |\
+    fzf --print0 --reverse --header jump-to-session --preview 'tmux capture-pane -pt {}' |\
+    xargs -0 tmux switch-client -t"
+
+bind C-n new-window -n "window-switcher" "\
+    tmux list-windows -F '#{?window_active,,#{session_id}:#{window_index} #{session_name} > #{window_name}}' |\
+    awk 'NF>0{print}' |\
+    fzf --reverse --header jump-to-window --preview \"tmux capture-pane -pt {1}\" |\
+    awk '{print $1}' |\
+    xargs tmux switch-client -t"
+
+bind C-w new-window -n "global-window-switcher" "\
+    CURRENT_INFO=\"\$(tmux display-message -p '0:#{session_id}:#{window_index}')\";\
+    tmux list-windows -aF '#{window_last_flag}:#{session_id}:#{window_index} #{session_name} > #{window_name}' |\
+    awk -v cur_info_raw=\"\$CURRENT_INFO\" 'BEGIN{split(cur_info_raw,cur_info,\":\")} {split($1,this_info,\":\"); if($1 == cur_info_raw || (this_info[1] && this_info[2] == cur_info[2])) next; print gensub(\"^[01]:\",\"\",1)}' |\
+    fzf --reverse --header global-jump-to-window --preview 'tmux capture-pane -pt {1}' |\
+    awk '{print $1}' |\
+    xargs tmux switch-client -t"
+
+bind C-j new-window -n "pane-switcher" "\
+    tmux list-panes -sF '#{?#{&&:#{window_active},#{pane_active}},,#{session_id}:#{window_index}.#{pane_index} #{session_name} > #{window_name} > #{pane_title}}' |\
+    awk 'NF>0{print}' |\
+    fzf --reverse --header jump-to-pane --preview 'tmux capture-pane -pt {1}' |\
+    awk '{print $1}' |\
+    xargs tmux switch-client -t"
+
+bind C-k new-window -n "global-pane-switcher" "\
+    CURRENT_INFO=\"\$(tmux display-message -p '0:#{pane_active}:#{session_id}:#{window_index}.#{pane_index}')\";\
+    tmux list-panes -aF '#{window_last_flag}:#{pane_active}:#{session_id}:#{window_index}.#{pane_index} #{session_name} > #{window_name} > #{pane_title}' |\
+    awk -v cur_info_raw=\"\$CURRENT_INFO\" 'BEGIN{split(cur_info_raw,cur_info,\":\")} {split($1,this_info,\":\"); if($1 == cur_info_raw || (this_info[1] && this_info[3] == cur_info[3] && this_info[2] == cur_info[2])) next; print gensub(\"^[01]:[01]:\",\"\",1)}' |\
+    fzf --reverse --header jump-to-pane --preview 'tmux capture-pane -pt {1}' |\
+    awk '{print $1}' |\
+    xargs tmux switch-client -t"
 
 # pane navigation
 bind -r h select-pane -L  # move left