aboutsummaryrefslogtreecommitdiff
path: root/linux/home/.config/tmux/tmux.conf
blob: 8a821e67f5fdcf5bb5b73ae300da0e96dd4a9fb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
#  ████████╗███╗   ███╗██╗   ██╗██╗  ██╗
#  ╚══██╔══╝████╗ ████║██║   ██║╚██╗██╔╝
#     ██║   ██╔████╔██║██║   ██║ ╚███╔╝
#     ██║   ██║╚██╔╝██║██║   ██║ ██╔██╗
#     ██║   ██║ ╚═╝ ██║╚██████╔╝██╔╝ ██╗
#     ╚═╝   ╚═╝     ╚═╝ ╚═════╝ ╚═╝  ╚═╝
#――――――――――――――――――――――――――――――――――――――――――

### Options ###

# Setting the Prefix from Ctrl+b to Ctrl+s
unbind C-b
set -g prefix C-s
#set -g prefix M-Space

# Ensure that we can send Ctrl+s to other apps
bind C-s send-prefix

# Check if in (n)vim
is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

# Send the prefix to client inside window (nested tmux)
#bind-key -n C-a send-prefix
bind -n C-a if-shell "$is_vim" "send-keys C-a" "send-prefix"

# Disable local tmux keys (nested tmux)
bind -T root F12 \
  set prefix None \;\
  set key-table off \;\
  if -F '#{pane_in_mode}' 'send-keys -X cancel' \;\
  refresh-client -S \;\

bind -T off F12 \
  set -u prefix \;\
  set -u key-table \;\
  refresh-client -S

# Toggle Sync Panes
bind C-y set-window-option synchronize-panes\; display-message "Synchronize-Panes is now #{?pane_synchronized,on,off}"

# Setting the delay between Prefix and Command
set -sg escape-time 10
#set-option -sg escape-time 10

# Reload tmux with <Prefix>r
bind r source-file ~/.config/tmux/tmux.conf \; display "Reloaded!"

# Use <Prefix>L to clear terminal
#bind -r L send-keys C-l \; send-keys -R \; clear-history
bind -r L send-keys C-l \; send-keys 'Enter'
#bind-key -n C-L if-shell "$is_vim" "send-keys C-l"  "send-keys C-l"

# Rename current window (Ctrl + A, A)
bind R rename-window '' \; \
        command-prompt -I "#W" "rename-window -- '%%'"

# Auto rename pane
set -wg automatic-rename on
set -g automatic-rename-format "#{pane_current_command}"

# Renumber all windows when one is killed
set -g renumber-windows on

# Mouse
set -g mouse on

# Right Click Menu
bind -n MouseDown3Pane \
  if-shell -F "#{||:#{match:#{pane_current_command},nvim},#{match:#{pane_current_command},vim}}" \
    "send-keys -M" \
    "display-menu -t= -xM -yM -T '#[align=centre]#{pane_index} (#{pane_id})' \
    'Vim' v 'send-keys \"vi\" Enter' \
    'File-manager' f 'send-keys \"yazi\" Enter' \
    '' \
    'Horizontal Split' '\\\\' 'split-window -h -c \"#{pane_current_path}\"' \
    'Vertical Split' '-' 'split-window -v -c \"#{pane_current_path}\"' \
    '' \
    'Copy Mode' 'C' 'copy-mode -e' \
    'Paste' 'p' {send-keys C-v} \
    'Open with xdg-open' 'o' 'send-keys \"xdg-open #{pane_current_path}\"' \
    'Enter' 'e' 'send-keys Enter' \
    '' \
    'Go To Top' '↑' 'copy-mode -e; send-keys gg' \
    'Go To Bottom' '↓' 'copy-mode -e; send-keys G' \
    '' \
    'Break Pane' 't' 'break-pane' \
    'Join Pane' 'j' 'choose-window \"join-pane -h -s %%\"' \
    '#{?pane_marked,Unmark,Mark}' 'm' 'select-pane -m' \
    '#{?#{>:#{window_panes},1},,-}Swap Up' 'u' 'swap-pane -U' \
    '#{?#{>:#{window_panes},1},,-}Swap Down' 'd' 'swap-pane -D' \
    '' \
    'New Window' 'n' 'new-window' \
    'Previous Window' '🡠' 'previous-window' \
    'Next Window' '🡢' 'next-window' \
    'Swap Window Left' '<' 'swap-window -t -1' \
    'Swap Window Right' '>' 'swap-window -t +1' \
    '' \
    'Kill' 'X' 'kill-pane' \
    'Respawn' 'R' 'respawn-pane -k' \
    'Interrupt (Ctrl+C)' 'c' 'send-keys C-c' \
    '#{?#{>:#{window_panes},1},,-}#{?window_zoomed_flag,Unzoom,Zoom}' 'z' 'resize-pane -Z'"


# Disable copy on primary selection (prevents auto-copy on mouse drag release)
unbind -T root MouseDrag1Pane
unbind -T copy-mode MouseDragEnd1Pane
unbind -T copy-mode-vi MouseDragEnd1Pane

# Left-click to select a pane (no copy mode)
bind -T root MouseDown1Pane {
  select-pane -t=  # Select pane under mouse
  send-keys -M    # Enable mouse interaction in pane
}

# Don't go into copy-mode on first click/drag
bind -T root MouseDrag1Pane if-shell '[ "#{mouse_flags}" = "drag" ]' {
    if-shell '[ "#{pane_id}" = "#{mouse_pane}" ]' {
        copy-mode
        send-keys -X begin-selection
    } {
        select-pane -t=
        send-keys -M
    }
} {
    select-pane -t=
    send-keys -M
}

# Left-click to start selection in copy-mode
bind -T root DoubleClick1Pane {
  copy-mode
  send-keys -X begin-selection
}

# Left-click to start selection in copy-mode
bind -T copy-mode-vi MouseDown1Pane {
  send-keys -X cancel-selection
  send-keys -X begin-selection
}

# Left-click drag to start selection in copy-mode
bind -T copy-mode-vi MouseDown1Pane {
  send-keys -X begin-selection
}

# Right click to copy the selected text and exit copy mode
bind -T copy-mode-vi MouseDown3Pane {
  send-keys -X copy-pipe-no-clear
  send-keys -X end-selection
  send-keys Escape
}

# Middle click exit copy mode without copying (cancel the selection)
bind -T copy-mode-vi MouseDown2Pane {
  send-keys -X end-selection
  send-keys Escape
}

# Middle click paste if not in copy-mode
# Conditional behavior for MouseDown2Pane
if-shell '[ "$CTRL_V_PASTE" = "true" ]' \
  "bind -T root MouseDown2Pane send-keys C-v" \
  "bind -T root MouseDown2Pane send-keys C-S-v"

## Right-click drag to exit copy mode without selecting text
#bind -T copy-mode-vi MouseDragEnd3Pane {
#  send-keys -X end-selection  # End selection
#  send-keys Escape  # Exit copy mode
#}

# Make mouse-drag work only with Ctrl
#unbind -T root MouseDrag1Pane
#unbind -T copy-mode-vi MouseDrag1Pane
#bind -n C-MouseDrag1Pane if -Ft= \
#'#{mouse_any_flag}' 'if -Ft= \"#{pane_in_mode}\" \"copy-mode -M\" send-keys -X \"send-keys -M\"' 'copy-mode -M'
#bind -T copy-mode-vi C-MouseDrag1Pane send-keys -X begin-selection

# Exiting copy mode without copying (Left-click to exit)
#bind -T copy-mode-vi MouseDown1Pane {
#  select-pane -t=  # Deselect pane
#  send-keys Escape  # Exit copy mode
#}


# Scrollback
set -g history-limit 10000

# Sane scrolling
set -g terminal-overrides 'xterm*:smcup@:rmcup@'

# Sane scrolling
bind -n WheelUpPane {
  if -F '#{==:#{window_name},nvim}' {
        #send-keys -M
        send-keys Up
    } {
        copy-mode -e
    }
}
bind -n WheelDownPane {
  if -F '#{==:#{window_name},nvim}' {
        #send-keys -M
        send-keys Down
    } {
        copy-mode -e
    }
}

# Focus events, allow supported requests from applications to passthrough/run in tmux
set-option -g focus-events on

# Update the TERM variable of terminal emulator when creating a new session or attaching a existing session
set -g update-environment 'DISPLAY SSH_ASKPASS SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY TERM'

# New session
#bind -n M-N new-session


# Lock
#set-option -g lock-command "vlock -c"
#set -g lock-after-time 910 # Seconds; 0 = never
#bind -n M-Escape lock-session


#――――――――――――――――――――――――――――――――――――――――――

### Window Movement/Control ###

# Aggressive resizing, useful when using "grouped sessions" and multi-monitor setup
setw -g aggressive-resize on

# Menu
bind-key -n M-m display-menu -x W -y S \
    "New Session"                        S "command-prompt -p \"New Session:\" \"new-session -A -s '%%'\"" \
    "Kill Session"                       x "kill-session" \
    "Kill Other Session(s)"              X "kill-session -a" \
    "" \
    "New Window"                         ␍ new-window \
    "Kill Window"                        k "killw"  \
    "Choose Window"                      w choose-window \
    "Previous Window"                    🡠 previous-window \
    "Next Window"                        🡢 next-window \
    "Swap Window Right"                  ↑ "swap-window -t -1" \
    "Swap Window Left"                   ↓ "swap-window -t +1" \
    "Horizontal Split"                   v "split-window -h" \
    "Vertical Split"                     s "split-window -v"  \
    "" \
    "Layout Horizontal"                  h "select-layout even-horizontal"  \
    "Layout Vertical"                    k "select-layout even-horizontal"  \
    "" \
    "Swap Pane Up"                       < "swap-pane -U" \
    "Swap Pane Down"                     > "swap-pane -D" \
    "Break Pane"                         t break-pane \
    "Join Pane"                          j "choose-window 'join-pane -h -s \"%%\"'" \
    "#{?window_zoomed_flag,Unzoom,Zoom}" z "resize-pane -Z"


# Popup Pane Manager
bind-key -n M-/ run-shell ~/.config/tmux/tmux-popup-pane-manager.sh

# List sessions

#bind M-q display-popup -E -w 75% -h 75% "\
#  tmux list-sessions -F '#{?session_attached,,#{session_name}}' |\
#  sed '/^$/d' |\
#  fzf --reverse --header jump-to-session --preview 'tmux capture-pane -pt {}'  |\
#  xargs tmux switch-client -t"

# Kill sessions
bind M-q display-popup -E "\
    tmux list-sessions -F '#{?session_attached,,#{session_name}}' |\
    fzf --reverse -m --header=kill-session |\
    xargs -I {} tmux kill-session -t {}"

# List sessions
bind -n M-Space display-popup -E "\
    tmux list-sessions -F '#{?session_attached,,#{session_name}}' |\
    sed '/^$/d' |\
    fzf --reverse --header jump-to-session --preview 'tmux capture-pane -pt {}'  |\
    xargs tmux switch-client -t"

# Session chooser
#bind -n M-q choose-tree -Zs -O time

# Quick window select
bind -n M-? list-keys
bind -n M-0 select-window -t :=0
bind -n M-1 select-window -t :=1
bind -n M-2 select-window -t :=2
bind -n M-3 select-window -t :=3
bind -n M-4 select-window -t :=4
bind -n M-5 select-window -t :=5
bind -n M-6 select-window -t :=6
bind -n M-7 select-window -t :=7
bind -n M-8 select-window -t :=8
bind -n M-9 select-window -t :=9

# move pane to existing window or create it
bind-key -n M-! run -C '#{?#{m:*|1|*,|#{W:#I|}},joinp -ht :1,breakp -t :1}'
bind-key -n M-@ run -C '#{?#{m:*|2|*,|#{W:#I|}},joinp -ht :2,breakp -t :2}'
bind-key -n M-# run -C '#{?#{m:*|3|*,|#{W:#I|}},joinp -ht :3,breakp -t :3}'
bind-key -n M-$ run -C '#{?#{m:*|4|*,|#{W:#I|}},joinp -ht :4,breakp -t :4}'
bind-key -n M-% run -C '#{?#{m:*|5|*,|#{W:#I|}},joinp -ht :5,breakp -t :5}'
bind-key -n M-^ run -C '#{?#{m:*|6|*,|#{W:#I|}},joinp -ht :6,breakp -t :6}'
bind-key -n M-& run -C '#{?#{m:*|7|*,|#{W:#I|}},joinp -ht :7,breakp -t :7}'
bind-key -n M-* run -C '#{?#{m:*|8|*,|#{W:#I|}},joinp -ht :8,breakp -t :8}'
bind-key -n M-( run -C '#{?#{m:*|9|*,|#{W:#I|}},joinp -ht :9,breakp -t :9}'
bind-key -n M-) run -C '#{?#{m:*|0|*,|#{W:#I|}},joinp -ht :0,breakp -t :0}'

#――――――――――――――――――――――――――――――――――――――――――

### Pane Movement/Control ###

# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator
# Navigate across tmux-vim

# {{{ keybinds: select-pane
#bind -Tnav h   select-pane -L
#bind       h   select-pane -L
#bind -Tnav C-h select-pane -L
#bind       C-h select-pane -L
#bind -Tnav j   select-pane -D
#bind       j   select-pane -D
#bind -Tnav C-j select-pane -D
#bind       C-j select-pane -D
#bind -Tnav k   select-pane -U
#bind       k   select-pane -U
#bind -Tnav C-k select-pane -U
#bind       C-k select-pane -U
#bind -Tnav l   select-pane -R
#bind       l   select-pane -R
#bind -Tnav C-l select-pane -R
#bind       C-l select-pane -R

# {{{ keybinds: select-pane
# Smart pane switching with Vim awareness
is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(n?vim|vimx?)'"

bind -n C-h if-shell "$is_vim" "send-keys C-\\ C-n C-h" "select-pane -L"
bind -n C-j if-shell "$is_vim" "send-keys C-\\ C-n C-j" "select-pane -D"
bind -n C-k if-shell "$is_vim" "send-keys C-\\ C-n C-k" "select-pane -U"
bind -n C-l if-shell "$is_vim" "send-keys C-\\ C-n C-l" "select-pane -R"
# keybinds: select-pane }}}

# {{{ keybinds: split-window
bind -Tnav "\\" split-window -h  -c "#{pane_current_path}" # vertical
bind       "\\" split-window -h  -c "#{pane_current_path}" # vertical
bind -Tnav "|"  split-window -fh -c "#{pane_current_path}" # full vertical
bind       "|"  split-window -fh -c "#{pane_current_path}" # full vertical
bind -Tnav "-"  split-window -v  -c "#{pane_current_path}" # horizontal
bind       "-"  split-window -v  -c "#{pane_current_path}" # horizontal
bind -Tnav "_"  split-window -fv -c "#{pane_current_path}" # full horizontal
bind       "_"  split-window -fv -c "#{pane_current_path}" # full horizontal
# keybinds: split-window }}}

# {{{ keybinds: resize-pane
bind -r -Tnav M-h resize-pane -L 10
bind -r       M-h resize-pane -L 10
bind -r -Tnav M-j resize-pane -D 5
bind -r       M-j resize-pane -D 5
bind -r -Tnav M-k resize-pane -U 5
bind -r       M-k resize-pane -U 5
bind -r -Tnav M-l resize-pane -R 10
bind -r       M-l resize-pane -R 10
# keybinds: resize-pane }}}

# Hide a pane and bring it back with <Prefix>! and <Prefix>@ respectively
bind-key ! break-pane -d -n _hidden_pane
bind-key @ join-pane -s $.0

# Send Pane to another session/window with <Prefix>=
bind-key = command-prompt -p "send pane to:"  "join-pane -t '%%'"

#――――――――――――――――――――――――――――――――――――――――――

### Popup ###

# Toggle popup "term" session
set -g @term_session_name "term"
bind -n M-t if-shell -F '#{==:#{session_name},term}' {
    detach-client
  } {
    if-shell "tmux has-session -t term" {
      set -gF '@last_session_name' '#S'  # Store the current session name
      display-popup -E -xC -yC -w60% -h60% "tmux attach-session -t term"
    } {
      set -gF '@last_session_name' '#S'  # Store the current session name
      display-popup -E -xC -yC -w60% -h60% "tmux new-session -d -c '#{pane_current_path}' -s term && tmux set-option -t term status off && tmux attach-session -t term"
    }
}


## Toggle popup "note" session
bind-key -n M-n run-shell ~/.config/tmux/tmux-notes-menu.sh
bind-key -n M-N run-shell "~/.config/tmux/tmux-notes-menu.sh --new"

# Toggle popup "pack" session
bind-key -n M-p if-shell -F '#{==:#{session_name},pack}' {
    detach-client
} {
    set -gF '@last_session_name' '#S'  # Store the current session name
    #display-popup -w 80% -h 80% -E "tmux new-session -A -s pack"
    display-popup -E -x200% -y0 -w50% -h99% "tmux new-session -A -s pack"
}

# M-T → Open TODO.md in popup
bind-key -n M-o if-shell -F '#{==:#{session_name},todo}' {

    detach-client
} {
    set -gF '@last_session_name' '#S'
    display-popup -E -x200% -y0 -w50% -h99% "tmux new-session -A -s todo 'nvim ~/documents/main/inbox/tasks/TODO.md'"
}

# M-H → Open history in popup
bind-key -n M-H if-shell -F '#{==:#{session_name},hist}' {
    detach-client
} {
    set -gF '@last_session_name' '#S'
    display-popup -E -x200% -y0 -w50% -h99% "tmux new-session -A -s hist \"bash -c \'
        shell=\\\$(basename \\\$SHELL)
        if [[ \\\$shell == zsh ]]; then
            if [[ -f \\\$HOME/.config/zsh/.zhistory ]]; then
                nvim \\\$HOME/.config/zsh/.zhistory
            elif [[ -f \\\$HOME/.zhistory ]]; then
                nvim \\\$HOME/.zhistory
            else
                echo \\\"No Zsh history found\\\"; sleep 5
            fi
        elif [[ \\\$shell == bash ]]; then
            if [[ -f \\\$HOME/.bash_history ]]; then
                nvim \\\$HOME/.bash_history
            else
                echo \\\"No Bash history found\\\"; sleep 5
            fi
        else
            echo \\\"Unknown shell: \\\$shell\\\"; sleep 5
        fi
    \'\""
}

# Move popup session (note, term, or pack) to scratchpad (tmux)/last session or back to respective popup
bind -n M-x run-shell '
    current_session=$(tmux display-message -p "#S");

    # Check if the current session is one of the popups (note, term, pack)
    if [ "$current_session" = "note" ] || [ "$current_session" = "term" ] || [ "$current_session" = "pack" ]; then
        # Detach, break the pane out of the popup, and join it to the last session
        tmux detach-client;
        tmux break-pane;
        last_session=$(tmux show -gvq @last_session_name || echo tmux);
        tmux set -g @last_popup_session "$current_session";  # Save which popup it was
        tmux join-pane -s "$current_session" -t "$last_session" -h;
    else
        # If not in one of the popups, move the last popup session back to its popup
        last_popup=$(tmux show -gqv @last_popup_session);

        if [ "$last_popup" = "note" ]; then
            if ! tmux has-session -t note; then
                # Spawn note session in detached mode without blocking
                tmux new-session -d -s note;
            fi
            tmux join-pane -s $(tmux display-message -p "#P") -t note;
            tmux kill-pane -a -t note:0;
        elif [ "$last_popup" = "term" ]; then
            if ! tmux has-session -t term; then
                tmux new-session -d -s term;
            fi
            tmux join-pane -s $(tmux display-message -p "#P") -t term;
            tmux kill-pane -a -t term:0;
        elif [ "$last_popup" = "pack" ]; then
            if ! tmux has-session -t pack; then
                tmux new-session -d -s pack;
            fi
            tmux join-pane -s $(tmux display-message -p "#P") -t pack;
            tmux kill-pane -a -t pack:0;
        fi
    fi'
# TODO: add keybinding that will allow going across the dipslay popup and main tmux session
# TODO: add keybinding for toggling the display popup size to/from fullscreen and it's original size

# Toggle popup "fzf" session
#bind-key -n M-o run-shell ~/.config/tmux/fzf-menu.sh

# Toggle popup "htop" session
bind -n M-i if-shell -F '#{==:#{session_name},htop}' {
    detach-client
  } {
    if-shell "tmux has-session -t HUD" {
      display-popup -E -x200% -y0 -w40% -h40% "tmux new-session -A -s HUD"
    } {
      display-popup -E -x200% -y0 -w40% -h40% "tmux new-session -A -s htop 'htop'"
    }
}

# Toggle popup "lazygit" session
bind-key -n M-g if-shell -F '#{==:#{session_name},lazygit}' {
    detach-client
} {
    display-popup -w 90% -h 90% -E "tmux new-session -A -s lazygit 'lazygit'"
}

## Toggle popup "yazi" session
#bind-key -n M-f if-shell -F '#{==:#{session_name},yazi}' {
#    detach-client
#} {
#    display-popup -w 90% -h 90% -E "tmux new-session -A -s yazi 'yazi'"
#}
#tmux split-window -h -p 30 \
#    "NNN_OPENER=~/.config/tmux/file_manager.sh nnn"

#bind-key -n M-f run-shell 'tmux split-window -hb -p 30 "~/.config/tmux/file_manager.sh $(tmux display-message -p "#I.#P")"'

# Robust file manager toggle - checks both title and option
bind-key -n M-f run-shell "\
fm_pane_title=\$(tmux list-panes -F '#{pane_id} #{pane_title}' | awk '\$2==\"FILE_MANAGER\" {print \$1}'); \
fm_pane_option=\$(tmux list-panes -F '#{pane_id} #{@file_manager}' | awk '\$2==\"1\" {print \$1}'); \
fm_pane=\${fm_pane_title:-\$fm_pane_option}; \
if [ -n \"\$fm_pane\" ]; then \
    tmux kill-pane -t \"\$fm_pane\"; \
else \
    current_pane=\$(tmux display-message -p '#{session_name}:#{window_index}.#{pane_index}'); \
    tmux split-window -hb -p 30 \"~/.config/tmux/file_manager.sh \$current_pane\"; \
fi"

## Toggle popup "speedtest" session
#bind-key -n M-s if-shell -F '#{==:#{session_name},speedtest}' {
#    detach-client
#} {
#    display-popup -E "tmux new-session -A -s speedtest 'speedtest'"
#}


# Toggle popup "nvim" session
bind-key -n M-v if-shell -F '#{==:#{session_name},edit}' {
    detach-client
} {
    display-popup -w 100% -h 100% -E "tmux new-session -A -s edit 'cd && nvim'"
}


# Toggle popup "ssh" session
bind-key -n M-s if-shell -F '#{==:#{session_name},ssh}' {
    detach-client
} {
    display-popup -w 40% -h 50% -E "tmux new-session -A -s ssh"
}

# Toggle popup session to fullscreen
bind-key -n M-F run-shell '
    popup_session=$(tmux display-message -p "#{session_name}");
    if [[ "$popup_session" == "term" || "$popup_session" == "note" || "$popup_session" == "lazygit" || "$popup_session" == "yazi" || "$popup_session" == "speedtest" || "$popup_session" == "nvim" || "$popup_session" == "ssh" || "$popup_session" == "pack" || "$popup_session" == "htop" ]]; then
        tmux detach-client
        sleep 0.1
        tmux display-popup -E -w 100% -h 100% "tmux new-session -A -s $popup_session"
    fi
'

bind -n M-L lock-session

#bind-key -n M-o display-popup -E nvim -c ":ObsidianNew"
#bind-key -n M-/ display-popup -w "90%" -h "85%" -E nvim -c ":ObsidianSearch"


#proc
#netw
#ssh


#――――――――――――――――――――――――――――――――――――――――――

### Copy Mode (Copy/Paste) ###

# Set Vi copy mode, use <prefix>[ to enter copy mode
setw -g mode-keys vi # `<prefix>:list-keys -T copy-mode-vi` to confirm

# super fast way to reach copy-mode and search upwards
bind-key / copy-mode \; send-key ?

# Alt + Space copy-mode without prefix
bind-key -n M-c copy-mode

# Shift up/down copy-mode without prefix
bind-key -n S-Up copy-mode \; send-key Up
bind-key -n S-Down copy-mode \; send-key Down
bind-key -n Pageup copy-mode \; send-key Pageup
bind-key -n Pagedown copy-mode \; send-key Pagedown
#bind -n Pageup if-shell "$is_vim" "send-keys Pageup" "copy-mode -u"
#bind -n S-Pageup if-shell "$is_vim" "send-keys Pageup" "copy-mode -u"
bind -n Pageup if-shell "$is_vim" "send-keys Pageup"
bind -n S-Pageup if-shell "$is_vim" "send-keys Pageup"
bind -n S-Pagedown send-keys Pagedown

# Change selection <space> and enter to vi keybinding
bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-selection-and-cancel
bind -T copy-mode-vi Escape send -X cancel
bind -T copy-mode-vi C-c send -X clear-selection
bind -T copy-mode-vi C-v send -X begin-selection \; send-keys -X rectangle-toggle

# Unbind any previous 'y' in copy-mode-vi to prevent conflicts
unbind -T copy-mode-vi 'y'

# Clipboard copy for X11/XWayland
if-shell -b '[ "$DISPLAY" ] && command -v xclip >/dev/null' "\
    bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard > /dev/null 2>&1'"

# Clipboard copy for Wayland
if-shell -b '[ \"$WAYLAND_DISPLAY\" ] && command -v wl-copy >/dev/null' "\
    bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe-and-cancel 'wl-copy --foreground --type text/plain > /dev/null 2>&1'"

# macOS clipboard copy
if-shell -b '[ \"$(uname -s)\" = \"Darwin\" ] && command -v pbcopy >/dev/null' "\
    bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe-and-cancel 'pbcopy'"

# Windows (WSL) clipboard copy
if-shell -b 'uname -r | grep -qi microsoft && command -v clip.exe >/dev/null' "\
    bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe-and-cancel 'clip.exe > /dev/null 2>&1'"

# For OSC-Yank in vim to work over ssh
# https://github.com/ojroques/vim-oscyank
#set -s set-clipboard on
#set -g set-clipboard on
#set -g terminal-overrides 'xterm*:paste:Ctrl+Shift+V'

set -g allow-passthrough on
set extended-keys on

# notify when a window has activity
set-window-option -g monitor-activity on

# Copy mode search with a simple shortcut (@see https://superuser.com/a/1253137)
bind-key / copy-mode \; send-key ?

#――――――――――――――――――――――――――――――――――――――――――

### Colors ###

## Assume external terminal supports the 256 colors palette (when TERM=xterm-256color)
#set -sa terminal-features ",xterm-256color:256"
## Assume external terminal supports RGB colors (when TERM=xterm-256color)
#set -sa terminal-features ",xterm-256color:RGB"
#
## Set TERM for proper colors
#set -g default-terminal "tmux-256color"
#set -g default-terminal "xterm-256color"

# Set 256 color terminal
#set-option -sa terminal-overrides ",tmux-256color:Tc"
#set-option -sa terminal-overrides ",xterm*:Tc,alacritty*:Tc"

# Colors for pane borders(default)
setw -g pane-border-style fg=white
setw -g pane-active-border-style fg=green

# Active pane normal, other shaded out
setw -g window-style fg=colour240
setw -g window-active-style fg=white

# Popup border
set -g popup-border-lines rounded

#

#――――――――――――――――――――――――――――――――――――――――――

### Status ###

set -g status-position bottom # [top, bottom]
set -g status on
#set -g status-interval 1
set -g status-interval 500
#set -g status-style fg=#50fa7b,bg=default

# Toggle status
bind T set status

# Inactive windows
set -g status-style bg=default
set -g window-status-activity-style noreverse
set -g window-status-format "#[fg=#ffffff,bg=default]#I: #W"

## Left
set -g status-left ""
set -g status-left-length 60

## Center
set -g status-justify absolute-centre

## Right
set -g status-right-length 60
set-option -g status-right ""


### Status Style
set -g status-style "bg=default,fg=#CDD6F4"
#set -g status-left-length 100
#set -g status-right-length 150

### Left
set -g status-left '#( \
  if fc-list | grep -qi nerd; then \
    cat ~/.vi-mode | awk '\''/-- NORMAL --/ {print "#[fg=#39BAE6]#[bg=default]#[bg=#39BAE6,fg=#000000]  ❐ #S  #[fg=#39BAE6,bg=default]"} \
                      /-- INSERT --/ {print "#[fg=#50fa7b]#[bg=default]#[bg=#50fa7b,fg=#000000]  ❐ #S  #[fg=#50fa7b,bg=default]"}'\''; \
  else \
    cat ~/.vi-mode | awk '\''/-- NORMAL --/ {print "#[bg=default,fg=#39BAE6]  #S  "} \
                      /-- INSERT --/ {print "#[bg=default,fg=#50fa7b]  #S  "}'\''; \
  fi \
)'

### Center
set -g window-status-current-format '#( \
  if fc-list | grep -qi nerd; then \
    cat ~/.vi-mode | awk '\''/-- NORMAL --/ {print "#[fg=#39BAE6]#[bg=default]#[fg=#000000,bg=#39BAE6] #I:#W #[bg=default,fg=#39BAE6]"} \
                      /-- INSERT --/ {print "#[fg=#50fa7b]#[bg=default]#[fg=#000000,bg=#50fa7b] #I:#W #[bg=default,fg=#50fa7b]"}'\''; \
  else \
    cat ~/.vi-mode | awk '\''/-- NORMAL --/ {print "#[fg=#39BAE6,bg=default] #I:#W "} \
                      /-- INSERT --/ {print "#[fg=#50fa7b,bg=default] #I:#W "}'\''; \
  fi \
)'

### Right
set -g status-right '#( \
  key_off="#[fg=#50fa7b,bg=default]#([ $(tmux show-option -qv key-table) = off ] && echo KEYS\ OFF)#[default]"; \
  vi_segment=$(cat ~/.vi-mode | awk '\''/-- NORMAL --/ {print "#[fg=#50fa7b,bg=default] #[fg=#39BAE6]#[bg=#39BAE6,fg=#000000]  %H:%M  #[bg=default,fg=#39BAE6]"} \
                               /-- INSERT --/ {print "#[fg=#39BAE6,bg=default] #[fg=#50fa7b]#[bg=#50fa7b,fg=#000000]  %H:%M  #[bg=default,fg=#50fa7b]"}'\'')

  if fc-list | grep -qi nerd; then \
    echo "$key_off $vi_segment"; \
  else \
    cat ~/.vi-mode | awk '\''/-- NORMAL --/ {print "#[fg=#50fa7b,bg=default] #[fg=#39BAE6] %H:%M "} \
                      /-- INSERT --/ {print "#[fg=#39BAE6,bg=default] #[fg=#50fa7b] %H:%M "}'\''; \
  fi \
)'

# Force refresh after session creation
#set-hook -g after-new-session 'source-file ~/.tmux.conf'
#set-hook -g client-attached 'source-file ~/.tmux.conf'


# Set environment variable from script output
#run-shell 'tmux set-environment -g NERD_FONT_DETECTED "$(~/.config/tmux/detect_nerd_font)"'

## Reload Status with IP addr, Cpu, Mem and Date
bind a run-shell ~/.config/tmux/tmux-toggle-option.sh

## Numbers
bind < command-prompt -p index "run-shell '~/.config/tmux/tmux_number.sh %%'"

## Reload Config/Status silently
bind b source-file ~/.config/tmux/tmux.conf

set-option -g default-shell "/usr/bin/zsh"


#――――――――――――――――――――――――――――――――――――――――――

### Plugin Install ###

set -g @plugin "tmux-plugins/tpm"
set -g @plugin "tmux-plugins/tmux-sensible"
set -g @plugin "tmux-plugins/tmux-resurrect"
set -g @plugin "tmux-plugins/tmux-continuum"
#set -g @plugin "loichyan/tmux-toggle-popup"
set -g @plugin "christoomey/vim-tmux-navigator"
#set -g @plugin "tmux-plugins/tmux-yank"
#set -g @plugin 'srdusr/tmux-vi-mode'
#set -g @plugin 'vi-mode ~/.config/tmux/plugins/vi-mode.sh'


set -g @plugin 'catppuccin/tmux'
set -g @plugin 'tmux-plugins/tmux-online-status'
set -g @plugin 'tmux-plugins/tmux-battery'

#――――――――――――――――――――――――――――――――――――――――――

### Plugins Settings ###

# Plugin to save and restore tmux sessions after restart
# * Save with: <Prefix> + Ctrl-s
# * Restore with: <Prefix> + Ctlr-r
# Change default save and restore keybindings
  set -g @resurrect-save "W" # <Prefix> + W
  set -g @resurrect-restore "E" # <Prefix> + E

# Restore vim and nvim sessions as well
# For vim:
  set -g @resurrect-strategy-vim "session"
# For neovim:
  set -g @resurrect-strategy-nvim "session"

# Automatic restore
  #set -g @continuum-restore "on"
  #set -g @continuum-boot "on"

# Restore Panes
#  set -g @resurrect-capture-pane-contents "on"

# This is a hook for tmux-resurrect which tells it to kill session 0 before restoring the panels
  set -g @resurrect-hook-pre-restore-pane-processes "tmux switch-client -n && tmux kill-session -t=0"

# Tmux navigation
  #set -g @plugin 'christoomey/vim-tmux-navigator'
# Plugin uninstall
  # Delete or comment out the plugin in .tmux.conf.
  # Press <prefix> + alt + u to remove the plugin.

# >>>>> CATPPUCCIN CONFIGS <<<<<

# Configure Catppuccin
set -g @catppuccin_flavor "mocha"
set -g @catppuccin_status_background "none"
set -g @catppuccin_window_status_style "none"
set -g @catppuccin_pane_status_enabled "off"
set -g @catppuccin_pane_border_status "off"

# Configure Online status
set -g @online_icon "ok"
set -g @offline_icon "nok"

# TMUX plugin manager (keep at the bottom of tmux.conf)
  #run "~/.config/tmux/plugins/tpm/tpm"
  if "test ! -d ~/.config/tmux/plugins/tpm" \
   "run 'git clone https://github.com/tmux-plugins/tpm ~/.config/tmux/plugins/tpm && ~/.config/tmux/plugins/tpm/bin/install_plugins'"
run -b "~/.config/tmux/plugins/tpm/tpm"