资讯专栏INFORMATION COLUMN

优雅地使用命令行:Tmux 终端复用

lykops / 1229人阅读

摘要:每个有若干个,每个又可以分成多个窗格。极大地满足用户的需求。此外即使意外关闭也没关系,因为完全保存在中。我始终觉得,后端就应该拥抱,能纯键盘命令行操作效率真的是太高了。

什么是 Tmux

你是否曾经开过一大堆的 Terminal?有没有把它们都保存下来的冲动?Tmux 的Session就是做这件事情的! 你可以随时退出或者进入任何一个 Session。每个 Session 有若干个 Window,每个 Window 又可以分成多个窗格(Pane)。 极大地满足 Terminal 用户的需求。

此外即使 Terminal 意外关闭也没关系,因为 Session 完全保存在 Tmux Server 中。 再次打开 Terminal 时只需$ tmux attach便可回到你的工作区,就像从未退出过一样。

同时 Tmux 还支持“远程协助”,原本在服务器中,大家ssh上去之后都是互不干扰的操作,如果进入了相同的 Tmux Session,所有人看到的画面都是一样的,你可以看到别人的操作,别人也可以看到你的操作,你们的操作也会互相影响,一般在服务器出现难题需要人协助的时候,可以通过这个实时“直播”大神的操作。

分享我的 Tmux 配置

因为是小公司,没有运维,作为后端,也不可避免的负责了一部分运维的工作,还好自己Linux的操作还算熟悉,就写了一个自己用的 Tmux 配置,不一定适合所有人,但是我个人用的确实挺顺手。配置的快捷键是基于 Emacs 快捷键操作的( Emacs 绝对是最好的编辑器,哈哈哈 )

######################
### DESIGN CHANGES ###
######################
set -g bell-action any
set -g terminal-overrides "rxvt-unicode*:sitm@,ritm@"
set -g default-terminal "screen-256color"

unbind C-b
set -g default-shell /bin/bash
set -g prefix C-x
set -g status-keys emacs
set -gw mode-keys emacs

set-option -g default-command "which reattach-to-user-namespace > /dev/null && reattach-to-user-namespace -l $SHELL || $SHELL"
# bind-key -t emacs-copy M-w copy-pipe "reattach-to-user-namespace pbcopy"
bind-key -n C-y paste-buffer

bind-key C-x send-prefix
set-option -g history-limit 30000
bind r source-file ~/.tmux.conf ; display-message "Reloading tmux config ..."


bind-key -nr C-M-P resize-pane -U 5
bind-key -nr C-M-N resize-pane -D 5
bind-key -nr C-M-F resize-pane -R 5
bind-key -nr C-M-B resize-pane -L 5

bind-key -n M-p select-pane -U
bind-key -n M-n select-pane -D

bind-key -nr M-o display-panes

bind-key 2 split-window
bind-key 3 split-window -h

bind-key -nr M-, previous-window
bind-key -nr M-. next-window


# start window indexing at one instead of zero
set-option -g base-index 1
set-window-option -g pane-base-index 1

# enable wm window titles
set -g set-titles on
# wm window title string (uses statusbar variables)
set -g set-titles-string "tmux:#I #W"
# session initialization

set -g @tpm_plugins "          
  tmux-plugins/tpm             
  tmux-plugins/tmux-sensible   
"

set -g @tpm_plugins "          
  tmux-plugins/tpm             
  tmux-plugins/tmux-copycat    
"

# set -g @plugin "jimeh/tmux-themepack"
# set -g @themepack "powerline/default/gray"
# set -g @plugin "seebi/tmux-colors-solarized"
# set -g @colors-solarized "dark"

# enable wm window titles
set -g set-titles on
# wm window title string (uses statusbar variables)
set -g set-titles-string "tmux:#I #W"

# Default termtype. If the rcfile sets $TERM, that overrides this value.
set -g default-terminal screen-256color

set -g status-left-length 52
set -g status-right-length 451
set -g status-fg white
set -g status-bg colour234
set -g pane-border-fg colour245
set -g pane-active-border-fg colour39
set -g message-fg colour16
set -g message-bg colour221
set -g message-attr bold

set -g status-left "#[fg=colour235,bg=colour252,bold] ❐ #S #[fg=colour252,bg=colour238,nobold]#[fg=colour245,bg=colour238,bold] #(whoami) #[fg=colour238,bg=colour234,nobold]"
set -g window-status-current-format "#[fg=colour234,bg=colour39]#[fg=black,bg=colour39,noreverse,bold] #I: #W #[fg=colour39,bg=colour234,nobold]"

set -sg escape-time 0

把这份配置写入~/.tmux.conf文件,然后$ pkill tmux && tmux就可以了

Tmux 快捷键

我主要是把 Tmux 的 Ctrl+b 换成了 Emacs 的 Ctrl+x,其他的快捷键没怎么修改,新增了一些 Emacs 上常用的快捷键,下面说说几个常用的快捷键,其余的大家可以看配置文件
ps:ctrl+x是先按ctrl,再按x,并不是按住,ctrl-alt才是按住不放

// pane 操作
ctrl+x 2   //上下分屏
ctrl+x 3   //左右分屏
ctrl+x x   //删除当前pane
ctrl+x o   //pane切换
alt+n      //下一个pane
alt+p      //上一个pane
ctrl-alt p //当前pane向上移动
ctrl-alt n //当前pane向下移动
ctrl-alt f //当前pane向右移动
ctrl-alt b //当前pane向左移动
ctrl+x 空格 //切换pane布局

// window 操作
ctrl+x c   //新建window
ctrl+x n   //下一个window
ctrl+x p   //上一个window
ctrl+x w   //显示window列表

// session 操作
ctrl+x s   //显示session列表
ctrl+x d   //优雅退出tmux(保留session)
小结

tmux的应用还有很多,比如在运行服务的时候再也不需要$ nohup /root/test_srv &......
平时总是太忙了,也没什么时间写,今天总算是有时间写点什么,这个只是开头,以后有时间,我会把我这些年开发用到的一些东西分享给大家。
我始终觉得,后端就应该拥抱Linux,能纯键盘+命令行操作效率真的是太高了。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/29709.html

相关文章

  • Tmux使用手册

    摘要:关于我的博客掘金专栏路易斯专栏原文链接使用手册链接程序员使用手册极客头条全文共字,阅读需分钟。使用快捷键组合,三次按键就可以断开当前会话。新增面板中,使用最多的功能之一就是新增一个面板。 本文首发于CSDN网站,下面的版本又经过进一步的修订。 关于 我的博客:louis blog 掘金专栏:路易斯专栏 原文链接:Tmux使用手册 CSDN链接:程序员Tmux使用手册 - 极客头...

    Half 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<