Post on 15-Jan-2015
description
transcript
tmux
tmuxA Great Terminal Multiplexer
lgfang
Alcatel-Lucent QingDao R&D
December 9, 2013
lgfang | QD R&D | December 9, 2013 1 / 47
tmux
Agenda
1 Why Tmux
2 Feature Highlight
3 Comparisons
4 Setup Terminal
5 More Usages
6 “Advanced” skills and tips
7 Q&A
lgfang | QD R&D | December 9, 2013 2 / 47
tmux | Why Tmux
Agenda
1 Why Tmux
lgfang | QD R&D | December 9, 2013 3 / 47
tmux | Why Tmux
The Itch
Isn’t it boring and in-efficient that
Loosing everything when the connection to server broken?
Switching among dozens of terminal windows.
Drag&Drop again and again to tile terminal windows
Can not share current terminal with others.
Connected to a console directly
lgfang | QD R&D | December 9, 2013 4 / 47
tmux | Why Tmux
The Itch
Isn’t it boring and in-efficient that
Loosing everything when the connection to server broken?
Switching among dozens of terminal windows.
Drag&Drop again and again to tile terminal windows
Can not share current terminal with others.
Connected to a console directly
lgfang | QD R&D | December 9, 2013 4 / 47
tmux | Why Tmux
The Itch
Isn’t it boring and in-efficient that
Loosing everything when the connection to server broken?
Switching among dozens of terminal windows.
Drag&Drop again and again to tile terminal windows
Can not share current terminal with others.
Connected to a console directly
lgfang | QD R&D | December 9, 2013 4 / 47
tmux | Why Tmux
The Itch
Isn’t it boring and in-efficient that
Loosing everything when the connection to server broken?
Switching among dozens of terminal windows.
Drag&Drop again and again to tile terminal windows
Can not share current terminal with others.
Connected to a console directly
lgfang | QD R&D | December 9, 2013 4 / 47
tmux | Why Tmux
The Itch
Isn’t it boring and in-efficient that
Loosing everything when the connection to server broken?
Switching among dozens of terminal windows.
Drag&Drop again and again to tile terminal windows
Can not share current terminal with others.
Connected to a console directly
lgfang | QD R&D | December 9, 2013 4 / 47
tmux | Why Tmux
What is tmux
A Terminal Multiplexer capable of
detach/re-attach
remote-share, pair-working (esp. remote pair-working)
window split and tiling
grouping windows
re-sizing windows
. . .
Meanwhile, it is
Easy to use
Well defined command line interface
Well documented
BSD licensed
lgfang | QD R&D | December 9, 2013 5 / 47
tmux | Why Tmux
What is tmux
A Terminal Multiplexer capable of
detach/re-attach
remote-share, pair-working (esp. remote pair-working)
window split and tiling
grouping windows
re-sizing windows
. . .
Meanwhile, it is
Easy to use
Well defined command line interface
Well documented
BSD licensed
lgfang | QD R&D | December 9, 2013 5 / 47
tmux | Feature Highlight
Agenda
2 Feature Highlight
lgfang | QD R&D | December 9, 2013 6 / 47
tmux | Feature Highlight
Detach, re-attach and remote share
This Page Intentionally Left Blank :)
lgfang | QD R&D | December 9, 2013 7 / 47
tmux | Feature Highlight
Split at will
lgfang | QD R&D | December 9, 2013 8 / 47
tmux | Feature Highlight
Split at will
lgfang | QD R&D | December 9, 2013 8 / 47
tmux | Feature Highlight
Split at will
lgfang | QD R&D | December 9, 2013 8 / 47
tmux | Feature Highlight
Or, preset layouts
lgfang | QD R&D | December 9, 2013 9 / 47
tmux | Feature Highlight
Auto-resize when shared
lgfang | QD R&D | December 9, 2013 10 / 47
tmux | Feature Highlight
Group windows according to project/work item etc.
lgfang | QD R&D | December 9, 2013 11 / 47
tmux | Feature Highlight
The Simpler The Better
lgfang | QD R&D | December 9, 2013 12 / 47
tmux | Feature Highlight
Easy to configure
Configure file $HOME/.tmux.conf
# default prefix ’C-b’
set-option -g prefix F12
bind-key | split-window -h
bind-key _ split-window -v
lgfang | QD R&D | December 9, 2013 13 / 47
tmux | Feature Highlight
Even support mouse
set-option -g mouse-select-pane on
set-option -g mouse-resize-pane on
set-option -g mouse-select-window on
set-window-option -g mode-mouse on
lgfang | QD R&D | December 9, 2013 14 / 47
tmux | Feature Highlight
Easy to use
This is enough to get you started1.
New session tmux [new -s session-name]
Attach tmux attach [-t session-name]
Detach F12 d
New window F12 c
Split vertically F12
Split horizontally F12 |
Change layout F12 space
Kill pane 2F12 x
1I bind prefix to F122Use “exit”. “kill pane” only when it does not response.
lgfang | QD R&D | December 9, 2013 15 / 47
tmux | Feature Highlight
Easy to install
If you can not persuade your system administrator, install one underyour HOME.
# install libevent if not already
tar xvfz libevent -1.4.14b-stable.tar.gz
cd libevent -1.4.14b-stable
./ configure --prefix=$HOME /.local
make && make install
echo "LD_LIBRARY_PATH=$HOME /.local/bin:$LD_LIBRARY_PATH" >>
↪→~/. bash_profile
tar xvfz tmux -1.6. tar.gz && cd tmux -1.6/
./ configure --prefix=$HOME /.local \
CFLAGS="-I$HOME /.local/include" \
LDFLAGS="-L$HOME /. local/lib"
# To statically link tmux: --enable -static
make && make install
echo "PATH=$HOME /.local/bin:$PATH" >> ~/. bash_profile
lgfang | QD R&D | December 9, 2013 16 / 47
tmux | Feature Highlight
Easy scripting
It is simple to control tmux in a shell script. Refer to my start up script
for an example.
lgfang | QD R&D | December 9, 2013 17 / 47
tmux | Feature Highlight
Free as in “freedom”
Last but not least: it is BSD-licensed.Which means, it is free to use, modify, re-distribute, or even makeprofit from (sell) it.
lgfang | QD R&D | December 9, 2013 18 / 47
tmux | Comparisons
Agenda
3 Comparisons
lgfang | QD R&D | December 9, 2013 19 / 47
tmux | Comparisons
Copmared with secureCRT
I prefer (“putty + tmux + to”) since, as far as I know, secureCRT
can not detach/attach
can not group windows per projects(or bug/escalation etc.)
can not tile
can not remote share
not convenient if the host list is really long
can not jump to dest host via an intermediate server
your hot-keys is not available other PCs, or after re-install
seems it does not support UTF-8
non-free
lgfang | QD R&D | December 9, 2013 20 / 47
tmux | Comparisons
Compared with gnu “screen”
As far as I know, “screen”
Is a little bit more complicated to configure
Is not as easy as tmux to split window
Can not resize window
lgfang | QD R&D | December 9, 2013 21 / 47
tmux | Comparisons
Compared with VNC
The cons of VNC:
Depends on X (GUI environment)
Consume more CPU and network traffic (I think)
Can not resize the desktop
Does not tile3
The pros of VNC:
Uses dedicated password instead of login/password for sharing
Supports X applications
3To be accurate, you can start a tiling WM in vnc. Average engineers just don’tbother to do that.lgfang | QD R&D | December 9, 2013 22 / 47
tmux | Setup Terminal
Agenda
4 Setup Terminal
lgfang | QD R&D | December 9, 2013 23 / 47
tmux | Setup Terminal
Server Side
TERM and color
TERM=xterm-256color to indicate tmux to support 256 colors.4
Or, force 256 color with tmux attach -2
Ensure your locale is utf8.For instance: LC ALL=en US.utf8
4TERM=xterm only supports 8 colorslgfang | QD R&D | December 9, 2013 24 / 47
tmux | Setup Terminal
Setup PuTTY
To cope with UTF8 chars correctly.
To not intercept hot-keys sent to tmux.
A way to not send keys to application.
Here is an example:
Example (Seting up putty)
Window → Translation → Received data ... character set : UTF-8 a
Terminal → Keyboard → The Function keys and keypad : XtermR6
Window → Selection → Shift overrides applications use of mouse
ayour putty can receive and display Chinese characters(UTF-8) now
lgfang | QD R&D | December 9, 2013 25 / 47
tmux | More Usages
Agenda
5 More Usages
lgfang | QD R&D | December 9, 2013 26 / 47
tmux | More Usages
You are ready to get started already. However, you may find followingslides useful.
lgfang | QD R&D | December 9, 2013 27 / 47
tmux | More Usages
Eye candy
lgfang | QD R&D | December 9, 2013 28 / 47
tmux | More Usages
Copy mode
Entering “copy mode” to view,search and copy screen output.
Entering copy mode F12 [ ( or F12 PageUp )
Scroll up/down: PageUp /PageDown (or emacs/vi key bindings ormouse).
Search: Ctrl +s then n / N (for mode-keys emacs)
Paste: F12 ]
Show “clip board”: F12 =
set-window-option -g mode-keys emacs
set-option -g history-limit 600000
lgfang | QD R&D | December 9, 2013 29 / 47
tmux | More Usages
Panes
Select pane F12 Tab (or one of ← ↑ → ↓ )
Swap two adjacent panes F12 {
Break current pane to a new window F12 !
Join a pane to current window F12 j x.y
Swap with pane y of window x F12 m x.y
Toggle Synchronize mode5 F12 Ctrl + s
bind-key Tab select-pane -t:.+
bind-key BTab select-pane -t:.-
set-option -g display-time 3000
bind-key j command-prompt "join-pane -s ’%1’"
bind-key m command-prompt -p "move (swap) current pane with: "
↪→"swap-pane -s %1"
bind-key C-s setw synchronize-panes
5Duplicate input (key-stokes) to all panes in the same windowlgfang | QD R&D | December 9, 2013 30 / 47
tmux | More Usages
Windows
To rename a window: F12 , new name
Ways to select certain window
Last Window F12 l will select last (previous “current”)window.
Using index F12 0 - 9
Next window F12 n
Previous window F12 p
lgfang | QD R&D | December 9, 2013 31 / 47
tmux | More Usages
Sessions within tmux client
New session F12 :new-session -s name
Choose(switch) session F12 s
move window among sessions F12 :move-window -t sessionName
lgfang | QD R&D | December 9, 2013 32 / 47
tmux | More Usages
Run Complicated Commands
Command mode: F12 : source-file .tmux.conf
Command line command: tmux source-file .tmux.conf
lgfang | QD R&D | December 9, 2013 33 / 47
tmux | More Usages
Apply Configuration
F12 :source-file .tmux.conf
Why “restart” tmux does not make new configure take effect?You just “restart” a session, not the tmux server.
What does “-g” in “set-option -g” mean?Without “-g”, the option applies to current session only.
lgfang | QD R&D | December 9, 2013 34 / 47
tmux | “Advanced” skills and tips
Agenda
6 “Advanced” skills and tips
lgfang | QD R&D | December 9, 2013 35 / 47
tmux | “Advanced” skills and tips
Log Screen Outputs
When you want screen log of current pane, save it with: F12 Ctrl + h
bind-key C-h command-prompt -p "save log to:" "copy-mode; send-keys
↪→’M->’ C-e C-space ’M-<’ C-a C-w; save-buffer %%"
# # if "set-window-option -g mode-keys vi", use this one:
# bind-key C-h command-prompt -p "save log to:" "copy-mode; send-keys
↪→g ’space’ G ’enter’; save-buffer %%"
lgfang | QD R&D | December 9, 2013 36 / 47
tmux | “Advanced” skills and tips
Another Way to Log Screen Outputs
Though I do not prefer this one, here it is:
Start logging F12 h
Stop logging F12 H6
# for mode-keys emacs
bind-key h pipe-pane "cat >>$HOME/#S-#I-#P.log" \; display-message
↪→"Start logging($HOME/#S-#I-#P.log)"
bind-key H pipe-pane \; display-message "Stop logging(#S-#I-#P.log)"
6shift + h
lgfang | QD R&D | December 9, 2013 37 / 47
tmux | “Advanced” skills and tips
Nested tmux
There are sometimes nested tmux sessions (refer to my start up script ).
1 Start a tmux session on local Linux box.
2 From that tmux session, ssh to a server and run another tmuxsession.
To send prefix to
nested tmux session Press F11 .
outer tmux session F12 .
tmux session nested twice F11 C-b
one more layer7 of nest F11 C-b C-b .
bind-key -n F11 send-prefix
bind-key C-b send-prefix
7do you really want a nested nested nested ... session?lgfang | QD R&D | December 9, 2013 38 / 47
tmux | “Advanced” skills and tips
Ways to bind-key
Normal:bind-key Tab select-pane -t:.+
Repeatable: bind-key -r Space next-layout
Without prefix: bind-key -n F11 send-prefix
lgfang | QD R&D | December 9, 2013 39 / 47
tmux | “Advanced” skills and tips
“Maximize” a pane
Use cases:
Show more in the pane for a little while
Maximize, Copy (drag mouse) and Restore
# from http://superuser.com/questions/238702/maximizing-a-pane-in-tmux
unbind +
bind-key + run "if [[ $(tmux list-window) =~ MAX ]]; then \
tmux last-window;\
tmux swap-pane -s MAX.0; \
tmux kill-window -t MAX; \
else tmux new-window -d -n MAX; \
tmux swap-pane -s MAX.0; \
tmux select-window -t MAX;fi"
lgfang | QD R&D | December 9, 2013 40 / 47
tmux | “Advanced” skills and tips
Shell Scripting Tmux
This script starts tmux sessions, creates windows and run commands inthem.
#!/bin/bash
# Created: Fang lungang 2011; Modified: Fang lungang 07/08/2013 16:44>
SESSION_NAME=daily;SVR="135.252.41.248"
tmux has -session -t $SESSION_NAME 2>/dev/null && exec tmux attach -t $SESSION_NAME
tmux new -session -d -s $SESSION_NAME
if hostname | grep -q "\( lgfang \| lungang \)"; then # On my local PC
if ping -c 1 -W 1 $SVR; then
tmux new -window -d -n qdbbuild2 -t $SESSION_NAME :9 \
"ssh -t lungangfang@$SVR ’source .bash_profile; .local/bin/tmux -go.sh’"
tmux select -window -t $SESSION_NAME :9
# make it feels like I’m working on SVR locally
tmux set -option -g -t $SESSION_NAME status off
tmux set -option -g -t $SESSION_NAME prefix F11
fi
else # on servers , a window for emacs (start it if needed).
tmux new -window -d -n emacsclient -t $SESSION_NAME :9 \
"emacsclient -t || (emacs --daemon && emacsclient -t)"
fi
exec tmux attach -t $SESSION_NAME
lgfang | QD R&D | December 9, 2013 41 / 47
tmux | “Advanced” skills and tips
Command Line Auto-complete
if [ -n "$BASH_VERSION" -a -f
↪→$HOME /.local/bin/bash_completion_tmux.sh ]; then
source $HOME /.local/bin/bash_completion_tmux.sh
fi
# -n $BASH_VERSION ==> we are in bash
lgfang | QD R&D | December 9, 2013 42 / 47
tmux | “Advanced” skills and tips
auto-login and hot-keys
Some can not live without
auto-login: the emulators remember hostip/username/password.
auto-hotkey: a windows software enable you to define your ownhotkeys.
Do not worry, I wrote “to” in expect, which is (IMHO) moreconvenient.
lgfang | QD R&D | December 9, 2013 43 / 47
tmux | “Advanced” skills and tips
Share Sessions among Users 8
To share all:
Find corresponding socket by echo $TMUX in the sessionMake it readable/writable to others chmod 777 /path/to/socket
Others attach with tmux -S /path/to/socket attach
To separate shared sessions from others:
tmux -S /path/to/mySocketName [new -session -s
↪→session_name]
8Seems to me the only benefit is that you do not share your passwordlgfang | QD R&D | December 9, 2013 44 / 47
tmux | Q&A
Agenda
7 Q&A
lgfang | QD R&D | December 9, 2013 45 / 47
tmux | Q&A
Q&A
Questions?Thanks
lgfang | QD R&D | December 9, 2013 46 / 47
tmux | Appendix
Reference card according to my configure
List Keys F12 ?
Detach F12 d
New window F12 c
Split vertically F12
Split horizontally F12 |
Change layout F12 space
Kill pane F12 x
Select window F12 0 - 9
Last window F12 l
Rename window F12 , new name
Save log F12 Ctrl + h
Select pane F12 Tab (or one of ← ↑ → ↓ )
Break current pane F12 !
Join a pane F12 j x.y
Swap two panes F12 {
lgfang | QD R&D | December 9, 2013 47 / 47