
Tmux: Terminal multiplexer bất kỳ developer nào cũng nên biết
Tmux cho phép chia cửa sổ terminal thành nhiều pane, tạo nhiều session, detach/attach bất cứ lúc nào. Khi kết hợp với sessionizer (script fuzzy finder cho session), bạn có thể chuyển đổi giữa các project chỉ trong vài giây mà không cần nhớ đường dẫn hay tên session.
Cài đặt và cấu hình cơ bản Tmux
Trên Ubuntu/Debian: sudo apt install tmux. Trên macOS: brew install tmux. File cấu hình chính ở ~/.tmux.conf. Các setting quan trọng:
# Prefix key thay C-b bằng C-Space (dễ nhấn hơn)
unbind C-b
set -g prefix C-Space
bind C-Space send-prefix
# Cho phép mouse click chọn pane, resize
set -g mouse on
# Index từ 1 thay vì 0
set -g base-index 1
setw -g pane-base-index 1
# History limit lớn
set -g history-limit 10000
# Vi mode cho copy mode
setw -g mode-keys vi
Sessionizer: Fuzzy finder cho Tmux session
Sessionizer là một script shell (thường dùng fzf) cho phép tìm và switch session nhanh. Tạo file ~/scripts/tmux-sessionizer:
#!/bin/bash
# Tmux sessionizer - tìm và switch session bằng fzf
selected=$(find ~/projects ~/work ~/personal -mindepth 1 -maxdepth 1 -type d | fzf)
if [[ -z $selected ]]; then exit 0; fi
selected_name=$(basename "$selected" | tr . _)
tmux_running=$(pgrep tmux)
if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then
tmux new-session -s "$selected_name" -c "$selected"
exit 0
fi
if ! tmux has-session -t="$selected_name" 2>/dev/null; then
tmux new-session -ds "$selected_name" -c "$selected"
fi
tmux switch-client -t "$selected_name"
Cấp quyền thực thi: chmod +x ~/scripts/tmux-sessionizer. Bind vào tmux prefix trong ~/.tmux.conf:
bind-key -r f run-shell "tmux neww ~/scripts/tmux-sessionizer"
Giờ bấm Prefix + f (C-Space f) sẽ mở fuzzy finder chọn project, tự tạo session mới nếu chưa có.
Workflow thực tế hàng ngày
- Khởi động ngày làm việc: Mở terminal, Tmux auto-start (qua systemd user service hoặc shell rc), bấm
Prefix + fchọn project đầu tiên - Chuyển project:
Prefix + f→ gõ tên project → Enter. Session cũ vẫn chạy nền với mọi process - Chia màn hình trong project:
Prefix + |(vertical split),Prefix + -(horizontal split). Navigate pane:Prefix + h/j/k/l(vim-style) - Detach an toàn:
Prefix + d→ đóng terminal/laptop, mọi process vẫn chạy - Attach lại:
tmux attachhoặctmux attach -t session_name
Mẹo nâng cao: Tmuxinator cho project phức tạp
Đối với project có nhiều service (backend, frontend, database, redis), dùng Tmuxinator để định nghĩa layout trong file YAML:
# ~/.config/tmuxinator/myproject.yml
name: myproject
root: ~/projects/myproject
windows:
- editor: nvim .
- backend: go run main.go
- frontend: npm run dev
- db: docker compose up db
- logs: docker compose logs -f
Chạy tmuxinator myproject sẽ tự tạo session với 5 window, mỗi window chạy command tương ứng. Kết hợp với sessionizer: bấm Prefix + f, gõ myproject, nếu session chưa có thì tmuxinator sẽ tạo mới.


Kết luận
Tmux + sessionizer biến terminal thành workspace linh hoạt, bền vững. Bạn không bao giờ mất context khi chuyển project, không lo process bị kill khi đóng terminal. Thời gian setup một lần, hưởng lợi cả đời dev career.
Tài liệu tham khảo: Tmux trên GitHub, fzf trên GitHub, Tmuxinator documentation.
