Post

tmux (Terminal Multiplexer) 사용법

터미널을 여러개의 세션으로 나누어서 사용할 수 있게 해주는 tmux 사용법에 대한 내용

tmux (Terminal Multiplexer) 사용법
  • tmux는 터미널을 여러개의 세션으로 나누어서 사용할 수 있게 해주는 터미널 멀티플렉서이다. (vs screen)
  • tmux는 세션을 생성하여 여러개의 터미널을 관리할 수 있게 해주며 하나의 세션은 다른 세션과 독립적으로 동작한다.
  • 세션은 여러개의 윈도우로 구성되어 있고, 윈도우는 여러개의 패널로 구성되어 있다. (세션 > 윈도우 > 패널)

설치

1
2
3
brew install tmux # macOS
sudo apt-get install tmux # Ubuntu
sudo yum install tmux # CentOS

설정

  • tmux 설정은 ~/.tmux.conf 파일에 설정을 추가하여 사용할 수 있다.
  • tmux 설정 파일은 tmux가 실행될 때 자동으로 로드된다.

cheat sheet

  • tmux는 다양한 단축키를 제공하여 터미널을 효율적으로 사용할 수 있게 해준다.
  • tmux의 단축키는 <ctrl+b>를 누른 후 다른 키를 누르면 된다.
  • 아래는 tmux의 주요 단축키이다.

cheatsheet

tmux 세션

  • tmux는 세션을 생성하여 여러개의 터미널을 관리할 수 있게 해준다. 하나의 세션은 다른 세션과 독립적으로 동작한다.
  • 세션은 여러개의 윈도우로 구성되어 있고, 윈도우는 여러개의 패널로 구성되어 있다. (세션 > 윈도우 > 패널)
  1. 세션 생성
    1
    2
    3
    4
    
    tmux
    tmux new
    tmux new-session
    tmux new -s sessionname
    
  2. 세션 접속
    1
    2
    3
    4
    5
    
    tmux a
    tmux att
    tmux attach
    tmux attach-session
    tmux a -t sessionname
    
  3. 세션 종료
    1
    2
    
    tmux kill-session
    tmux kill-session -t sessionname
    

단축키

  • <ctrl+b> + $ : rename session
  • <ctrl+b> + d : detach session
  • <ctrl+b> + ) : next session
  • <ctrl+b> + ( : previous session
  • <ctrl+b> + w : list session

tmux windows

  • 브라우저의 탭과 유사한 개념으로서 하나의 세션에 여러개의 윈도우를 생성할 수 있다.

단축키

  • <ctrl+b> + c : create window
  • <ctrl+b> + n : move to next window
  • <ctrl+b> + p : move to previous window
  • <ctrl+b> + l : move to window last used
  • <ctrl+b> + 0..9 : select window by nubmer
  • <ctrl+b> + ‘ : select window by name
  • <ctrl+b> + . : change window number
  • <ctrl+b> + , : rename window
  • <ctrl+b> + f : search windows
  • <ctrl+b> + & : kill window
  • <ctrl+b> + w : list windows
  • <ctrl+b> + z : toggle pane zoom

윈도우 합치기 or 분리하기

1
2
3
4
# ctrl + b + : 로 tmux 명령 입력창 활성화
# :tmux join-pane [-bdhv] [-l size | -p percentage] [-s src-pane] [-t dst-pane]
join-pane -s 2 -t 1  # 2번 윈도우를 1번 윈도우로 합치기
break-pane -s 1 -t 2  # 1번 윈도우를 2번 윈도우로 분리하기

tmux panes

윈도우를 여러개의 패널로 나눠서 사용할 수 있다.

단축키

  • <ctrl+b> + % : vertical split
  • <ctrl+b> + “ : horizontal split
  • <ctrl+b> + : move to pane to the right
  • <ctrl+b> + : move to pane to the left
  • <ctrl+b> + : move up to pane
  • <ctrl+b> + : move down to pane
  • <ctrl+b> + o : go to next pane
  • <ctrl+b> + ; : go to last active pane
  • <ctrl+b> + } : move pane right
  • <ctrl+b> + { : move pane left
  • <ctrl+b> + ! : convert pane to window
  • <ctrl+b> + x : kill pane
  • <ctrl+b> + <ctrl+o> : swap pane

tmux pane resize

패널의 크기를 조절할 수 있다.

1
2
3
4
5
6
7
8
9
// <ctrl+b> + : 로 tmux 명령 입력창 활성화 후 아래 명령어 입력
:resize-pane -D (Resizes the current pane down) 
:resize-pane -U (Resizes the current pane upward) 
:resize-pane -L (Resizes the current pane left) 
:resize-pane -R (Resizes the current pane right) 
:resize-pane -D 10 (Resizes the current pane down by 10 cells) 
:resize-pane -U 10 (Resizes the current pane upward by 10 cells) 
:resize-pane -L 10 (Resizes the current pane left by 10 cells) 
:resize-pane -R 10 (Resizes the current pane right by 10 cells) 

tmux 복사 & 붙여넣기

  • 복사모드로 들어가서 복사하고 붙여넣기를 할 수 있다.
  • <ctrl+b> + [ : enter copy mode
  • <ctrl+b> + ] : paster from buffer

복사모드 단축키

  • <space> : start selection
  • <enter> : copy selection
  • <esc> : clear selection
  • g : go to top
  • G : go to bottom
  • h : move cursor left
  • j : move cursor down
  • k : move cursor up
  • l : move cursor right
  • / : search
  • # : list paste buffers
  • q : quit

tmux 스크롤 하기

  • tmux에서 스크롤을 하기 위해서는 복사모드로 들어가야 한다.
1
<ctrl+b> + [

마우스로 스크롤하기

  • 위의 방법은 키보드로 스크롤하는 방법이다. 마우스로 스크롤하기 위해서는 마우스모드를 활성화 해야 한다.
  • .tmux.conf 파일에 아래 내용을 추가한다.
1
set -g mouse on

Tmux Plugin Manager

  • Tmux Plugin Manager는 tmux의 플러그인을 관리해주는 플러그인으로 tmux의 플러그인을 쉽게 설치하고 관리할 수 있게 해준다.
  1. 설치
    1
    
    git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
    
  2. 설정 .tmux.conf 파일에 아래 내용을 추가
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
     # tpm plugin
    set -g @plugin 'tmux-plugins/tpm'
    # ---
    # list of tmux plugins
    set -g @plugin 'tmux-plugins/tmux-sensible' # for sane tmux defaults
    set -g @plugin 'christoomey/vim-tmux-navigator' # for navigating panes and vim/nvim with Ctrl-hjkl
    set -g @plugin 'jimeh/tmux-themepack' # to configure tmux theme
    set -g @plugin 'tmux-plugins/tmux-resurrect' # persist tmux sessions after computer restart
    set -g @plugin 'tmux-plugins/tmux-continuum' # automatically saves sessions for you every 15 minutes
    set -g @themepack 'powerline/default/cyan' # use this theme for tmux
    set -g @resurrect-capture-pane-contents 'on' # allow tmux-ressurect to capture pane contents
    set -g @continuum-restore 'on' # enable tmux-continuum functionality
    # ---
    # tmux settings
    set -g default-terminal "screen-256color"
    set -g mouse on # enable mouse support
    # ---
    run '~/.tmux/plugins/tpm/tpm' # Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
    
  3. 플러그인 설치 tmux를 실행한 후 <ctrl+b> + :를 눌러서 tmux 명령 입력창을 활성화.
    1
    2
    3
    
    `<crtl+b>` + : # tmux 명령 입력창을 활성화.
    :source-file ~/.tmux.conf  # tmux 설정 파일을 다시 로드.
    `<ctrl+b>` + I # 플러그인을 설치
    
This post is licensed under CC BY 4.0 by the author.