tmux使用 ################ 安装:: brew install tmux sudo port install tmux sudo apt-get install tmux 基本命令:: tmux //启动 exit // 退出 PREFIX: Ctrl+b PREFIX ? //打印帮助信息 Session相关:: tmux new -s // tmux new-session -s tmux new -s -d // 后台创建一新session tmux ls // tmux list-sessions tmux attach -t // 连接指定session tmux kill-session -t PREFIX d //离开session PREFIX s // 列出session列表 windows相关:: tmux new -s -n PREFIX c // 在sesson中创建新的window PREFIX , //修改window名 PREFIX n //移动到下一个window下 PREFIX p //移动到前一个window下 PREFIX //移动到第个window下(取值从0-9) PREFIX f // 切换到指定window下 PREFIX w //把当前session下的window列表列出 PREFIX & // 同exit,有确认窗口 panel相关(挺好用):: PREFIX % //vertically split into two panels PREFIX " //horizontally split into two panels PREFIX o //在不同panel间 PREFIX q // 显示window下不同panel对应的数值 PREFIX x // 关闭panel PREFIX Space //切换layout [copy mode] => 基本命令:: PREFIX [ // 进入copy mode[一般是emacs模式] Ctrl_X Ctrl_C // 退出copy mode // copy里面内容 Ctrl_X @ //进入标记状态 Alt_W //拷贝里面内容,并退出copy mode sh> tmux show-buffer | less //查看刚才copy的内容 sh> tmux capture-pane && tmux save-buffer buffer.txt //capture the current buffer to buffer.txt // buffer是共享的 //你可以在一个tmux中copy后,再另一tmux中用 [copy mode] => 进入命令界面:: PREFIX : //进入命令界面 tmux> capture-pane //抓取当前页面 tmux> show messages // 展示消息列表 tmux> show buffer // tmux> ... [copy mode] => 在linux和它的clipboard一起工作:: sh> sudo apt-get install xclip // 修改.tmux.conf文件 sh> edit .tmux.conf // PREFIX CTRL-c 变为拷贝 bind C-c run "tmux save-buffer - | xclip -i -sel clipboard" // PREFIX CTRL-v 变为复制 bind C-v run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer" [copy mode] => 在mac和它的Clipboard一起工作:: $ git clone https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard.git $ cd tmux-MacOSX-pasteboard/ $ make reattach-to-user-namespace tmux配置:: /etc/tmux.conf ~/.tmux.conf //编辑文件tmux.conf: set -g prefix C-a //把prefix绑定为Ctrl+a unbind C-b //把Ctrl+b取消绑定 set -g base-index 1 //window的索引,默认从0开始 set -g pane-base-index 1 //panel的索引,默认从1开始 .. warning:: 修改tmux.conf文件后不会立即生效, 你需要关闭所有tmux窗口或键入 ``PREFIX :`` 并输入 ``source-file ~/.tmux.conf`` 常见问题 ======== 解决tmux启动「can't create socket」的问题:: >> strace -e trace=file tmux open("/lib64/tls/libpthread.so.0", O_RDONLY) = 3 access("/bin/bash", X_OK) = 0 access("/home/xxx/.tmux.conf", R_OK) = -1 ENOENT (No such file or directory) mkdir("/tmp//tmux-501", 0700) = 0 lstat("/tmp//tmux-501", {st_mode=S_IFDIR|0700, st_size=4096, ...}) = 0 can't create socket Process 3171 detached 「/tmp/tmux-501」这个目录出了问题,删掉这个目录,tmux就可以正常启动了 运行一段时间后tmux session 丢失 ------------------------------------ * 起因,一段时间不用再进入时发现 tmux ls 为空:: $ tmux ls no server running on /tmp/tmux-1004/default $ tmux attach no sessions $ ls -ld /tmp/tmux-$(id -u)/ drwx------ 2 zhaowg3 zhaowg3 4096 Jul 14 12:02 /tmp/tmux-1004/ * 原因:: tmux 的客户端通过 socket 文件连接到服务端; 如果 socket 文件不存在或损坏,客户端连接就会报:failed to connect to server 而 SIGUSR1 是 tmux 定义的信号之一,用于重建 socket 文件。 * 解决方案:: # 找到 tmux 进程 $ ps -aef | fgrep -i tmux hari 7139 1 1 2016 ? 2-20:32:31 tmux hari 25943 25113 0 22:00 pts/0 00:00:00 fgrep --color=auto -i tmux # 删除 tmux 进程 $ kill -s USR1 7139 $ tmux ls 0: 12 windows (created Mon Apr 18 21:17:55 2016) [198x62] * 参考 * https://stackoverflow.com/questions/9668763/why-am-i-getting-a-failed-to-connect-to-server-message-from-tmux-when-i-try-to * https://www.cnblogs.com/baiyunwanglai/p/15984105.html 屏幕比例不协调问题 ------------------ 有时候 tmux 突然中断之后,再次进入时,会发现屏幕被限制在了一个很小的范围内,其他的地方全部变成了点 “.”, 或者“烫” .. figure:: https://img.zhaoweiguo.com/uPic/2024/05/GUUCQX.png 原因:: 有其他client使用了tmux连接了这个session, 但他的窗口较小 定位:: # 查看有几个client在连接 $ tmux list-clients /dev/pts/0: syscmd [158x49 xterm] (utf8) /dev/pts/7: syscmd [230x64 xterm-256color] (utf8) 可以看到两个client的分辨率不同 解决:: 1. 手工退出那个client 2. 进入时使用 -d 参数(detach) tmux a -d -t [YOUR TMUX NAME] tmux attach -d -t [YOUR TMUX NAME] 3. 命令分离那个client tmux detach-client -t