tmux คือ terminal multiplexer คำอธิบายเต็ม ๆ ก็ตามนี้

tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached.

เวลาเราใช้งานก็ค่อยมาสร้าง Window หรือ Split Horizontal/Vertical ตามใช้งาน สนใจเพิ่มเติมก็อ่านต่อได้ที่นี่ ส่วนต่าง ๆ ของ tmux ตามนี้

แต่ถ้าเรามีแพทเทิร์นใช้งานประจำอยู่แล้วก็สามารถสร้างเป็น Shell script สั้น ๆ ได้ ตัวอย่างเช่น

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/bash
currentsession=$(tmux display-message -p '#S')
session="$1"
function has-session {
tmux has-session -t $session 2>/dev/null
}
if has-session ; then
tmux list-sessions
read -p "Session already exists. [A]ttach/[K]ill/Kill [S]erver?" -i Y input
case $input in
a|A)
# Attach to created session
tmux attach-session -t $session
;;
k|K)
tmux kill-session -t $session
;;
s|S)
tmux kill-server
;;
*) echo "What?" ;;
esac
else
tmux new-session -d -s $session -n 'home' 'cd ~; bash -i'
tmux new-window -a -t $session -n 'server' 'bash -i'
tmux new-window -a -t $session -n 'server-log' 'bash -i'
tmux select-window -t $session:1 \; split-window -h \; select-pane -t 1 \; split-window -v \; select-pane -t 3 \; split-window -v \; select-pane -t 1 \;
tmux select-window -t $session:2 \; split-window -h \; split-window -v \; select-pane -t 1 \;
tmux select-window -t $session:3 \; split-window -h \; split-window -v \; split-window -v \; select-pane -t 2 \; split-window -v \; select-pane -t 1 \;
# Attach to created session
tmux attach-session -t $session
fi
#!/bin/bash currentsession=$(tmux display-message -p '#S') session="$1" function has-session { tmux has-session -t $session 2>/dev/null } if has-session ; then tmux list-sessions read -p "Session already exists. [A]ttach/[K]ill/Kill [S]erver?" -i Y input case $input in a|A) # Attach to created session tmux attach-session -t $session ;; k|K) tmux kill-session -t $session ;; s|S) tmux kill-server ;; *) echo "What?" ;; esac else tmux new-session -d -s $session -n 'home' 'cd ~; bash -i' tmux new-window -a -t $session -n 'server' 'bash -i' tmux new-window -a -t $session -n 'server-log' 'bash -i' tmux select-window -t $session:1 \; split-window -h \; select-pane -t 1 \; split-window -v \; select-pane -t 3 \; split-window -v \; select-pane -t 1 \; tmux select-window -t $session:2 \; split-window -h \; split-window -v \; select-pane -t 1 \; tmux select-window -t $session:3 \; split-window -h \; split-window -v \; split-window -v \; select-pane -t 2 \; split-window -v \; select-pane -t 1 \; # Attach to created session tmux attach-session -t $session fi
#!/bin/bash

currentsession=$(tmux display-message -p '#S')
session="$1"

function has-session {
  tmux has-session -t $session 2>/dev/null
}

if has-session ; then
  tmux list-sessions
  read -p "Session already exists. [A]ttach/[K]ill/Kill [S]erver?" -i Y input 
  case $input in  
    a|A)
      # Attach to created session
      tmux attach-session -t $session
      ;;
    k|K)
      tmux kill-session -t $session
      ;; 
    s|S)
      tmux kill-server
      ;; 
    *) echo "What?" ;;
  esac
else
  tmux new-session -d -s $session -n 'home' 'cd ~; bash -i'
  tmux new-window -a -t $session -n 'server' 'bash -i'
  tmux new-window -a -t $session -n 'server-log'  'bash -i'

  tmux select-window -t $session:1 \; split-window -h \; select-pane -t 1 \; split-window -v \; select-pane -t 3 \; split-window -v \; select-pane -t 1 \;
  tmux select-window -t $session:2 \; split-window -h \; split-window -v \; select-pane -t 1 \;
  tmux select-window -t $session:3 \; split-window -h \; split-window -v \; split-window -v \; select-pane -t 2 \; split-window -v \; select-pane -t 1 \;
  
  # Attach to created session
  tmux attach-session -t $session
fi

จะเรียก Shell script เองก็ได้หรือจะสร้างเป็น alias ใน

.bashrc/.zshrc
.bashrc/.zshrcตามสะดวก ผลลัพธ์ก็ตามนี้

จบปิ๊ง !!

Published by Man Friday

Application Developer, Photographer and WordPress aficionado. Particularly interested in relational database design, In usability, UX and accessibility on software development. I just wear glasses, Lives in Ubonratchathani, Thailand.