Tmux

From "PTTLink Wiki"
Revision as of 06:34, 13 March 2021 by Kg7qin (talk | contribs) (Created initial page.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page is for collecting various snippets and pieces for use with tmux.

Quick cheat sheet of helpful tmux commands

Source: [1]

  1. tmux new - Create and attach to a new session.
  2. tmux new -s NAME_HERE - Create and attach to a new session named NAME_HERE.
  3. CTRL-b, d - Detach (i.e. exit) from the currently-opened tmux session (alternatively, `tmux detach`). Note, this means press and hold `CTRL`, press `b`, release both, press `d`.
  4. tmux ls - Show list of tmux sessions.
  5. tmux a - Attach to the previously-opened tmux session.
  6. tmux a -t NAME_HERE - Attach to the tmux session named NAME_HERE.
  7. CTRL-d - Delete (i.e. kill) currently-opened tmux session (alternatively `tmux kill-session`).
  8. CTRL-b, [ - Enter copy mode, and enable scrolling in currently-opened tmux session. Press `q` to exit.
  9. CTRL-b, " - Split window horizontally (i.e. split and add a pane below).
  10. CTRL-b, % - Split window vertically (i.e. split and add a pane to the right).
  11. CTRL-b, UpArrow - Move to the pane above (I prefer to rebind this to `CTRL-b, k` to be similar to Vim).
  12. CTRL-b, DownArrow - Move to the pane below (I prefer to rebind this to `CTRL-b, j` to be similar to Vim).
  13. CTRL-b, LeftArrow - Move to the pane to the left (I prefer to rebind this to `CTRL-b, h` to be similar to Vim).
  14. CTRL-b, RightArrow - Move to the pane to the right (I prefer to rebind this to `CTRL-b, l` to be similar to Vim).
  15. CTRL-b, z - Toggle maximization of current pane.
  16. CTRL-b, ESC, 2 - Resize all panes to equal size.
  17. CTRL-b, q - Show pane numbers.
  18. CTRL-b, x - Kill current pane.
  19. CTRL-b, c - Create a new window.
  20. CTRL-b, n - Switch to next window.
  21. CTRL-b, p - Switch to previous window.
  22. CTRL-b, 3 - Switch to window 3.
  23. CTRL-b, CTRL-b, COMMAND - Send the command to a nested tmux session (repeat prefix for each level of nesting).

Configuration file additions

Alt arrow keys

Use Alt-arrow keys without prefix key to switch panes[2]

   bind -n M-Left select-pane -L
   bind -n M-Right select-pane -R
   bind -n M-Up select-pane -U
   bind -n M-Down select-pane -D

Shift + arrow keys to switch windows[2]

   bind -n S-Left  previous-window
   bind -n S-Right next-window

Zoom window

Zoom Window - Useful for when you need to copy text out of a pane to your local clipboard without selecting text in other panes.[2]

   unbind !
   bind ! \
     new-window -d -n tmux-zoom 'clear && echo TMUX ZOOM && read' \;\
     swap-pane -s tmux-zoom.0 \;\
     select-window -t tmux-zoom
   
   unbind `
   bind ` \
     last-window \;\
     swap-pane -s tmux-zoom.0 \;\
     kill-window -t tmux-zoom

Sync panes

Sync panes (Send input to all panes in the window). When enabled, pane borders become red as an indication.[2]

   bind-key y set-window-option synchronize-panes on \;\
     set-window-option pane-active-border-style fg=red \;\
     set-window-option pane-border-style fg=yellow \;\
     display-message "Sync panes ON"
   bind-key Y set-window-option synchronize-panes off \;\
     set-window-option pane-active-border-style fg=green \;\
     set-window-option pane-border-style default \;\
     display-message "Sync panes OFF"

Bash Multi Command (works with panes)

Source: [2]

Works with tmux sync panes and spawns a new per argument and then sync them.

Useful for running ssh to multiple machines at same time, or issuing multiple commands at once.

Try it with:

   multi ping 127.0.0.{1..10}

or

   multi ssh server{1..5}

Configuration snippet:

   function multi {
     cmd=$1
     shift
     while $cmd = "ssh" ; do
       pre_check="$(echo $@ | tr ' ' '\n' | sed -e 's/^.*@//g' | \
                    xargs nmap -p 22 -PN -oG - | grep Port | grep -v open)"
       test "${pre_check}x" != "x" && (clear; echo "$pre_check") || break
     done
     tmux send-keys -t 0 "$cmd ${@[1]}"
     for ((pane = 1; pane < ${#@[@]}; pane++)); do
       tmux splitw -h
       tmux send-keys -t $pane "$cmd ${@[pane+1]}"
       tmux select-layout tiled > /dev/null
     done
     tmux set-window-option synchronize-panes on > /dev/null
     tmux set-window-option pane-active-border-style fg=red > /dev/null
     tmux set-window-option pane-border-style fg=yellow > /dev/null
     tmux send-keys Enter
   }

References