Permalink: 2014-05-08 09:52:32 by ning in misc tags: all

1   why

  1. 配置更可读, 比如状态栏:

    #screen
    hardstatus alwayslastline '%{gk}[ %{G}%H %{g}][%= %{wk}%?%-Lw%?%{=b kR}(%{W}%n*%f%t%?(%u)%?%{=b kR})%{= kw}%?%+Lw%?%?%= %{y}][%{G} %l%{y}] %{y}[%{G} %m/%d %c %{y}]%{W}'
    
    #tmux
    set -g status-left ' #[fg=green,bold]#H#[default]'
    set -g status-right '#[fg=yellow,bold]#(cut -d " " -f 1-4 /proc/loadavg)#[default] #[fg=cyan,bold]%Y-%m-%d %H:%M:%S#[default] '
    
    screen的配置完全看不懂 颜色在哪, 看上去更像魔法.
    
  2. 可编程

    nearly all commands can be invoked from the command line,

    screen 也可以的.

  3. Window Splitting

  4. 和screen一样, 也支持 前缀. 比如 tmux attach 可以简写为 tmux att

  5. 列出所有window, 在window很多的时候 有用:

    Ctrl-b w (List all windows / window numbers)
    
  6. 动态修改配置(有些属性不能及时生效, 要杀掉所有session):

    By default tmux reads ~/.tmux.conf only if it was not already running. To have tmux load a configuration file afterwards, execute:
    tmux source-file <path>
    
  7. 很多外部工具, 如tmuxp

2   安装

因为我的ubuntu tmux是1.1.. ubuntu 也太老, 只能自己编译了.. 需要libevent 2.x and ncurses .

用这个脚本: https://gist.github.com/ryin/3106801

3   使用

3.1   session

#creates a new tmux session named session_name
tmux new -s session_name

#attaches to an existing tmux session named session_name
tmux attach -t session_name

#switches to an existing session named session_name
tmux switch -t session_name

#lists existing tmux sessions
tmux list-sessions

#detach the currently attached session
tmux detach (prefix + d)

3.2   window

create a new window
tmux new-window (prefix + c)

move to the window based on index
tmux select-window -t :0-9 (prefix + 0-9)

rename the current window
tmux rename-window (prefix + ,)

3.3   pane

tmux split-window (prefix + ")
splits the window into two vertical panes

tmux split-window -h (prefix + %)
splits the window into two horizontal panes

tmux swap-pane -[UDLR] (prefix + { or })
swaps pane with another in the specified direction

tmux select-pane -[UDLR]
selects the next pane in the specified direction

tmux select-pane -t :.+
selects the next pane in numerical order

3.4   copy-paste

空格开始选择, Enter确认copy 1. enter copy mode using Control+b [ 2. navigate to beginning of text, you want to select and hit Space 3. move around using arrow keys to select region 4. when you reach end of region simply hit Enter to copy the region 5. now Control+b ] will paste the selection

配置:

# Setup 'v' to begin selection as in Vim
bind-key -t vi-copy v begin-selection
bind-key -t vi-copy y copy-selection
#bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"

# 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 p paste-buffer

这样就可以 ^a[ 进入回滚模式, v 开始选择, y 拷贝, q 退出,

^a p 粘贴.

3.5   交换两个window

当前window和1号window交换:

swap-window -t 1

3.6   kill-window

tmux kill-window -t ndb:7

3.7   帮助

列出命令:

ning@ning-laptop:~/idning/blog_and_notes$ tmux lscm
attach-session [-d] [-t target-session]
bind-key [-cnr] [-t key-table] key command [arguments]
break-pane [-t target-pane] [-d]
choose-client [-t target-window] [template]
choose-session [-t target-window] [template]
choose-window [-t target-window] [template]

列出绑定:

^A?

4   问题

  1. 用^A ^A 来在最常用的两个window间切换:

    #select last window
    bind-key C-a last-window
    
  2. .bashrc/.profile is not loaded on new tmux session

    tmux加载 .bash_profile 需要在 .bash_profile 中加入:

    . ~/.bashrc
    
  3. new 和attatch 的标识不一样:

    tmux new -s
    tmux attach -t
    

    好在有这个alias:

    alias t="if tmux has-session -t $USER; then tmux attach -t $USER; else tmux new -s $USER; fi"
    

    不过这个不能指定名字, 用函数:

    t(){
        if [ $# -eq 0 ]
          then
            tmux ls
          else
            if tmux has-session -t $@; then tmux attach -t $@; else tmux new -s $@; fi
        fi
    
    }
    
  4. ctrl-left 不能用, 特别是在vim里面, 需要配置一下:

    tmux.conf:

    set-window-option -g xterm-keys on
    

    .vimrc:

    "for tmux
    if &term =~ '^screen'
        " tmux will send xterm-style keys when xterm-keys is on
        execute "set <xUp>=\e[1;*A"
        execute "set <xDown>=\e[1;*B"
        execute "set <xRight>=\e[1;*C"
        execute "set <xLeft>=\e[1;*D"
    endif
    
  5. 双层嵌套退出:

    tmux detach  -s tmp
    
  1. vim里面 shift+tab 不能用, 至今无解.

  2. tagbar等乱掉:

    https://plus.google.com/u/0/+VysakhSreenivasan/posts/4heRzoxbM72

    在tmux.conf里面加上:

    set-window-option -g utf8 on
    

5   工具

5.1   tmuxp

pip install tmuxp

保存会话(我试用有问题, ):

usage: tmuxp freeze [-h] [-L socket-name] [-S socket-path]
                session_name [session_name ...]

ning@ning-laptop ~/test$ tmuxp load a.yaml
(E) [10:05:09] tmuxp.cli cli.main():952 [u'tmux: unknown option -- V', u'usage: tmux [-28lquv] [-c shell-command] [-f file] [-L socket-name]', u'            [-S socket-path] [command [flags]]']

5.2   workspace

修改了我的workspace脚本, 老的:

@task
def redissrc():
    cmd('''
gnome-terminal --geometry=120x40
--tab --working-directory ~/idning/blog_and_notes
--tab --working-directory ~/idning/blog_and_notes
--tab --working-directory ~/idning-github/redis/src
--tab --working-directory ~/idning-github/twemproxy/src
--tab --working-directory ~/idning-github/twemproxy/src
''')

新的:

@task
def redismgr():
    startcmd = '''
        tmux new -d      -s redismgr -n src  -c ~/idning-github/redis-mgr/
        tmux new-window  -t redismgr -n src  -c ~/idning-github/redis-mgr/
        tmux new-window  -t redismgr -n src  -c ~/idning-github/redis-mgr/
        tmux new-window  -t redismgr -n note -c ~/idning/blog_and_notes/
        tmux new-window  -t redismgr -n note -c ~/idning/blog_and_notes/
        '''

    if not _tmux_exists('redismgr'):
        cmd2(startcmd)
    cmd('tmux attach -t redismgr')

类似的:

@task
def work():
    cmd('''
gnome-terminal --geometry=120x40  --name=abc
--tab --working-directory /home/ning/mywork/work-log
--tab --working-directory /home/ning/mywork/work-log
--tab --working-directory /home/ning/mywork/codebase-git/
--tab --working-directory /home/ning/mywork/codebase
''')

    cmd('xdotool key alt+1')
    cmd('xdotool type "vim M/201402.rst"')
    cmd('xdotool key  Return F8')
    #cmd('xdotool key  alt+Tab')

换成了:

@task
def work():
    startcmd = '''
        tmux new -d      -s work -n worklog     -c /home/ning/mywork/work-log/
        tmux new-window  -t work -n worklog     -c /home/ning/mywork/work-log/
        tmux new-window  -t work -n git         -c /home/ning/mywork/codebase-git/
        tmux new-window  -t work -n code        -c /home/ning/mywork/codebase/
        '''

    if not _tmux_exists('work'):
        cmd2(startcmd)

    cmd('tmux send-keys -t work:0 "vi M/201405.rst" Enter')
    cmd('tmux select-window -t work:0')
    cmd('tmux attach -t work')

6   小结

  • 比screen好(可定制性强)
  • 自己写脚本管理, 比用tmuxp这样的工具好

Comments