0%

[Linux] tmux 工具

前言

tmux 是一個 Terminal Multiplexer, 它可以在一個 terminal 中開啟多個視窗、切割視窗區塊,讓我們在一個 terminal 中就能完成許多操作。它還可以保存 session 狀態,我們可以隨時離開(detach)這個連線 session, 也能夠隨時回到(attach)上次離開時的 session 狀態, 可以避免操作內容因為網路斷線而未儲存或是被中斷。而 tmux 會一直維持這個 session 直到被 delete 或是機器重新啟動。

安裝

On Ubuntu:

1
$ sudo apt-get install tmux

裝完之後可以執行 tmux 指令,就會看到 terminal 下方會有一條綠色的狀態列:

image

基本概念

接下來介紹一下 tmux 中的基本概念:

session

一個 terminal 中可以有多個 session, 每次使用 tmux 指令之後就會產生一個新的 session, 每個 session 是各自獨立的。

window

一個 session 中可以開啟多個 window (視窗),讓多個程式在不同視窗中執行。

pane

一個 window 中可以切割成多個 pane (區塊),方便在同一個視窗中直接觀察多個 pane 中的內容。

tmux2

基本操作

未開啟 tmux 之前的操作

  • 建立新的 session

    1
    $ tmux
  • 回到已存在的 session

    1
    $ tmux a [session name]

    若未輸入 session name, 則會連到最近使用的 session.

  • 查看目前的 session list

    1
    $ tmux ls

開啟 tmux 後的操作

開啟 tmux 之後, 大部分指令都需要先按 Ctrl + b 之後再按快捷鍵進行操作: (以下以 <prefix> 表示 Ctrl + b)

session

  • <prefix> + d: Detach current session
  • <prefix> + $: Rename session
  • Ctrl + f: Find the session name and switch to the specified session

window

  • <prefix> + c: Create new window
  • <prefix> + p: Change to previous window
  • <prefix> + n: Change to next window
  • <prefix> + 0 ~ 9: Change to the specified window
  • <prefix> + w: List all windows
  • <prefix> + ,: Rename the current window
  • <prefix> + .: Renumber the current window
  • <prefix> + &: Close the current window
  • <prefix> + f: Find the window name and switch to the specified window

pane

  • <prefix> + t: Clock mode
  • <prefix> + x: Close the current pane
  • <prefix> + %: Vertical split the window
  • <prefix> + “: Horizontal split the window
  • <prefix> + arrow keys: move between panes

另外也附上我比較常使用的 config 及 tmux theme: Github - myConfig

參考資料