콘텐츠로 이동

tmux 치트시트

개요

tmux (터미널 멀티플렉서)는 단일 터미널 창 내에서 여러 터미널 세션을 실행할 수 있게 해주는 도구입니다. 세션을 분리하고 다시 연결할 수 있어 원격 작업과 장시간 실행되는 프로세스에 완벽합니다.

설치

패키지 관리자

# Ubuntu/Debian
sudo apt install tmux

# macOS
brew install tmux

# CentOS/RHEL
sudo yum install tmux

# Arch Linux
sudo pacman -S tmux

# From source
git clone https://github.com/tmux/tmux.git
cd tmux && ./configure && make && sudo make install
```[내용 필요]

## 기본 개념

### 용어

Session # A collection of windows Window # A single terminal screen Pane # A subdivision of a window Prefix # Default Ctrl+b (customizable)


### 세션 계층 구조

Session ├── Window 1 │ ├── Pane 1 │ └── Pane 2 ├── Window 2 │ └── Pane 1 └── Window 3 ├── Pane 1 ├── Pane 2 └── Pane 3


## 기본 사용법

### tmux 시작하기
```bash
# Start new session
tmux

# Start named session
tmux new-session -s mysession
tmux new -s mysession

# Start session with specific window name
tmux new -s mysession -n mywindow

# Start detached session
tmux new -d -s mysession
```[내용 필요]

### 세션 관리
```bash
# List sessions
tmux list-sessions
tmux ls

# Attach to session
tmux attach-session -t mysession
tmux attach -t mysession
tmux a -t mysession

# Detach from session
# Inside tmux: Ctrl+b d

# Kill session
tmux kill-session -t mysession

# Kill all sessions
tmux kill-server
```[내용 필요]

## 키 바인딩 (기본 접두사: Ctrl+b)

### 세션 명령어

Prefix + d # Detach from session Prefix + s # List sessions Prefix + $ # Rename session Prefix + ( # Previous session Prefix + ) # Next session


### 창 명령어

Prefix + c # Create new window Prefix + n # Next window Prefix + p # Previous window Prefix + l # Last window Prefix + w # List windows Prefix + & # Kill window Prefix + , # Rename window Prefix + 0-9 # Switch to window by number Prefix + f # Find window


### 창 명령어

Prefix + % # Split horizontally Prefix + ” # Split vertically Prefix + o # Switch to next pane Prefix + ; # Switch to last pane Prefix + x # Kill pane Prefix + ! # Break pane into window Prefix + z # Toggle pane zoom Prefix + \\{ # Move pane left Prefix + \\} # Move pane right Prefix + q # Show pane numbers Prefix + q 0-9 # Switch to pane by number


### 창 탐색

Prefix + ↑ # Move to pane above Prefix + ↓ # Move to pane below Prefix + ← # Move to pane left Prefix + → # Move to pane right


### 창 크기 조정

Prefix + Ctrl+↑ # Resize pane up Prefix + Ctrl+↓ # Resize pane down Prefix + Ctrl+← # Resize pane left Prefix + Ctrl+→ # Resize pane right

Alternative (hold Prefix)

Prefix + Alt+↑ # Resize pane up (5 lines) Prefix + Alt+↓ # Resize pane down (5 lines) Prefix + Alt+← # Resize pane left (5 columns) Prefix + Alt+→ # Resize pane right (5 columns)


## 복사 모드

### 복사 모드 들어가기

Prefix + [ # Enter copy mode


### 복사 모드 탐색

h, j, k, l # Move cursor (vim-style) Arrow keys # Move cursor w # Next word b # Previous word 0 # Beginning of line $ # End of line g # Go to top G # Go to bottom Ctrl+f # Page down Ctrl+b # Page up / # Search forward ? # Search backward n # Next search result N # Previous search result


### 복사 모드 선택

Space # Start selection Enter # Copy selection and exit Escape # Exit copy mode v # Start selection (vi mode) y # Copy selection (vi mode)


### 붙여넣기

Prefix + ] # Paste buffer


## 구성

### 구성 파일 (~/.tmux.conf)
```bash
# Basic settings
set -g default-terminal "screen-256color"
set -g history-limit 10000
set -g base-index 1
setw -g pane-base-index 1

# Change prefix key
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# Reload config
bind r source-file ~/.tmux.conf \; display "Config reloaded!"

# Mouse support
set -g mouse on

# Vi mode
setw -g mode-keys vi

# Pane splitting
bind|split-window -h
bind - split-window -v
unbind '"'
unbind %

# Pane navigation (vim-style)
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Window navigation
bind -n M-Left previous-window
bind -n M-Right next-window

# Pane resizing
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

# Status bar
set -g status-bg black
set -g status-fg white
set -g status-left '#[fg=green]#S '
set -g status-right '#[fg=yellow]%Y-%m-%d %H:%M'
set -g status-interval 60

# Window status
setw -g window-status-current-style 'fg=black bg=white'
setw -g window-status-style 'fg=white bg=black'

# Pane borders
set -g pane-border-style 'fg=colour238'
set -g pane-active-border-style 'fg=colour51'
```[내용 필요]

### 고급 구성
```bash
# Automatic window renaming
setw -g automatic-rename on
set -g set-titles on

# Activity monitoring
setw -g monitor-activity on
set -g visual-activity on

# Copy mode improvements
bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
bind -T copy-mode-vi r send-keys -X rectangle-toggle

# Synchronize panes
bind S setw synchronize-panes

# Quick pane cycling
bind -n M-o select-pane -t :.+

# Session management
bind C-s choose-session
bind C-n new-session
```[내용 필요]

## 고급 기능

### 세션 스크립팅
```bash
# Create session with multiple windows
tmux new-session -d -s development
tmux new-window -t development -n editor 'vim'
tmux new-window -t development -n server 'cd ~/project && npm start'
tmux split-window -t development:server -h 'cd ~/project && npm run watch'
tmux attach-session -t development
```[내용 필요]

### Tmux 스크립트
```bash
#!/bin/bash
# dev-session.sh

SESSION="development"

# Check if session exists
tmux has-session -t $SESSION 2>/dev/null

if [ $? != 0 ]; then
    # Create new session
    tmux new-session -d -s $SESSION

    # Setup windows
    tmux rename-window -t $SESSION:1 'editor'
    tmux send-keys -t $SESSION:1 'cd ~/project && vim' C-m

    tmux new-window -t $SESSION -n 'server'
    tmux send-keys -t $SESSION:2 'cd ~/project && npm start' C-m

    tmux new-window -t $SESSION -n 'git'
    tmux send-keys -t $SESSION:3 'cd ~/project && git status' C-m

    # Split server window
    tmux split-window -t $SESSION:2 -h
    tmux send-keys -t $SESSION:2.2 'cd ~/project && npm run watch' C-m
fi

# Attach to session
tmux attach-session -t $SESSION
```[내용 필요]

### Tmuxinator
```bash
# Install tmuxinator
gem install tmuxinator

# Create project
tmuxinator new myproject

# Example tmuxinator config (~/.tmuxinator/myproject.yml)
name: myproject
root: ~/projects/myproject

windows:
  - editor:
      layout: main-vertical
      panes:
        - vim
        - guard
  - server: bundle exec rails s
  - logs: tail -f log/development.log
```[내용 필요]

## 플러그인

### TPM (Tmux 플러그인 관리자)

Would you like me to continue translating the remaining sections or provide more detailed translations for the empty sections?```bash
# Install TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# Add to ~/.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'

# Initialize TPM (add to bottom of .tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

# Install plugins: Prefix + I
# Update plugins: Prefix + U
# Remove plugins: Prefix + alt + u
```### 인기 있는 플러그인
```bash
# tmux-resurrect - save/restore sessions
set -g @plugin 'tmux-plugins/tmux-resurrect'
# Save: Prefix + Ctrl+s
# Restore: Prefix + Ctrl+r

# tmux-continuum - automatic save/restore
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-restore 'on'

# tmux-yank - copy to system clipboard
set -g @plugin 'tmux-plugins/tmux-yank'

# tmux-pain-control - better pane management
set -g @plugin 'tmux-plugins/tmux-pain-control'

# tmux-sidebar - file tree sidebar
set -g @plugin 'tmux-plugins/tmux-sidebar'
# Toggle: Prefix + Tab
```## 명령줄 인터페이스
```bash
# Create session with command
tmux new-session -d -s mysession 'htop'

# Send commands to session
tmux send-keys -t mysession 'ls -la' C-m

# Capture pane content
tmux capture-pane -t mysession -p

# List windows in session
tmux list-windows -t mysession

# List panes in window
tmux list-panes -t mysession:1
```### 세션 관리
```bash
# Create development environment
create_dev_env() \\\\{
    local session="dev-$(date +%s)"
    tmux new-session -d -s "$session"
    tmux send-keys -t "$session" 'cd ~/project' C-m
    tmux split-window -t "$session" -h
    tmux send-keys -t "$session" 'cd ~/project && npm start' C-m
    tmux select-pane -t "$session":0
    tmux attach-session -t "$session"
\\\\}

# Kill all sessions except current
kill_other_sessions() \\\\{
    local current=$(tmux display-message -p '#S')
    tmux list-sessions -F '#\\\\{session_name\\\\}'|\
    grep -v "^$current$"|\
    xargs -I \\\\{\\\\} tmux kill-session -t \\\\{\\\\}
\\\\}
```### 스크립팅 예시
```bash
# Quick session switching
bind -n M-1 switch-client -t session1
bind -n M-2 switch-client -t session2

# Broadcast to all panes
bind a setw synchronize-panes

# Quick layouts
Prefix + Space  # Cycle through layouts
Prefix + Alt+1  # Even horizontal
Prefix + Alt+2  # Even vertical
Prefix + Alt+3  # Main horizontal
Prefix + Alt+4  # Main vertical
Prefix + Alt+5  # Tiled

# Zoom pane
Prefix + z      # Toggle pane zoom

# Break pane to new window
Prefix + !      # Break pane out

# Join pane from another window
bind j command-prompt -p "join pane from:" "join-pane -s '%%'"
```## 팁과 트릭
```bash
# Add to ~/.bashrc or ~/.zshrc
alias ta='tmux attach'
alias tls='tmux list-sessions'
alias tn='tmux new-session'
alias tk='tmux kill-session'

# Function to create or attach to session
tm() \\\\{
    [[ -n "$TMUX" ]] && change="switch-client"||change="attach-session"
    if [ $1 ]; then
        tmux $change -t "$1" 2>/dev/null||(tmux new-session -d -s $1 && tmux $change -t "$1"); return
    fi
    session=$(tmux list-sessions -F "#\\\\{session_name\\\\}" 2>/dev/null|fzf --exit-0) &&  tmux $change -t "$session"||echo "No sessions found."
\\\\}
```### 생산성 팁
```bash
# Fix 256 color support
export TERM=screen-256color

# Fix clipboard issues
# Install xclip (Linux) or reattach-to-user-namespace (macOS)

# Reset tmux completely
tmux kill-server

# Check tmux version
tmux -V

# Validate configuration
tmux source-file ~/.tmux.conf
```### 유용한 별칭
```bash
# Reduce escape time
set -sg escape-time 0

# Increase history limit
set -g history-limit 50000

# Refresh status bar less frequently
set -g status-interval 5
```## 문제 해결
```bash
# vim-tmux-navigator plugin
# Seamless navigation between vim and tmux panes
bind -n C-h run "(tmux display-message -p '#\\\\{pane_current_command\\\\}'|grep -iq vim && tmux send-keys C-h)||tmux select-pane -L"
bind -n C-j run "(tmux display-message -p '#\\\\{pane_current_command\\\\}'|grep -iq vim && tmux send-keys C-j)||tmux select-pane -D"
bind -n C-k run "(tmux display-message -p '#\\\\{pane_current_command\\\\}'|grep -iq vim && tmux send-keys C-k)||tmux select-pane -U"
bind -n C-l run "(tmux display-message -p '#\\\\{pane_current_command\\\\}'|grep -iq vim && tmux send-keys C-l)||tmux select-pane -R"
```### 일반적인 문제
```bash
# Auto-attach to tmux on SSH
# Add to ~/.bashrc on remote server
if [[ -z "$TMUX" ]] && [ "$SSH_CONNECTION" != "" ]; then
    tmux attach-session -t ssh_tmux||tmux new-session -s ssh_tmux
fi
```### 성능
https://github.com/tmux/tmux#

# 통합
`man tmux`### Vim/Neovim과 함께
https://github.com/tmux/tmux/wiki##

# SSH와 함께
https://github.com/rothgar/awesome-tmux#

# 리소스