这两天在修改blog,以前没怎么长时间写过css+html,后来发现很蛋疼的一件事就是每次调试的时候得看着终端生成blog页,代码得在终端下另一个标签里更改.切换起来很费劲,终于想起了大名鼎鼎的tmux.

这是配置的最终效果:

配置tmux

1 如果你用zsh,设置zsh作为默认的shell解析器
 set-option -g default-shell /bin/zsh 
2 配置前缀键,我一般是用<C-q>来调用终端,<C-a>来绑定tmux的prefix
set -g prefix C-a
unbind C-b
3 用vim的hjkl键来选择面板
bind-key k select-pane -U
bind-key j select-pane -D
bind-key h select-pane -L
bind-key l select-pane -R
4 调整面板大小,也是vim的四个方向键,很舒服
bind -r ^k resizep -U 10 # upward (prefix Ctrl+k)
bind -r ^j resizep -D 10 # downward (prefix Ctrl+j)
bind -r ^h resizep -L 10 # to the left (prefix Ctrl+h)
bind -r ^l resizep -R 10 # to the right (prefix Ctrl+l)
5 鼠标操作
#鼠标滚轮可以使用
set-window-option -g mode-mouse on
# 鼠标可以选中窗口
set-option -g mouse-select-pane on
6 复制模式copy-mode
  1. 前缀 [ 进入复制模式
  2. 按 space 开始复制,移动光标选择复制区域
  3. 按 Enter 复制并退出copy-mode。
  4. 将光标移动到指定位置,按 PREIFX ] 粘贴

如果把tmux比作vim的话,那么我们大部分时间都是处于编辑模式,我们复制的时候可不可以像vim一样移动呢?只需要在配置文件(~/.tmux.conf)中加入如下行即可。

#copy-mode 将快捷键设置为vi 模式
setw -g mode-keys vi

ps:如果想要返回终端鼠标复制的的话,按住shift就可以了

7 分屏操作
unbind '"'
bind - splitw -v # vertical split (prefix -)
unbind %
bind | splitw -h # horizontal split (prefix |)
8 使当前pane 最大化,需要一个脚本,脚本地址发现失效了,把代码的贴出来
#!/bin/bash

# Copyright (c) 2012 Juan Ignacio Pumarino, jipumarino@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Instructions
# ------------
#
# 1. Install this script and give it execute permission somewhere in your PATH.
#    For example:
#
#    $ mkdir -p ~/bin
#    $ wget https://raw.github.com/jipumarino/tmux-zoom/master/tmux-zoom.sh -O ~/bin/tmux-zoom.sh
#    $ chmod +x ~/bin/tmux-zoom.sh
#
# 2. Add a shortcut in your ~/.tmux.conf file:
#
#    bind C-k run "tmux-zoom.sh"
#
# 3. When using this shortcut, the current tmux pane will open in a new window by itself.
#    Running it again in the zoomed window will return it to its original pane. You can have
#    as many zoomed windows as you want.

current=$(tmux display-message -p '#W-#I-#P')
list=$(tmux list-window)

[[ "$current" =~ ^(.*)-([0-9]+)-([0-9]+) ]]
current_window=${BASH_REMATCH[1]}
current_pane=${BASH_REMATCH[2]}-${BASH_REMATCH[3]}
new_zoom_window=ZOOM-$current_pane

if [[ $current_window =~ ZOOM-([0-9]+)-([0-9+]) ]]; then
  old_zoom_window=ZOOM-${BASH_REMATCH[1]}-${BASH_REMATCH[2]}
  tmux select-window -t ${BASH_REMATCH[1]} \; select-pane -t ${BASH_REMATCH[2]} \; swap-pane -s $old_zoom_window.1 \; kill-window -t $old_zoom_window
elif [[ $list =~ $new_zoom_window ]]; then
  tmux select-window -t $new_zoom_window
else
  tmux new-window -d -n $new_zoom_window \; swap-pane -s $new_zoom_window.1 \; select-window -t $new_zoom_window
fi
9 终端启动初始化

这也是再网上找到的,如果是zsh的话就写在~/.zshrc里,他会在每次打开终端的时候开启两个窗口并在第二个窗口开启三个panel并启动一个htop.

tmux_init()
{
    tmux new-session -s "vim" -d -n "local"    # 开启一个会话
    tmux new-window -n "other"          # 开启一个窗口
    tmux split-window -h                # 开启一个竖屏
    tmux split-window -v "htop"          # 开启一个横屏,并执行top命令
    tmux -2 attach-session -d           # tmux -2强制启用256color,连接已开启的tmux
    selectp -t 1 #选中0panel
}
# 判断是否已有开启的tmux会话,没有则开启
if which tmux 2>&1 >/dev/null; then
test -z "$TMUX" && (tmux attach || tmux_init)
fi

powerline

伟大的poweline也兼容在tmux上了,项目地址在 https://github.com/erikw/tmux-powerline

详细配置都有介绍,最主要的是还可以自己定制插件(shell),左右插件位置都可以设置,很方便.

自己常用操作

  1. 创建窗口 <C-a>+C
  2. 选择窗口 <C-a>+w
  3. 切换上一个下一个会话 <C-a>+<n,p>
  4. 切换面板 <C-a>+hjkl
  5. 调整面板大小<C-a>+<C-hjkl>
  6. 切换窗口<C-a>+1,2,3,4...
  7. 分屏操作<C-a>+-<C-a>+|
  8. 关闭当前面板<C-a>+q
  9. 上面那个最大化当前的面板,我绑定了k键但是和调整面板大小的按键有冲突(不管了)<C-a>+<C-k>
  10. 面板顺时针移位<C-a>+<C-o>
  11. tmux自带内置布局<C-a>+空格可以切换

参考资料:http://mingxinglai.com/cn/2012/09/tmux/