從0開始的 Tmux 教學 (一)

Tmux Series Story 1

Lau Dai He
8 min readApr 22, 2019

Author:Lau Dai

Date:2019/08/23

筆者還在大學時期時,還是使用putty連線至本地虛擬機使用ubuntu

當時為了要多開terminal就還要多個putty的連線,真的很麻煩

在整理2014年SITCON筆記時,意外發現了這個好用的軟體

因此在linux上一直是我最愛的軟體,從2015年一直使用至今(2019)

//當初SITCON講者讓兩台火車同時經過的畫面到現在依然有印象XD

類似功能的軟體早期為screen,不過我沒使用過因此不多討論

以下正文開始

軟體介紹

Tmux 按照manual 而言 是一個terminal的多工器(?

tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen.  tmux may be detached from a screen and continue running in the background, then later reattached.

簡單來說,會使用tmux不外乎就幾個目的

  1. 想要將Terminal在不多重連線的情況下多開
  2. 想要將正在跑的程式、軟體放至背景
  3. 同上效果,在tmux中即使你的連線突然斷掉,在執行的程式依舊不會斷線
  4. 想要與多人分享你terminal中執行的畫面,或想多人同時對一個tmux session進行動作
  5. 若有想到其他目的歡迎幫我補充

簡單而言,當你啟用一個tmux服務時,其實你是啟用一個tmux session且開啟一個tmux window,而預設的shell 其實是可以設定的

When tmux is started it creates a new session with a single window and displays it on screen.  A status line at the bottom of the screen shows information on the current session and is used to enter interactive commands.

簡單來說 ,一個session 裡面有多個 的終端機

至於原理,建議有興趣的讀者可以去了解tty , pty 等差異

A session is a single collection of pseudo terminals under the management of tmux.  Each session has one or more windows linked to it.  A window occupies the entire screen and may be split into rectangular panes, each of which is a separate pseudo terminal (the pty(7) manual page documents the technical details of pseudo terminals).  Any number of tmux instances may connect to the same session, and any number of windows may be present in the same session.  Once all sessions are killed, tmux exits.

Tmux 主要是以C語言做撰寫,以下是兩個相關的專案

Tmux 是有許多好用的插件可以使用(使用tpm專案),後續有空會在分享一篇

啟用與離開

確認版本

$tmux -V

啟用Tmux

$tmux
$tmux new -s {Session name}
$tmux new -s hi
$tmux n # tmux new的縮寫
可以看到左下方的Session name 為 ”hi”

暫時離開

Ctrl + b 後 按下d

Ctrl +b 的動作稱之為 前綴動作(Prefix),可以理解為要觸發Tmux 快捷鍵之前的動作

※注意 大小寫在Tmux中是不同的字元

離開後重連

$tmux attach
$tmux a # tmux attach的縮寫

刪除/離開Tmux Session

在此Session中僅剩一個的Window中輸入$exit

暫時離開Tmux後,刪除指定的Session

列出Tmux 所有的Session

$tmux ls

刪除指定的 session

$tmux kill-session -t {Session name}
$tmux kill-session -t 0
此圖可以看出有一個session name 為”0” ,且只有一個window

使用觀念介紹

Tmux 分作三個概念 Session , Window , Pane

Session

建立三個在背景的session
顯示目前這台電腦上的session

可透過下列兩種方式顯示上圖

  1. Prefix + s
  2. Prefix +: 後打入choose-tree[Enter]

Window

在此Session中開啟三個windows,分別為vim , htop , python
選擇此Session中的window

可透過下列兩種方式顯示上圖

  1. Prefix + w
  2. Prefix + : 後打choose-window[Enter]

在2.5版中還可以看見預覽圖片

Pane

透過此圖可看到我們將此window分割成4個區塊(Pane)
Prefix + q 可看到每個區塊的編號,以及目前所在的編號(紅字)

三者的關係可以想作,一台機器上可以有多個Session ,每個Session中可以多個Window,而每個Window可以切割為多個Pane

因此只要給予指定的Session name ,是可以將其中的Window , Pane進行重新命名、搬移、刪除等動作

若是第一次使用無法了解,把它想成程式語言中物件與屬性的關係或許就比較好理解

以上為本次預計分享的內容,下列為篇數與預計分享的內容

預計章節

1. 介紹基本環境 本篇

2. 介紹安裝與基本操作 點我 2020/01/04發布

3. 如何編寫自己習慣與喜歡的tmux configuration

4. 套件與插件的教學

番外篇.與其他軟體整合應用

若有錯誤或者想交流的,歡迎在下方留言

以上,感謝收看

--

--

Lau Dai He

Less is more. 2021/08/01起,主要會紀錄各種技術的精華重點文章,方向可能是Pyhton, Linux, WIndows等都有可能。文章內容精簡為主,搭配少部份的個人情感抒發等。(inspired by 路人甲的世界​-知乎作者)