组合键:

:new<回车> 启动新会话
s 列出所有会话
w 相当于s的时候展开列表,展开windows
$ 重命名当前会话

tmux kill-session -t demo # 关闭demo会话

他有一个session,windows,panes的概念

一个session可以有N个windows,N个panes

如果是最大化利用,那么需要找到一种可以跳来跳去的方法,不管是跳windows还是session。

一般来说是跳windows,因为windows下有几个不同的panes

所以,怎么跳最快?

name不管是pane还是windows,是不是只要有name就可以跳过去,不对,应该是windows之间的跳,因为一组windows就是一组任务。

一个session里面可以最下面的状态栏可以看到有几个windows,windows的名字如何

###windows的操作:

p  切换到上一个windows
n  切换到下一个widnwos
,  重命名窗口
.  修改当前窗口编号
f  根据关键词搜索windows   这个实际测试可用,可以跳session

###Pane的操作

{  向前置换面板
}  向后置换面板,这个有意思
z  最大化当前的面板,再输入一次就变回去了
Esc+1  横着的面板变成竖立状态

###Pane和windows之间的互换
比如你当前想把另外一个windows合并到现在的pane里面

join-pane -s window01 # 合并名称为window01的窗口的默认(第一个)面板到当前窗口中
join-pane -s window01.1 # .1显式指定了第一个面板,.2就是第二个面板(我本地将面板编号起始值设置为1,默认是0)

有另外一种方式,通过karabiner,单独在iterm2里面绑定某个组合键盘,比如cap绑定一个Home键,这样tmux的prefix就是单独按一下cap就可以了。

所以需要两个地方设置:

set -g prefix Home
unbind C-b
bind-key Home send-prefix

第二个地方需要在karabiner里面针对iterm2单独设置一个规则: 即按下cap等于home键。
这个是网上的一个例子,可以照着写

?xml version="1.0"?>
<root>
  <appdef>
    <appname>PYCHARM</appname>
    <equal>com.jetbrains.pycharm</equal>
  </appdef>
   <item>
    <name>custom settings</name>
    <item>
      <name>Change Functional Keys to F3...F10 for PyCharm</name>
      <identifier>remap.app_pycharm_functional2function</identifier>
      <only>PYCHARM</only>
      <autogen>__KeyToKey__ KeyCode::EXPOSE_ALL, KeyCode::F3</autogen>
      <autogen>__KeyToKey__ KeyCode::DASHBOARD, KeyCode::F4</autogen>
      <autogen>__KeyToKey__ KeyCode::LAUNCHPAD, KeyCode::F4</autogen>
      <autogen>__KeyToKey__ ConsumerKeyCode::KEYBOARDLIGHT_LOW, KeyCode::F5</autogen>
      <autogen>__KeyToKey__ ConsumerKeyCode::KEYBOARDLIGHT_HIGH, KeyCode::F6</autogen>
      <autogen>__KeyToKey__ ConsumerKeyCode::MUSIC_PREV, KeyCode::F7</autogen>
      <autogen>__KeyToKey__ ConsumerKeyCode::MUSIC_PLAY, KeyCode::F8</autogen>
      <autogen>__KeyToKey__ ConsumerKeyCode::MUSIC_NEXT, KeyCode::F9</autogen>
      <autogen>__KeyToKey__ ConsumerKeyCode::VOLUME_MUTE, KeyCode::F10</autogen>
    </item>
  </item>
</root>

获取程序的budnle identifiers:

> osascript -e 'id of app "WebStorm"'
  com.jetbrains.WebStorm

  > osascript -e 'id of app "Pycharm"'
  com.jetbrains.pycharm

tmux的最大化利用是远程服务器的时候,在iterm2里面新开一个标签。可以选择每一个vps一个标签,然后在每一个标签里面利用tmux,w

可以在iterm2里面修改切换tab是cmd + [,和cmd + ]


# -- general -------------------------------------------------------------------

set -g default-terminal "screen-256color" # colors!
setw -g xterm-keys on
set -s escape-time 10                     # faster command sequences
set -sg repeat-time 600                   # increase repeat timeout
set -s focus-events on

# Fix Ctrl+Arrow for PuTTY
set -g terminal-overrides "xterm*:kLFT5=\eOD:kRIT5=\eOC:kUP5=\eOA:kDN5=\eOB:smkx@:rmkx@"

# remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-d
bind-key C-d send-prefix

set -g mode-keys vi
# prefix-C-z causes blankscreen, tmux gets suspended
unbind C-z

set -q -g status-utf8 on                  # expect UTF-8 (tmux < 2.2)
setw -q -g utf8 on

# reload configuration
bind r source-file ~/.tmux.conf \; display '~/.tmux.conf sourced'


# -- display -------------------------------------------------------------------

set -g base-index 1           # start windows numbering at 1
setw -g pane-base-index 1     # make pane numbering consistent with windows

setw -g automatic-rename on   # rename window to reflect current program
set -g renumber-windows on    # renumber windows when a window is closed

set -g set-titles on          # set terminal title

set -g display-panes-time 800 # slightly longer pane indicators display time
set -g display-time 1000      # slightly longer status messages display time

set -g status-interval 10     # redraw status line every 10 seconds

# clear both screen and history
bind -n C-l send-keys C-l \; run 'sleep 0.1' \; clear-history

# activity
set -g monitor-activity on
set -g visual-activity off


# -- navigation ----------------------------------------------------------------

# create session
bind C-c new-session

# find session
bind C-f command-prompt -p find-session 'switch-client -t %%'

# split current window horizontally
bind - split-window -v -c '#{pane_current_path}'
# split current window vertically
bind \\ split-window -h -c '#{pane_current_path}'

# pane navigation
bind -r h select-pane -L  # move left
bind -r j select-pane -D  # move down
bind -r k select-pane -U  # move up
bind -r l select-pane -R  # move right
bind > swap-pane -D       # swap current pane with the next one
bind < swap-pane -U       # swap current pane with the previous one

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

# window navigation
unbind n
unbind p
bind -r C-h previous-window # select previous window
bind -r C-l next-window     # select next window
bind Tab last-window        # move to last active window

# -- list choice (tmux < 2.4) --------------------------------------------------

# vi-choice is gone in tmux >= 2.4
run -b 'tmux bind -t vi-choice h tree-collapse 2> /dev/null || true'
run -b 'tmux bind -t vi-choice l tree-expand 2> /dev/null || true'
run -b 'tmux bind -t vi-choice K start-of-list 2> /dev/null || true'
run -b 'tmux bind -t vi-choice J end-of-list 2> /dev/null || true'
run -b 'tmux bind -t vi-choice H tree-collapse-all 2> /dev/null || true'
run -b 'tmux bind -t vi-choice L tree-expand-all 2> /dev/null || true'
run -b 'tmux bind -t vi-choice Escape cancel 2> /dev/null || true'


# -- edit mode (tmux < 2.4) ----------------------------------------------------

# vi-edit is gone in tmux >= 2.4
run -b 'tmux bind -ct vi-edit H start-of-line 2> /dev/null || true'
run -b 'tmux bind -ct vi-edit L end-of-line 2> /dev/null || true'
run -b 'tmux bind -ct vi-edit q cancel 2> /dev/null || true'
run -b 'tmux bind -ct vi-edit Escape cancel 1> /dev/null || true'


# -- copy mode -----------------------------------------------------------------

bind Enter copy-mode # enter copy mode

run -b 'tmux bind -t vi-copy v begin-selection 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi v send -X begin-selection 2> /dev/null || true'
run -b 'tmux bind -t vi-copy C-v rectangle-toggle 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi C-v send -X rectangle-toggle 2> /dev/null || true'
run -b 'tmux bind -t vi-copy y copy-selection 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi y send -X copy-selection-and-cancel 2> /dev/null || true'
run -b 'tmux bind -t vi-copy Escape cancel 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi Escape send -X cancel 2> /dev/null || true'
run -b 'tmux bind -t vi-copy H start-of-line 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi H send -X start-of-line 2> /dev/null || true'
run -b 'tmux bind -t vi-copy L end-of-line 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi L send -X end-of-line 2> /dev/null || true'

# # copy to Mac OSX clipboard
# if -b 'command -v reattach-to-user-namespace > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | reattach-to-user-namespace pbcopy"'
# # copy to X11 clipboard
# if -b 'command -v xsel > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | xsel -i -b"'
# # if -b '! command -v xsel > /dev/null 2>&1 && command -v xclip > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | xclip -i -selection clipboard >/dev/null 2>&1"'
# # copy to Windows clipboard
# if -b 'command -v clip.exe > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | clip.exe"'
# if -b '[ -c /dev/clipboard ]' 'bind y run -b "tmux save-buffer - > /dev/clipboard"'


# -- buffers -------------------------------------------------------------------

bind b list-buffers  # list paste buffers
bind p paste-buffer  # paste from the top paste buffer
bind P choose-buffer # choose which buffer to paste from

# -- better UX -----------------------------------------------------------------

set -g mouse on
set -g history-limit 10000 # same as terminal itself
set -g pane-active-border-style bg=default,fg=colour4


# -- status line --------------------------------------------------------------
set -g status-position bottom
set -g status-justify left
set -g status-left-length 200
set -g status-right-length 200


set -g status-left "#[fg=colour16,bg=colour254,bold] ❐ #S #[fg=colour255,bg=colour24,bold] #(free -m | awk '/Mem/{printf($3)}')M  #[fg=colour232,bg=colour33,bold] #(vmstat -SM 1 2|awk 'END {print 100-$15}')% "
set -g status-right " #{?client_prefix,C-d,} #[fg=colour255,bg=colour24,bold] #{=21:pane_title} #[fg=colour255,bg=colour8,bold] #(uptime | cut -d ',' -f1 | sed 's/\ \ /\ /g') #[fg=colour232,bg=colour249,bold] #(echo #{pane_current_command}) #(ps -o args -p `pgrep -P #{pane_pid}`|grep ssh|cut -d ' ' -f2-)"

set -g status-style fg=colour15,bg=colour233
# set -g status-left-style fg=colour255,bg=colour233,bold
# set -g status-right-style fg=colour255,bg=colour233,bold

# -- window status -------------------------------------------------------------

# normal tab
set -g window-status-format " #I:#W #F "

# current tab
set -g window-status-current-format " #I:#W:#F "
set -g window-status-current-style fg=colour15,bg=colour233,bold

# activity in window
set -g window-status-activity-style fg=default,underscore

# bell
setw -g window-status-bell-style fg=colour1,bg=colour255,bold

# messages
setw -g message-style fg=black,bg=colour226,bold



set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# plugins
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-save-interval '0'
set -g @continuum-restore 'on'
run '~/.tmux/plugins/tpm/tpm'

再安装一个tpm插件:

mkdir -p ~/.tmux/plugins/
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

Press prefix + I (capital i, as in Install) to fetch the plugin.

I安装插件,

saved a session (prefix + ctrl-s)

最后的最后,如果是本机macosx,远程是vps,当复制远程vps内容的时候,复制的内容是到了远程主机的buffer,这个时候需要clipper来把远程主机复制到本地,我用的tmux版本是2.1。在mac上的ssh配置文件加上端口转发:

RemoteForward 8377 localhost:8377

配置开机启动:

sudo cp clipper /usr/local/bin
cp contrib/darwin/tcp-port/com.wincent.clipper.plist ~/Library/LaunchAgents/
launchctl load -w -S Aqua ~/Library/LaunchAgents/com.wincent.clipper.plist

然后在远程主机的tmux配置加上这样:

bind-key -t vi-copy Enter copy-pipe "nc localhost 8377"

这样就可以了。

在使用mosh的情况下,mosh是不支持端口转发的,需要先用ssh端口转发,然后再用mosh链接:

Host sandbox
  ControlMaster no
  ControlPath none
  Hostname sandbox.example.com

Host sandbox-clipper
  ControlMaster no
  ControlPath none
  ExitOnForwardFailure yes
  Hostname sandbox.example.com
  RemoteForward 8377 localhost:8377
  
ssh -N -f sandbox-clipper
mosh sandbox

clipper的文档讲的超级详细,可以看看。

https://github.com/wincent/clipper

2019-06-24
Contents

⬆︎TOP