TMUX 사용방법 (창분할하기, 빌드걸고 퇴근하기)

TMUX가 뭔가요?

TMUX는 terminal multiplexer로 터미널에서 사용하는 프로그램들을 손쉽게 switch하거나 backgroud로 수행할 수 있게 도와주는 툴입니다.

TMUX를 사용하면

  1. 터미널창을 분할해서 사용할 수 있습니다.
  2. Perforce Sync / 모델 빌드를 걸어두고 다음날 출근해서 확인할 수 있습니다. (session유지)

설치방법

  1. TMUX최신 버전을 설치합니다.
    Ubuntu에서는 apt-get install tmux로 설치가능 하지만 굉장히 오래전 버전입니다.
1
2
3
4
5
To get and build the latest from version control:
$ cd tmux
$ sh autogen.sh
$ ./configure && make
  1. Root권한이 있는 사용자라면 sudo make install로 모든 사용자가 해당 버전을 사용하게 해줄 수 있습니다. (서버쪽은 제가 최신버전으로 깔아뒀습니다.)
  2. 제가 설정해둔 설정파일을 사용하시면 좀 더 편할 듯싶습니다.
    아래 있는 파일을 ~/.tmux.conf로 저장하시면 됩니다.( 할필요 그닥 없음...)
  3. VI의 color scheme이 정상적으로 동작하게끔하기 위해서 .bashrc등에 아래 줄을 추가해둡니다.
    alias tmux='TERM=xterm-256color tmux -2'

                           <~/.tmux.conf>

    # use UTF8
    set -g utf8
    set-window-option -g utf8 on
    # make tmux display things in 256 colors
    # set -g default-terminal "screen-256color"
    set -g default-terminal "xterm"
    set-window-option -g xterm-keys on
    # set scrollback history to 10000 (10k)
    set -g history-limit 10000
    # set Ctrl-a as the default prefix key combination
    # and unbind C-b to free it up
    set -g prefix C-j
    unbind C-b
    unbind C-a
    # use send-prefix to pass C-a through to application
    bind C-j send-prefix
    # shorten command delay
    set -sg escape-time 1
    # set window and pane index to 1 (0 by default)
    set-option -g base-index 1
    setw -g pane-base-index 1
    # reload ~/.tmux.conf using PREFIX r
    bind r source-file ~/.tmux.conf \; display "Reloaded!"
    # use PREFIX | to split window horizontally and PREFIX - to split vertically
    bind | split-window -h
    bind \ split-window -h
    bind - split-window -v
    # Make the current window the first window
    bind T swap-window -t 1
    # map Vi movement keys as pane movement keys
    # bind h select-pane -L
    # bind j select-pane -D
    # bind k select-pane -U
    # bind l select-pane -R
    bind w select-pane -l #select last pane
    bind-key -n M-W last-pane
    # and use C-h and C-l to cycle thru panes
    bind -r C-h select-window -t :-
    bind -r C-l select-window -t :+
    # resize panes using PREFIX H, J, K, L
    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
    # explicitly disable mouse control
    set -g mode-mouse on
    set -g mouse-resize-pane on
    set -g mouse-select-pane on
    set -g mouse-select-window on
    set-option -g mouse on
    set-option -g mouse-utf8 on
    # ---------------------
    # Copy & Paste
    # ---------------------
    # provide access to the clipboard for pbpaste, pbcopy
    # set-option -g default-command "reattach-to-user-namespace -l zsh"
    # set-option -g default-command "bash"
    set-option -g default-command "zsh"
    set-window-option -g automatic-rename on
    # use vim keybindings in copy mode
    setw -g mode-keys vi
    # setup 'v' to begin selection as in Vim
    bind-key -t vi-copy v begin-selection
    #bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
    bind-key -t vi-copy y copy-pipe "xclip -selection clipboard -i"
    # update default binding of 'Enter' to also use copy-pipe
    unbind -t vi-copy Enter
    # bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"
    bind-key -t vi-copy Enter copy-pipe "xclip -selection clipboard -i"
    # bind y run 'tmux save-buffer - | reattach-to-user-namespace pbcopy '
    # bind C-y run 'tmux save-buffer - | reattach-to-user-namespace pbcopy '
    bind y run "tmux show-buffer | xclip -i -selection clipboard"
    bind C-y run "tmux show-buffer | xclip -i -selection clipboard"
    # ----------------------
    # set some pretty colors
    # ----------------------
    # set pane colors - hilight the active pane
    # set-option -g pane-border-fg colour235 #base02
    # set-option -g pane-active-border-fg colour240 #base01
    # colorize messages in the command line
    # set-option -g message-bg black #base02
    # set-option -g message-fg brightred #orange
    # ----------------------
    # Status Bar
    # -----------------------
    set-option -g status on # turn the status bar on
    set -g status-utf8 on # set utf-8 for the status bar
    set -g status-interval 5 # set update frequencey (default 15 seconds)
    set -g status-justify centre # center window list for clarity
    set-option -g status-position top # position the status bar at top of screen
    # visual notification of activity in other windows
    setw -g monitor-activity on
    set -g visual-activity on
    # set color for status bar
    #set-option -g status-bg colour235 #base02
    # set-option -g status-bg default
    # set-option -g status-fg yellow #yellow
    # set-option -g status-fg white
    # set-option -g status-attr dim
    # set window list colors - red for active and cyan for inactive
    #set-window-option -g window-status-fg white
    #set-window-option -g window-status-bg colour236
    #set-window-option -g window-status-attr bright
    #set-window-option -g window-status-current-fg black
    #set-window-option -g window-status-current-bg yellow
    #set-window-option -g window-status-current-attr bright
    # show host name and IP address on right side of status bar
    set -g status-right-length 70
    set -g status-right "#[fg=green]#h@#[fg=yellow]#(ifconfig eth0 | grep 'inet ' | awk '{print $2}')"
    # show session name, window & pane number, date and time on right side of
    # status bar
    set -g status-left-length 60
    set -g status-left "#[fg=green]::%l:%M %p#[fg=yellow]::%Y-%m-%d::#[fg=blue]#S #[fg=colour255]Tab#[fg=yellow]#I#[fg=colour255]:Pane#[fg=yellow]#P"
    #### COLOUR (Solarized 256)
    # default statusbar colors
    set-option -g status-bg colour235 #base02
    set-option -g status-fg colour136 #yellow
    set-option -g status-attr bright
    # default window title colors
    set-window-option -g window-status-fg colour244 #base0
    set-window-option -g window-status-bg default
    set-window-option -g window-status-attr dim
    # active window title colors
    set-window-option -g window-status-current-fg yellow
    set-window-option -g window-status-current-bg default
    set-window-option -g window-status-current-attr bright
    # set-window-option -g window-status-current-format "#[fg=colour235, bg=colour166]#[fg=colour255, bg=colour166]#I:#W #[fg=colour166, bg=colour235]"
    # set-window-option -g window-status-format "#[fg=colour235, bg=colour244]#[fg=colour255, bg=colour244]#I:#W#F#[fg=colour244, bg=colour235]"
    # pane border
    set-option -g pane-border-fg colour235 #base02
    set-option -g pane-active-border-fg colour240 #base01
    # message text
    set-option -g message-bg colour235 #base02
    set-option -g message-fg colour166 #orange
    # pane number display
    set-option -g display-panes-active-colour colour33 #blue
    set-option -g display-panes-colour colour166 #orange
    # clock
    set-window-option -g clock-mode-colour colour64 #green
    # Smart pane switching with awareness of vim splits
    # bind -n C-h run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim(diff)?$' && tmux send-keys C-h) || tmux select-pane -L"
    # bind -n C-j run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim(diff)?$' && tmux send-keys C-j) || tmux select-pane -D"
    # bind -n C-k run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim(diff)?$' && tmux send-keys C-k) || tmux select-pane -U"
    # bind -n C-l run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim(diff)?$' && tmux send-keys C-l) || tmux select-pane -R"
    # bind -n C-\ run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim(diff)?$' && tmux send-keys 'C-\\') || tmux select-pane -l"
    # key binding for ALT+num
    # switch windows alt+number
    bind-key -n M-1 select-window -t 1
    bind-key -n M-2 select-window -t 2
    bind-key -n M-3 select-window -t 3
    bind-key -n M-4 select-window -t 4
    bind-key -n M-5 select-window -t 5
    bind-key -n M-6 select-window -t 6
    bind-key -n M-7 select-window -t 7
    bind-key -n M-8 select-window -t 8
    bind-key -n M-9 select-window -t 9
    bind-key -n M-q display-panes
    bind C-k rotate-window
    bind k rotate-window
    bind C-j last-pane
    bind j last-pane
    # List of plugins
    set -g @plugin 'tmux-plugins/tpm'
    set -g @plugin 'tmux-plugins/tmux-sensible'
    set -g @plugin 'tmux-plugins/tmux-yank'
    # Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
    run '~/.tmux/plugins/tpm/tpm'

    view rawtmux.conf hosted with ❤ by GitHub

사용방법

설정파일을 적용했다고 가정하고 사용법을 설명드리도록 하겠습니다.

기본적인 invoke키는 Ctrl+b이지만 저는 Ctrl+j로 변경해두었습니다.

시작하기

1
tmux 혹은 tmux new -s <원하는 이름>

: 추후에 attache(재사용)을 하기 편하게끔 이름을 지정하시는게 좋습니다. tmux new -s normal

창 분할하기

Ctrl+j를 누른 후에 |를 누르면 좌우로 분할되고,
Ctrl+j를 누른 후에 -를 누르면 상하로 분할됩니다.

창 이동하기

Ctrl+j를 누르고 다시 Ctrl+j를 누르면 이전 창으로 돌아갑니다.
Ctrl+j를 누르고 q를 누르면 창마다 숫자가 나오는데 이 때 해당 숫자를 누르면 그 창으로 이동합니다.

Tab 만들기

Ctrl+j를 누른 후에 c를 누르면 새로운 tab이 생성됩니다.

Tab간 이동하기

Ctrl+j를 누른 후에 n을 누르면 다음 tab으로 이동합니다.
Alt+&lt;숫자키&gt;를 누르면 해당 tab으로 이동합니다.

창 닫기

방법 1. shell에서 exit명령을 입력합니다.
방법 2. Ctrl+j를 누른 후에 x를 누르면 해당 pane을 닫을 것인지 묻습니다. 이 때 y를 선택하면 해당 창이 닫힙니다.

Detache / Attache 사용하기 (build걸고 퇴근하기)

  1. tmux를 실행하고 나서 빌드를 실행합니다.
  2. Ctrl+j를 누르고 d를 누르면 [detached (from session xxx)] 라고 뜨면서 tmux를 실행하기 전 Shell로 빠져나옵니다.
  3. 퇴근합니다.~
  4. 다음날 와서 tmux attach -t xxx를 치면 퇴근 전 화면으로 돌아갑니다.

Zoom기능 사용하기

tmux최신버전에는 zoom이라는 기능이 있습니다. 창을 분할해서 사용하다가 잠시 전체 화면으로 바꾸고, 다시 원래대로 돌아갈 수 있는 기능입니다.
Ctrl+j를 누른 후에 z를 누르면 현재 커서가 위치한 창이 전체 화면으로 전환되고, 다시 Ctrl+j를 누른 후에 z를 누르면 축소된 상태로 돌아가게 됩니다.

More

  1. 제 설정에는 bash대신 zsh가 tmux로 접속하면 실행되게 되어 있습니다.
    해당 설정을 원래대로 bash로 변경하고 싶으시면 ~/.tmux.conf에서
1
2
# set-option -g default-command "bash"
set-option -g default-command "zsh"

두 부분을 바꾸시면 됩니다. (#은 주석처리입니다.)

  1. tmux에 간단한 설명은 아래를 참고하세요.
    http://nodeqa.com/nodejs_ref/99

  2. 매번 tmux를 설정하고 창을 분할하는 작업을 하게 되는데 이걸 미리 설정해서 할 수 있는 Tool도 있습니다.
    https://github.com/tmuxinator/tmuxinator
    저도 설정해서 쓰고 있는데 mux session-name으로 간단히 new/attache를 할 수 있어서 편합니다.


출처 : https://code4rain.wordpress.com/2015/03/01/tmux-%EC%82%AC%EC%9A%A9%EB%B0%A9%EB%B2%95-%EC%B0%BD%EB%B6%84%ED%95%A0%ED%95%98%EA%B8%B0-%EB%B9%8C%EB%93%9C%EA%B1%B8%EA%B3%A0-%ED%87%B4%EA%B7%BC%ED%95%98%EA%B8%B0/


'리눅스(ubuntu) > Tmux 관련' 카테고리의 다른 글

tmux 간단한 명령어들  (0) 2017.04.06

+ Recent posts