在tmux會話之間共享窗口(Windows & Panes)


去年寫過一篇 從Tmux 轉到GNU Screen,理由是我可以 在兩個顯示器上通過PuTTY連接到同一個GNU Screen會話,但兩個顯示器可以顯示不同的窗口(用GNU Screen的術語來說,是window和region),當時認為Tmux是做不到這一點的(如果兩個PuTTY窗口attach到同一個會話,一個執行切換窗口的操作,另外一個PuTTY上面也會跟着切換,不能工作在不同窗口上),

但后來發現,其實Tmux支持類似的功能,不過文檔中寫得有點不太淺顯明白.我也是碰到有文章講這個才發現有這個功能的.

tmux 會話間共享窗口

Tmux實現這個功能依靠的概念是grouped sessions,也就是將建立兩個會話(session)並將它們歸並到同一個組,同一個組內的多個會話擁有同樣的窗口(和窗口里面的pane),但各自有着不同的"當前窗口".創建組的方法是是在創建第二個session的時候用 -t target-session 將前一個session指定為新session的目標session:

new-session [-AdDP] [-c start-directory] [-F format] [-n window-name] [-s session-name]
             [-t target-session] [-x width] [-y height] [shell-command]
               (alias: new)
             Create a new session with name session-name.

             The new session is attached to the current terminal unless -d is given.
             window-name and shell-command are the name of and shell command to
             execute in the initial window.  If -d is used, -x and -y specify the
             size of the initial window (80 by 24 if not given).

             If run from a terminal, any termios(4) special characters are saved and
             used for new windows in the new session.

             The -A flag makes new-session behave like attach-session if session-
             name already exists; in the case, -D behaves like -d to attach-session.

             If -t is given, the new session is grouped with target-session.  This 
             means they share the same set of windows - all windows from target-
             session are linked to the new session and any subsequent new windows or
             windows being closed are applied to both sessions.  The current and
             previous window and any session options remain independent and either
             session may be killed without affecting the other.  Giving -n or shell-
             command are invalid if -t is used.

             The -P option prints information about the new session after it has
             been created.  By default, it uses the format ‘#{session_name}:’ but a 
             different format may be specified with -F.`

然后用 tmux list-sessions 就可以看到有兩個session.

bamanzi@raspberrypi:~ $ tmux ls
1: 2 windows (created Tue Jun 27 13:51:20 2017) [204x52] (group 0)
3: 2 windows (created Tue Jun 27 13:59:19 2017) [204x52] (group 0) (attached)

可以看到這里兩個會話都有 (group 0) 這個標識.

兩個顯示器(或者兩個人)用 tmux attach -t <id> 分別attached到這兩個不同的會話的話,兩邊可以各自查看不同的窗口.

解決窗口尺寸的問題

不過相對GNU Screen而言,這里還是有個不爽的地方,兩個tmux會話之間窗口(window)大小會相互影響:比如一個PuTTY是132x55的,另一個是140x50的,當只有一個session連上來的時候,tmux會將窗口尺寸調整到跟當前PuTTY一致,但組內另外一個窗口連上來的時候,window的大小跟會跟兩個屏幕的公共部分一樣大了(變成132x50)--這跟同一個tmux session的情況是一樣的.

而GNU Screen的設計不太一樣,屏幕尺寸是不相互影響的,連region分隔也不相互影響.我們可以在一個PuTTY窗口上顯示窗口3和5(GNU Screen的窗口比較類似於Tmux的pane),在另一個PuTTY窗口上顯示窗口1, 2和4.窗口大小完全相互不影響--不過它有個煩人的地方,將一個窗口放到當前region來現實的時候,要用C-a F (fit) 調整一下該程序的終端大小,使其跟當前region大小一致.

tmux 提供了一個選項來解決(部分解決)這個問題:

aggressive-resize [on | off]

         Aggressively resize the chosen window.  This means that tmux will resize the window to
         the size of the smallest session for which it is the current window, rather than the
         smallest session to which it is attached.  The window may resize when the current
         window is changed on another sessions; this option is good for full-screen programs
         which sup‐ port SIGWINCH and poor for interactive programs such as shells.

簡單翻譯一下:當打開這個選項之后,如果兩個session顯示的當前窗口不是同一個,那么兩個session各自根據自己的大小來調整其當前窗口的尺寸(簡單理解就是,各自最大化);如果兩個session顯示同一個窗口,那么還按之前的規則來(縮小到兩邊的公共面積內).

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM