个人的tmux使用习惯

tmux scripts

Tmux is a server/client application, with a socket working in background and can support may sessions, windows and panes.

Tmux is to console as what a desktop to GUI

  • multiple task in the terminal, run many tasks in separate panes.
  • panes can be tiled in windows, and windows can be further grouped in sessions, just like current GUI support many virtual desktops
  • server
  • A unix socket server, on my macbook the default socket file is located in /private/tmp/tmux-502/default, or you can specify a different socket file with options: -L "socket-file-name" or -S "socket-file-path"
  • client
  • A client can attach to any tmux socket to operate on a certain session.
  • session
  • window
  • pane
  • target
  • tmuxp

 

server

tmux -L moo
#start a new tmux server + session if non-exists under the socket name “moo”. With this command we create a local socket file 'moo'

tmux -L moo attach
#try to re-attach to session if one exists

client and session are different, but closely related. client has one status line, and session have a attached state.

tmux list-client
/dev/ttys000: 0 [134x47 xterm-256color] (utf8)
/dev/ttys002: 0 [134x47 xterm-256color] (utf8)
tmux list-sessions
0: 1 windows (created Tue Jan 17 09:47:13 2017) [134x46] (attached)
1: 3 windows (created Tue Jan 17 10:03:40 2017) [134x46]

sessions

tmux
# is the same as 'tmux new-session
tmux new-session -s"my clojure project"
shortcut action
Prefix + ( previous session
Prefix + ) next session
Prefix + L Last session
Prefix + s select interactively to a new session
Prefix + $ rename session
tmux switch-client -t dev
# will switch to a sesison named 'dev', if it exits.

tmux rename-session -t1 "my session"
# rename a session from '1' to 'my session'

tmux has-session -t1
# test whether there is a session named '1'

if tmux has-session -t0 ; then
echo "has session 0"
fi
# can be used in script programming

clients

To attach a client to a session, run command tmux attach -t'session-name', if no session-name specified, choose the first session from the session list. Command tmux list-sessions can be used to show the session list.

A session can be attached to [0 n] clients, and some clients can be readonly(ro)

MacBook:~ lizy$ tmux list-client
/dev/ttys002: main-pane-height [154x40 xterm-256color] (utf8)
/dev/ttys012: main-pane-height [154x40 xterm-256color] (utf8)
/dev/ttys013: main-pane-height [154x40 xterm-256color] (utf8) (ro)

windows

shutcut action
Prefix + number traversing windos
Prefix + ‘ prompt for a window index
Prefix + f window’s name for text match
Prefix + , rename a window

Panes

shutcut action
Prefix + [ scroll mode
use ‘enter’ and ‘space’ to copy copy
use ‘prefix + ]’ paste
In scroll mode, use vi viusal mode to select text visual mode

session persistency

tmuxp, can freeze sessions into a .json/.yaml(yet another markup language)

pip install tmuxp

tmuxp has lots of great layout examples to start with:examples

tmux config

Default config file is located in ~/.tmux.conf

# set scroll history to 10,000 lines
set -g history-limit 10000

# use vi key binding
setw -g mode-keys vi

# don't use a login shell
set -g default-command /bin/zsh

# select certain pane
bind j select-pane -D
bind k select-pane -U
bind h select-pane -L
bind l select-pane -R

# split window
bind | split-window -h
bind - split-window -v

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

# baord color
set -g pane-border-fg white
set -g pane-active-border-fg cyan
set -g pane-active-border-bg cyan

# status bar
set -g status-left "session: #S || window: #I || pane: #P"

tmuxp

session_name: solarsystem
start_directory: '.'
windows:
- layout: main-horizontal
options:
main-pane-height: 30
panes:
- shell_command:
- atom .
- shell_command:
- rlwrap lein figwheel
- shell_command:
- tig
window_name: "coding"

Questions

vi mode and emacs mode, how to config, which one to use?

reference

https://leanpub.com/the-tao-of-tmux/read

http://perltricks.com/article/an-introduction-to-tmux/

发表评论

电子邮件地址不会被公开。 必填项已用*标注