tmux로 터미널 멀티태스킹 마스터하기: 개발자를 위한 완전 가이드
🚀 tmux로 터미널 멀티태스킹 마스터하기
개발자라면 하루 종일 터미널과 함께 살아가죠. 코드를 작성하고, 서버를 띄우고, 로그를 확인하고, 테스트를 실행하는 모든 작업이 터미널에서 이루어집니다. 하지만 여러 작업을 동시에 해야 할 때 터미널 창을 여러 개 열어두고 왔다 갔다 하는 것은 비효율적이죠.
바로 이때 tmux가 진정한 게임 체인저가 됩니다! tmux(Terminal Multiplexer)는 하나의 터미널에서 여러 세션을 관리할 수 있게 해주는 강력한 도구입니다.
🔍 tmux란 무엇인가?
tmux는 터미널 멀티플렉서로, 다음과 같은 핵심 기능을 제공합니다:
- 세션 관리: 여러 작업을 독립적인 세션으로 분리
- 창 분할: 하나의 화면을 여러 패널로 나누어 동시 작업
- 세션 지속성: SSH 연결이 끊어져도 작업이 계속 실행
- 원격 협업: 여러 사용자가 같은 세션 공유
📦 tmux 설치하기
각 운영체제별 설치 방법:
Ubuntu/Debian
sudo apt update
sudo apt install tmux
CentOS/RHEL
sudo yum install tmux
# 또는 최신 버전
sudo dnf install tmux
macOS
# Homebrew 사용
brew install tmux
# MacPorts 사용
sudo port install tmux
설치 확인
tmux -V
# 출력 예시: tmux 3.2a
🏗️ tmux의 핵심 구조 이해하기
tmux는 계층적 구조로 되어 있습니다:
Server (tmux 데몬)
├── Session 1
│ ├── Window 1
│ │ ├── Pane 1
│ │ └── Pane 2
│ └── Window 2
│ └── Pane 1
└── Session 2
└── Window 1
└── Pane 1
- Server: tmux 데몬, 모든 세션을 관리
- Session: 독립적인 작업 환경
- Window: 세션 내의 탭과 같은 개념
- Pane: 창을 분할한 각 영역
🎯 기본 사용법 마스터하기
세션 관리
# 새 세션 시작
tmux
# 이름을 지정한 세션 생성
tmux new-session -s myproject
# 기존 세션에 연결
tmux attach-session -t myproject
# 세션 목록 확인
tmux list-sessions
# 세션에서 분리 (detach)
# Ctrl+b, d
# 세션 종료
exit
# 또는 Ctrl+d
실전 예시: 개발 환경 구성
# 프로젝트별 세션 생성
tmux new-session -s frontend -d 'cd ~/projects/frontend && npm start'
tmux new-session -s backend -d 'cd ~/projects/backend && npm run dev'
tmux new-session -s database -d 'cd ~/projects && docker-compose up'
# 각 세션에 순차적으로 연결
tmux attach -t frontend
⌨️ 필수 키바인딩 정리
tmux의 모든 명령은 Prefix Key (기본값: Ctrl+b) 후에 실행됩니다.
🔑 세션 관리
Ctrl+b, d
: 세션에서 분리Ctrl+b, s
: 세션 목록 보기Ctrl+b, $
: 세션 이름 변경
🪟 창(Window) 관리
Ctrl+b, c
: 새 창 생성Ctrl+b, n
: 다음 창으로 이동Ctrl+b, p
: 이전 창으로 이동Ctrl+b, 0-9
: 특정 창으로 이동Ctrl+b, &
: 창 종료Ctrl+b, ,
: 창 이름 변경
📱 패널(Pane) 관리
Ctrl+b, %
: 세로로 분할Ctrl+b, "
: 가로로 분할Ctrl+b, 방향키
: 패널 간 이동Ctrl+b, x
: 패널 종료Ctrl+b, z
: 패널 확대/축소
⚙️ .tmux.conf 설정 파일 최적화
tmux를 진정으로 마스터하려면 설정 파일을 커스터마이징해야 합니다.
기본 설정 파일 생성
# 홈 디렉터리에 설정 파일 생성
vim ~/.tmux.conf
추천 설정
# 기본 설정
set -g default-terminal "screen-256color"
set -g history-limit 10000
# Prefix 키 변경 (Ctrl+a로 변경)
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# 마우스 지원 활성화
set -g mouse on
# 패널 분할 단축키 개선
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %
# 패널 이동 단축키 (Alt + 방향키)
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
# 창 이동 단축키
bind -n M-p previous-window
bind -n M-n next-window
# 설정 파일 즉시 적용
bind r source-file ~/.tmux.conf \; display "Config reloaded!"
# 상태바 커스터마이징
set -g status-bg colour235
set -g status-fg colour136
set -g status-interval 60
set -g status-left-length 30
set -g status-left '#[fg=green](#S) #(whoami)@#H#[default]'
set -g status-right '#[fg=yellow]#(cut -d " " -f 1-3 /proc/loadavg)#[default] #[fg=blue]%H:%M#[default]'
설정 적용
# 설정 파일 리로드
tmux source-file ~/.tmux.conf
# 또는 tmux 내에서
# Ctrl+b, r (위 설정에서 바인딩한 경우)
🚀 고급 활용 패턴
1. 개발 워크플로우 자동화
#!/bin/bash
# dev-setup.sh
SESSION="development"
# 세션이 이미 존재하는지 확인
tmux has-session -t $SESSION 2>/dev/null
if [ $? != 0 ]; then
# 새 세션 생성
tmux new-session -d -s $SESSION
# 창 1: 에디터
tmux rename-window -t $SESSION:0 'editor'
tmux send-keys -t $SESSION:0 'cd ~/project && vim' C-m
# 창 2: 서버
tmux new-window -t $SESSION:1 -n 'server'
tmux send-keys -t $SESSION:1 'cd ~/project && npm start' C-m
# 창 3: 테스트
tmux new-window -t $SESSION:2 -n 'test'
tmux send-keys -t $SESSION:2 'cd ~/project && npm test -- --watch' C-m
# 창 4: Git
tmux new-window -t $SESSION:3 -n 'git'
tmux send-keys -t $SESSION:3 'cd ~/project && git status' C-m
fi
# 세션에 연결
tmux attach-session -t $SESSION
2. 로그 모니터링 대시보드
# 로그 모니터링 세션 생성
tmux new-session -d -s monitoring
# 패널 분할하여 여러 로그 동시 모니터링
tmux split-window -h
tmux split-window -v
tmux select-pane -t 0
tmux split-window -v
# 각 패널에 다른 로그 명령 실행
tmux send-keys -t monitoring:0.0 'tail -f /var/log/nginx/access.log' C-m
tmux send-keys -t monitoring:0.1 'tail -f /var/log/nginx/error.log' C-m
tmux send-keys -t monitoring:0.2 'tail -f /var/log/mysql/error.log' C-m
tmux send-keys -t monitoring:0.3 'htop' C-m
3. 원격 페어 프로그래밍
# 공유 세션 생성 (소켓 파일 권한 설정)
tmux -S /tmp/shared-session new-session -d -s pairing
chmod 777 /tmp/shared-session
# 다른 사용자가 접근
tmux -S /tmp/shared-session attach-session -t pairing
🔧 플러그인으로 tmux 확장하기
TPM (Tmux Plugin Manager) 설치
# TPM 다운로드
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
.tmux.conf에 플러그인 설정 추가
# 플러그인 목록
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 'christoomey/vim-tmux-navigator'
# tmux-resurrect 설정
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-strategy-vim 'session'
# tmux-continuum 설정 (자동 저장)
set -g @continuum-restore 'on'
# TPM 초기화 (파일 맨 끝에 위치)
run '~/.tmux/plugins/tpm/tpm'
유용한 플러그인들
- tmux-resurrect: 세션 저장/복원
- tmux-continuum: 자동 세션 저장
- vim-tmux-navigator: Vim과 tmux 패널 간 매끄러운 이동
- tmux-yank: 시스템 클립보드 통합
💡 실전 사용 팁
효율적인 세션 관리
# 프로젝트별 세션 스크립트
alias work="tmux attach -t work || tmux new -s work"
alias side="tmux attach -t sideproject || tmux new -s sideproject"
# 세션 정리 스크립트
tmux-cleanup() {
tmux list-sessions | grep -v attached | cut -d: -f1 | xargs -I {} tmux kill-session -t {}
}
키바인딩 치트시트 만들기
# ~/.tmux.conf에 추가
bind-key h display-popup -E "less ~/.tmux-cheatsheet.txt"
상태바에 유용한 정보 표시
# CPU 사용률, 메모리, 배터리 정보 표시
set -g status-right '#[fg=yellow]#(uptime | cut -d "," -f 3-) #[fg=green]#(free -h | awk "/^Mem:/ {print \$3}")#[default] #[fg=blue]%Y-%m-%d %H:%M#[default]'
🚨 주의사항과 문제 해결
일반적인 문제들
- 256 컬러 지원 안됨:
TERM
환경변수 확인 - 마우스 스크롤 안됨:
set -g mouse on
설정 확인 - 클립보드 공유 안됨: tmux-yank 플러그인 사용
성능 최적화
# 히스토리 제한 설정
set -g history-limit 5000
# 상태바 업데이트 간격 조정
set -g status-interval 30
# 불필요한 모니터링 비활성화
set -g monitor-activity off
Featured image by Anete Lūsiņa on Unsplash