「Tmux」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(→‎キーバインド: 空白の追加)
(文字列「Tips and tricks」を「ヒントとテクニック」に置換)
(他の1人の利用者による、間の8版が非表示)
1行目: 1行目:
  +
{{lowercase title}}
 
[[Category:ターミナルエミュレータ]]
 
[[Category:ターミナルエミュレータ]]
 
[[en:Tmux]]
 
[[en:Tmux]]
4行目: 5行目:
 
[[ru:Tmux]]
 
[[ru:Tmux]]
 
[[tr:Tmux]]
 
[[tr:Tmux]]
[[zh-CN:Tmux]]
+
[[zh-hans:Tmux]]
 
{{Related articles start}}
 
{{Related articles start}}
  +
{{Related|dtach}}
 
{{Related|GNU Screen}}
 
{{Related|GNU Screen}}
 
{{Related articles end}}
 
{{Related articles end}}
11行目: 13行目:
 
[http://tmux.github.io/ Tmux] はターミナルマルチプレクサです: 一つのスクリーンから別々のプログラムが動作する多数のターミナル (またはウィンドウ) を作成・アクセス・制御することができます。tmux はスクリーンから分離させてバックグラウンドで動作させることができ、後でまた繋ぎ直すことができます。
 
[http://tmux.github.io/ Tmux] はターミナルマルチプレクサです: 一つのスクリーンから別々のプログラムが動作する多数のターミナル (またはウィンドウ) を作成・アクセス・制御することができます。tmux はスクリーンから分離させてバックグラウンドで動作させることができ、後でまた繋ぎ直すことができます。
   
Tmux は [[GNU Screen]] の代わりになるものとして BSD ライセンスで配布されています。似ているところもありますが、プログラムには様々な違いが存在します。[https://raw.githubusercontent.com/tmux/tmux/master/FAQ tmux の FAQ ページ] を見て下さい。
+
Tmux は [[GNU Screen]] の代わりになるものとして ISC ライセンスで配布されています。似ているところもありますが、プログラムには様々な違いが存在します。[https://raw.githubusercontent.com/tmux/tmux/master/FAQ tmux の FAQ ページ] を見て下さい。
   
 
== インストール ==
 
== インストール ==
[[公式リポジトリ]]にある {{Pkg|tmux}} パッケージを[[インストール]]してください。
+
[[公式リポジトリ]]にある {{Pkg|tmux}} パッケージを[[インストール]]してください。任意で {{Aur|tmux-bash-completion}} をインストールすることで tmux の bash 補完を使うことができます
   
 
== 設定 ==
 
== 設定 ==
ユーザーごとの設定ファイルは {{ic|~/.tmux.conf}} に、グローバルな設定ファイルは {{ic|/etc/tmux.conf}} に配置されます。デフォルトの設定ファイルは {{Ic|/usr/share/tmux/}} にあります。
+
ユーザーごとの設定ファイルは {{ic|~/.tmux.conf}} に、グローバルな設定ファイルは {{ic|/etc/tmux.conf}} に配置されます。
   
 
=== キーバインド ===
 
=== キーバインド ===
35行目: 37行目:
 
}}
 
}}
   
{{Tip|Quote special characters to use them as prefix. You may also use {{ic|Alt}} (called Meta) instead of {{ic|Ctrl}}. For example: {{ic|set -g prefix m-'\'}}}}
+
{{Tip|特殊文字はクォートで囲ってプレフィックスとして使ってください。{{ic|Ctrl}} の代わりに {{ic|Alt}} (Meta) を使うこともできます。例: {{ic|set -g prefix m-'\'}}}}
   
 
さらに、ウィンドウ間の移動をする方法には以下のものもあります:
 
さらに、ウィンドウ間の移動をする方法には以下のものもあります:
51行目: 53行目:
 
==== コピーモード ====
 
==== コピーモード ====
   
  +
tmux ウィンドウには複数のモードが存在します。デフォルトのモードではウィンドウにアタッチされたターミナルに直接アクセスすることができ、もうひとつのモードはコピーモードです。コピーモードではバッファを移動することができ履歴をスクロールすることが可能です。vi または emacs スタイルのキーバインドが使えます。VISUAL や EDITOR に ‘vi’ が含まれていない場合、デフォルトでは emacs のキーバインドが使われます。
A tmux window may be in one of several modes. The default permits direct access to the terminal attached to the window; the other is copy mode. Once in copy mode you can navigate the buffer including scrolling the history. Use vi or emacs-style key bindings in copy mode. The default is emacs, unless VISUAL or EDITOR contains ‘vi’.
 
   
  +
コピーモードに入るには以下を実行:
To enter copy mode do the following:
 
 
 
 
Ctrl-b [
 
Ctrl-b [
   
  +
デフォルトエディタと同じようにバッファを操作することができます。
You can navigate the buffer as you would in your default editor.
 
   
  +
コピーモードを終了するには、以下のキーバインドのどちらかを使います:
To quit copy mode, use one of the following keybindings:
 
   
 
vi モード:
 
vi モード:
69行目: 71行目:
 
tmux の中で URL をブラウズするには {{AUR|urlview}} をインストールして設定する必要があります。
 
tmux の中で URL をブラウズするには {{AUR|urlview}} をインストールして設定する必要があります。
   
  +
新しいターミナルの中で:
Inside a new terminal:
 
 
bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; run-shell "$TERMINAL -e urlview /tmp/tmux-buffer"
 
bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; run-shell "$TERMINAL -e urlview /tmp/tmux-buffer"
   
  +
新しい tmux ウィンドウの中で (新しいターミナルは不要です):
Or inside a new tmux window (no new terminal needed):
 
 
bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; new-window -n "urlview" '$SHELL -c "urlview < /tmp/tmux-buffer"'
 
bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; new-window -n "urlview" '$SHELL -c "urlview < /tmp/tmux-buffer"'
   
 
=== 適切なターミナルの設定 ===
 
=== 適切なターミナルの設定 ===
If you are using a 256 colour terminal, you will need to set the correct term in tmux. You can do this in {{ic|tmux.conf}}:
 
   
  +
==== 256色 ====
set -g default-terminal "screen-256color"
 
  +
256色のターミナルを使っている場合、tmux で適切なターミナルを設定する必要があります。[https://raw.githubusercontent.com/tmux/tmux/master/CHANGES tmux 2.1] から、''tmux'' または ''tmux-256color'' が適切な設定値です。{{ic|tmux.conf}} で以下のように設定できます:
   
  +
set -g default-terminal "tmux-256color"
If you enable xterm-keys in your {{ic|tmux.conf}}, then you need to build a custom terminfo to declare the new escape codes or applications will not know about them. Compile the following with {{ic|tic}} and you can use "xterm-screen-256color" as your TERM:
 
  +
  +
もしくは ''screen'' や ''screen-256color'' などに設定する場合もあります:
  +
  +
set -g default-terminal "screen-256color"
  +
  +
また、{{ic|.bashrc}} に以下を追加することで強制的に tmux から256色をサポートしているターミナルを認識させることができます:
  +
  +
alias tmux="tmux -2"
  +
  +
==== 24ビットカラー ====
  +
Tmux はバージョン 2.2 から24ビットカラーをサポートしています ([https://github.com/tmux/tmux/commit/427b8204268af5548d09b830e101c59daa095df9])。使用しているターミナルが24ビットカラーをサポートしている場合 ([https://gist.github.com/XVilka/8346728 gist] を参照)、ターミナルを {{ic|terminal-overrides}} 設定に追加してください。例えば、[[Termite]] を使っているのであれば以下のように追加します:
  +
  +
set -ga terminal-overrides ",xterm-termite:Tc"
  +
  +
他のターミナルの場合、{{ic|xterm-termite}} を適切なターミナルのタイプに置き換えてください ({{ic|$TERM}} に保存されています)。{{ic|Tc}} の terminfo 拡張について詳しくは tmux(1) の man ページを見てください。
  +
  +
==== xterm-keys ====
  +
{{ic|tmux.conf}} で xterm-keys を有効にするには、以下の行を追加してください:
  +
  +
set-option -g xterm-keys on
  +
  +
{{ic|tmux.conf}} で xterm-keys を有効にしたら、新しいエスケープコードがアプリケーションから認識されるようにカスタム terminfo を作成して宣言する必要があります。{{ic|tic}} で以下のコードをコンパイルして TERM に "xterm-screen-256color" を指定してください:
   
 
# A screen- based TERMINFO that declares the escape sequences
 
# A screen- based TERMINFO that declares the escape sequences
100行目: 124行目:
   
 
=== 他の設定 ===
 
=== 他の設定 ===
スクロールバックを10000行に設定:
+
スクロールバックバッファを10000行に設定:
 
set -g history-limit 10000
 
set -g history-limit 10000
  +
  +
ターミナルエミュレータの設定を上書き:
  +
set -ga terminal-overrides ',xterm*:smcup@:rmcup@'
  +
set -ga terminal-override ',rxvt-uni*:XT:Ms=\E]52;%p1%s;%p2%s\007'
  +
  +
マウスを切り替える:
  +
bind-key m set-option -g mouse on \; display 'Mouse: ON'
  +
bind-key M set-option -g mouse off \; display 'Mouse: OFF'
   
 
=== systemd で自動起動 ===
 
=== systemd で自動起動 ===
   
  +
起動時に tmux サーバーを立ち上げることには複数の利点が存在します。特に、新しい tmux セッションを起動する場合、既にサービスが動作していれば起動時間が短縮されます。
There are some notable advantages to starting a tmux server at startup.
 
Notably, when you start a new tmux session, having the service already running reduces any delays in the startup.
 
   
  +
さらに、tmux セッションのカスタマイズは維持され、たとえログインしなくても tmux セッションを永続的にすることができます (tmux の設定をスクリプトで管理している場合やユーザーの tmux セッションを共有する場合などに有用です)。
Furthermore, any customization attached to your tmux session will be retained and your tmux session can be made to persist even if you have never logged in, if you have some reason to do that (like a heavily scripted tmux configuration or shared user tmux sessions).
 
   
The service below starts ''tmux'' for the specified user (i.e. start with {{ic|tmux@''username''.service}}):
+
以下のサービスは特定のユーザーで ''tmux'' を起動します ({{ic|tmux@''username''.service}} で起動と同じ):
   
 
{{hc|/etc/systemd/system/tmux@.service|<nowiki>
 
{{hc|/etc/systemd/system/tmux@.service|<nowiki>
126行目: 157行目:
 
</nowiki>}}
 
</nowiki>}}
   
{{Tip|You may want to add {{ic|1=WorkingDirectory=''custom_path''}} to customize working directory.}}
+
{{Tip|{{ic|1=WorkingDirectory=''custom_path''}} を追加することで作業ディレクトリをカスタマイズすることも可能です。}}
   
または、({{ic|1=User=%I}} を使わずに) 上記のファイルを [[systemd/ユーザー]]ディレクトリに配置することもできます、例: {{ic|~/.config/systemd/user/tmux.service}}。この方法だとログイン時に tmux サービスが起動します。
+
または、({{ic|1=User=%I}} を使わずに) 上記のファイルを [[systemd/ユーザー]]ディレクトリに配置することもできます、例: {{ic|~/.config/systemd/user/tmux.service}}。この方法だと[[systemd/ユーザー#systemd のユーザーインスタンスを自動起動|ユーザーインスタンスの自動起動]]を有効にしていない場合、ログイン時に tmux サービスが起動します。
   
 
== セッションの開始 ==
 
== セッションの開始 ==
  +
{{ic|~/.tmux.conf}} に以下のように記述することでウィンドウがプリロードされた状態で tmux でセッションを開くことができます:
You can have tmux open a session with preloaded windows by including those details in your {{ic|~/.tmux.conf}}:
 
   
 
new -n WindowName Command
 
new -n WindowName Command
137行目: 168行目:
 
neww -n WindowName Command
 
neww -n WindowName Command
   
  +
ウィンドウが分割された状態でセッションを起動するには (マルチペイン)、分割したい neww の下に splitw コマンドを追加してください:
To start a session with split windows (multiple panes), include the splitw command below the neww you would like to split; thus:
 
   
 
new -s SessionName -n WindowName Command
 
new -s SessionName -n WindowName Command
145行目: 176行目:
 
selectp -t 0
 
selectp -t 0
   
  +
上記の場合、ウィンドウが2つ開いて、2番目のウィンドウは foo/bar という名前で縦に半分に分割されます (50%)。bar の上で foo が実行されます。フォーカスはウィンドウ 2 (foo/bar) のトップペイン (foo) になります。
would open 2 windows, the second of which would be named foo/bar and would be split vertically in half (50%) with foo running above bar. Focus would be in window 2 (foo/bar), top pane (foo).
 
   
  +
{{Note|{{ic|.conf}} で base-index を 1 に指定しないかぎり、セッション・ウィンドウ・ペインの番号は 0 から割り振られます。}}
{{Note|Numbering for sessions, windows and panes starts at zero, unless you have specified a base-index of 1 in your {{ic|.conf}} }}
 
   
  +
複数のセッションを管理したい場合、設定ファイルから別のセッションファイルを読み込んでください:
To manage multiple sessions, source separate session files from your conf file:
 
   
 
# initialize sessions
 
# initialize sessions
158行目: 189行目:
   
 
=== スクロールの問題 ===
 
=== スクロールの問題 ===
  +
ターミナルで Shift-Page Up/Down を使ってスクロールができない場合、以下の設定で {{ic|xterm}} から識別子が始まるターミナルで smcup と rmcup の機能を削除できます:
If you have issues scrolling with Shift-Page Up/Down in your terminal, the following will remove the smcup and rmcup capabilities for any term that reports itself as anything beginning with {{ic|xterm}}:
 
   
 
set -ga terminal-overrides ',xterm*:smcup@:rmcup@'
 
set -ga terminal-overrides ',xterm*:smcup@:rmcup@'
   
  +
上記の設定を使うとターミナルエミュレータは tmux のことを [[pico]] や [[mutt]] と同じようなフルスクリーンのアプリケーションと認識するようになり [http://superuser.com/questions/310251/use-terminal-scrollbar-with-tmux]、スクロールバックが正しく記録されるようになります。ただし、ウィンドウやペインを切り替えたときに多少表示が崩れるかもしれません。Tmux のネイティブのスクロールバックを使うことを推奨します。
This tricks the terminal emulator into thinking Tmux is a full screen application like pico or mutt[http://superuser.com/questions/310251/use-terminal-scrollbar-with-tmux], which will make the scrollback be recorded properly. Beware however, it will get a bit messed up when switching between windows/panes. Consider using Tmux's native scrollback instead.
 
   
 
=== マウスのスクロール ===
 
=== マウスのスクロール ===
   
  +
{{Note|以下の設定はセレクションバッファのコピー・ペーストと干渉します。セレクションバッファからコピー・ペーストするときは shift キーを押してください。}}
{{Note|This interferes with selection buffer copying and pasting. To copy/paste to/from the selection buffer hold the shift key.}}
 
   
 
マウスホイールでスクロールしたい場合、{{ic|.tmux.conf}} で mode-mouse をオンにして下さい:
 
マウスホイールでスクロールしたい場合、{{ic|.tmux.conf}} で mode-mouse をオンにして下さい:
174行目: 205行目:
 
set -g history-limit 30000
 
set -g history-limit 30000
   
  +
tmux 2.1 からは、マウスホイールでスクロールするには {{ic|~/.tmux.conf}} に以下のどちらかあるいは両方を追加してみてください:
=== Fix reverse-video/italic mode in urxvt ===
 
  +
bind -T root WheelUpPane if-shell -F -t = "#{alternate_on}" "send-keys -M" "select-pane -t =; copy-mode -e; send-keys -M"
If your reverse-video and italic modes are reversed, you may follow these instructions. This happens for example in vim when italics are replaced by highlighting, or in less when the search highlighting is replaced by italics. This is because the screen terminfo doesn't define italics, and the italics escape of urxvt happens to be the standout escape defined in the terminfo. In this solution, you may replace {{ic|1=screen_terminfo="screen"}} by {{ic|1=screen_terminfo="screen-256color"}}.
 
  +
bind -T root WheelDownPane if-shell -F -t = "#{alternate_on}" "send-keys -M" "select-pane -t =; send-keys -M"
   
  +
上記の設定では一行ずつしかスクロールしませんが、以下を追加することでページ単位でスクロールすることができます:
mkdir $HOME/.terminfo/
 
  +
bind -t vi-copy WheelUpPane page-up
screen_terminfo="screen"
 
  +
bind -t vi-copy WheelDownPane page-down
  +
bind -t emacs-copy WheelUpPane page-up
  +
bind -t emacs-copy WheelDownPane page-down
   
  +
=== UTF-8 のマウスイベントをターミナルエミュレータがサポートしていない ===
Create a new terminfo adding the italic escape code:
 
   
  +
ターミナルエミュレータが UTF-8 のマウスイベントに対応していない場合、tmux の {{ic|mouse on}} オプションを設定しても、ターミナルウィンドウの中で左クリックすると {{ic|[M#}} や {{ic|[Ma}} などの文字列がプロンプトに貼り付けられてしまいます。
infocmp "$screen_terminfo" | sed \
 
-e 's/^screen[^|]*|[^,]*,/screen-it|screen with italics support,/' \
 
-e 's/%?%p1%t;3%/%?%p1%t;7%/' \
 
-e 's/smso=[^,]*,/smso=\\E[7m,/' \
 
-e 's/rmso=[^,]*,/rmso=\\E[27m,/' \
 
-e '$s/$/ sitm=\\E[3m, ritm=\\E[23m,/' > /tmp/screen.terminfo
 
   
  +
この問題を解決するには以下を設定:
Compile this terminfo:
 
 
tic /tmp/screen.terminfo
 
   
  +
set -g mouse-utf8 off
Then, you must add the following line in your tmux.conf. If you already defined {{ic|1=default-terminal}}, just replace it.
 
 
set -g default-terminal "screen-it"
 
 
The source of this solution can be found at [http://tmux.svn.sourceforge.net/viewvc/tmux/trunk/FAQ], in the section entitled "vim displays reverse video instead of italics, while less displays italics (or just regular text) instead of reverse. What's wrong?".
 
   
 
=== Shift+F6 が Midnight Commander で動作しない ===
 
=== Shift+F6 が Midnight Commander で動作しない ===
   
  +
[[Midnight Commander#ショートカットが機能しない]]を参照してください。
If the {{ic|Shift+F6}} key combination is not working with either {{ic|1=TERM=screen}} or {{ic|1=TERM=screen-256color}}, then from inside tmux, run this command:
 
infocmp > screen (or screen-256color)
 
 
Open the file in a text editor, and add the following to the bottom of that file:
 
kf16=\E[29~,
 
 
Then compile the file with {{ic|tic}}. The keys should be working now.
 
   
 
== X クリップボードの統合 ==
 
== X クリップボードの統合 ==
216行目: 234行目:
   
 
# Emacs style
 
# Emacs style
bind-key -t emacs-copy M-w copy-pipe "xsel -i -p -b"
+
bind-key -T copy-mode y send-key -X copy-pipe-and-cancel "xsel -i -p && xsel -o -p | xsel -i -b"
 
bind-key C-y run "xsel -o | tmux load-buffer - ; tmux paste-buffer"
 
bind-key C-y run "xsel -o | tmux load-buffer - ; tmux paste-buffer"
   
 
# Vim style
 
# Vim style
bind-key -t vi-copy y copy-pipe "xsel -i -p -b"
+
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xsel -i -p && xsel -o -p | xsel -i -b"
 
bind-key p run "xsel -o | tmux load-buffer - ; tmux paste-buffer"
 
bind-key p run "xsel -o | tmux load-buffer - ; tmux paste-buffer"
   
  +
{{pkg|xclip}} を使うことも可能です。xsel と違って現在のロケールに適合しない生のビットストリームの出力も上手く処理されます。ただし、tmux のバッファを読み込んだ後に xclip は標準出力を閉じないため、{{ic|xclip}} ではなく {{ic|xsel}} を使う方が簡単です。xclip では tmux からコピー作業の完了を知ることができないため、xclip が終了するまで待機することになり、tmux が反応しなくなってしまいます。以下のように {{ic|xclip}} の {{ic|STDOUT}} を {{ic|/dev/null}} にリダイレクトすれば解決します:
{{pkg|xclip}} could also be used for that purpose, unlike xsel it works better on printing raw bitstream that doesn't fit the current locale. Nevertheless, it is neater to use <code>xsel</code> instead of <code>xclip</code>, because xclip does not close STDOUT after it has read from tmux's buffer. As such, tmux doesn't know that the copy task has completed, and continues to wait for xclip's termination, thereby rendering tmux unresponsive. A workaround is to redirect <code>STDOUT</code> of <code>xclip</code> to <code>/dev/null</code>, like in the following:
 
   
 
# Vim style
 
# Vim style
bind-key -t vi-copy y copy-pipe "xclip -i -sel clip > /dev/null"
+
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -i -sel clip > /dev/null"
bind-key p run "xclip -o -sel clip | tmux load-buffer -"
+
bind-key p run "xclip -o -sel clip | tmux load-buffer - ; tmux paste-buffer"
   
 
=== Urxvt の中クリック ===
 
=== Urxvt の中クリック ===
   
  +
{{Note|以下の設定を使うにはマウスのサポートを有効にする必要があります。}}
{{Note|To use this, you need to enable mouse support}}
 
   
 
中マウスのクリックによって tmux で urxvt のコピーアンドペーストを有効にする非公式の perl 拡張が存在します (公式の [http://sourceforge.net/p/tmux/tmux-code/ci/master/tree/FAQ FAQ] に記載があります)。
 
中マウスのクリックによって tmux で urxvt のコピーアンドペーストを有効にする非公式の perl 拡張が存在します (公式の [http://sourceforge.net/p/tmux/tmux-code/ci/master/tree/FAQ FAQ] に記載があります)。
249行目: 267行目:
 
}}
 
}}
   
  +
次に、tmux に新しい関数を指定してマウスのサポートを有効にします:
Next, you want to tell tmux about the new function and enable mouse support (if you haven't already):
 
   
 
{{hc|~/.tmux.conf|
 
{{hc|~/.tmux.conf|
258行目: 276行目:
 
}}
 
}}
   
  +
新しい中クリック機能を試すには全ての tmux インスタンスを終了してください。
That's it. Be sure to end all instances of tmux before trying the new MiddleClick functionality.
 
   
  +
tmux の中では Shift+中マウスクリックを使うことでクリップボードのセレクションが貼り付けられます。中マウスのクリックだけの場合、tmux のバッファが貼り付けられます。
While in tmux, Shift+MiddleMouseClick will paste the clipboard selection while just MiddleMouseClick will paste your tmux buffer.
 
Outside of tmux, just use MiddleMouseClick to paste your tmux buffer and your standard Ctrl-c to copy.
 
   
  +
== ヒントとテクニック ==
== Tips and tricks ==
 
 
=== デフォルトのセッションレイアウトで tmux を起動 ===
 
=== デフォルトのセッションレイアウトで tmux を起動 ===
  +
tmuxinator や [[tmuxp]] などのセッションマネージャを使うことで共通のセッション設定を簡単に管理できます。
Tmux のデフォルトセションレイアウトを設定するには、[[AUR]] から {{AUR|tmuxinator}} をインストールします。次のコマンドでインストールをテストできます:
 
  +
  +
tmuxinator の場合、[[AUR]] から {{AUR|tmuxinator}} をインストールします。次のコマンドでインストールをテストできます:
   
 
tmuxinator doctor
 
tmuxinator doctor
   
  +
==== デフォルトのレイアウト値を取得 ====
==== Get the default layout values ====
 
  +
Tmux を通常通りに起動してウィンドウとペインのレイアウトを好きなように設定してください。設定が完了したら、以下のコマンドを実行することで現在のレイアウト値を取得できます (Tmux セッションの中から実行してください):
Start Tmux as usual and configure your windows and panes layout as you like. When finished, get the current layout values by executing (while you are still within the current Tmux session)
 
   
 
tmux list-windows
 
tmux list-windows
   
  +
3つのペインと2つのペインの2つのウィンドウからなるレイアウトの場合、出力は以下のようになります:
The output may look like this (two windows with 3 panes and 2 panes layout)
 
   
 
0: default* (3 panes) [274x83] [layout 20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]}] @2 (active)
 
0: default* (3 panes) [274x83] [layout 20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]}] @2 (active)
 
1: remote- (2 panes) [274x83] [layout e3d3,274x83,0,0[274x41,0,0,4,274x41,0,42,7]] @3
 
1: remote- (2 panes) [274x83] [layout e3d3,274x83,0,0[274x41,0,0,4,274x41,0,42,7]] @3
   
The Interesting part you need to copy for later use begins after '''[layout...''' and excludes '''... ] @2 (active)'''. For the first window layout you need to copy e.g. '''20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]}'''
+
コピーする必要があるのは '''[layout...''' の後から '''... ] @2 (active)''' を除いた部分です。1番目のウィンドウレイアウトであれば '''20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]}''' をコピーします。
   
 
==== デフォルトの tmux レイアウトを定義する ====
 
==== デフォルトの tmux レイアウトを定義する ====
   
  +
レイアウト値がわかったら現在の tmux セッションを終了してください。その後、Tmuxinator の設定ファイルを編集して Tmux のデフォルトセッションレイアウトを作成します (以下の例をそのままコピーするのではなく、上のセクションで確認したレイアウト値を使ってください):
Knowing this, you can exit the current tmux session. Following this, you create your default Tmux session layout by editing Tmuxinator's config file (Don't copy the example, get your layout values as described above)
 
   
 
{{hc|~/.tmuxinator/default.yml|<nowiki>
 
{{hc|~/.tmuxinator/default.yml|<nowiki>
302行目: 321行目:
 
</nowiki>}}
 
</nowiki>}}
   
  +
例では2つのウィンドウを "default" と "remote" として定義しています。適当なレイアウト値を決めてください。各ペインに対して最低でも1行は {{ic|-}} を使う必要があります。1番目のウィンドウペインでは、1番目のペインでコマンドライン "clear" を、2番目のペインで "vim" を、3番目のペインで "clear && emacs -nw" を実行します。2番目のウィンドウレイアウトには何も起動コマンドを定義していません。
The example defines two windows named "default" and "remote". With your determined layout values. For each pane you have to use at least one {{ic|-}} line. Within the first window panes you start the commandline "clear" in pane one, "vim" in pane two and "clear && emacs -nw" executes two commands in pane three on each Tmux start. The second window layout has two panes without defining any start commmands.
 
   
  +
新しいデフォルトレイアウトをテストするには:
Test the new default layout with (yes, it is "mux"):
 
   
 
mux default
 
mux default
310行目: 329行目:
 
==== デフォルトの tmux レイアウトで tmux を自動起動 ====
 
==== デフォルトの tmux レイアウトで tmux を自動起動 ====
   
  +
デフォルトの Tmux セッションレイアウトでターミナルセッションを起動したい場合、以下を編集してください:
If you like to start your terminal session with your default Tmux session layout edit
 
 
{{hc|~/.bashrc|<nowiki>
 
{{hc|~/.bashrc|<nowiki>
 
if [ -z "$TMUX" ]; then
 
if [ -z "$TMUX" ]; then
318行目: 337行目:
   
 
=== urxvt で tmux を起動 ===
 
=== urxvt で tmux を起動 ===
  +
以下のコマンドで urxvt を起動することで tmux セッションも起動します。{{ic|.ratpoisonrc}} ファイルの exec コマンドなどから使うことができます。
Use this command to start urxvt with a started tmux session. I use this with the exec command from my .ratpoisonrc file.
 
 
{{bc|<nowiki>urxvt -e bash -c "tmux -q has-session && exec tmux attach-session -d || exec tmux new-session -n$USER -s$USER@$HOSTNAME"</nowiki>}}
 
{{bc|<nowiki>urxvt -e bash -c "tmux -q has-session && exec tmux attach-session -d || exec tmux new-session -n$USER -s$USER@$HOSTNAME"</nowiki>}}
   
 
=== シェルのログイン時に tmux を起動 ===
 
=== シェルのログイン時に tmux を起動 ===
   
  +
以下の bash コードを {{ic|.bashrc}} のエイリアスの前に追加してください。他のシェルのコードも同じようになります:
Simply add the following line of bash code to your .bashrc before your aliases; the code for other shells is very similar:
 
   
 
{{hc|~/.bashrc|<nowiki>
 
{{hc|~/.bashrc|<nowiki>
333行目: 352行目:
 
{{note|This snippet ensures that tmux is not launched inside of itself (something tmux usually already checks for anyway). tmux sets $TMUX to the socket it is using whenever it runs, so if $TMUX isn't set or is length 0, we know we aren't already running tmux.}}
 
{{note|This snippet ensures that tmux is not launched inside of itself (something tmux usually already checks for anyway). tmux sets $TMUX to the socket it is using whenever it runs, so if $TMUX isn't set or is length 0, we know we aren't already running tmux.}}
   
  +
ログイン時にまずアタッチできないか試してみて tmux が実行されていない場合にのみセッションを作成したい場合、以下のスニペットを追加します:
Add the following snippet to start only one session(unless you start some manually), on login, try attach at first, only create a session if no tmux is running.
 
 
 
 
{{bc|<nowiki>
 
{{bc|<nowiki>
343行目: 362行目:
 
</nowiki>}}
 
</nowiki>}}
   
  +
以下のスニペットは起動する前に tmux がインストールされているかどうかチェックします。また、ログアウト時に既存の tmux セッションに再アタッチすることで、同一のターミナルからすばやく全ての tmux セッションをシャットダウンできるようにしています。.
The following snippet does the same thing, but also checks tmux is installed before trying to launch it. It also tries to reattach you to an existing tmux session at logout, so that you can shut down every tmux session quickly from the same terminal at logout.
 
 
{{bc|<nowiki>
 
{{bc|<nowiki>
 
# TMUX
 
# TMUX
357行目: 376行目:
 
</nowiki>}}
 
</nowiki>}}
   
  +
既存のデタッチされたセッションにアタッチしたり新しいセッションを起動する場合:
Another possibility is to try to attach to existing deattached session or start a new session:
 
   
 
{{bc|<nowiki>
 
{{bc|<nowiki>
369行目: 388行目:
 
fi
 
fi
 
</nowiki>}}
 
</nowiki>}}
 
{{note|Instead of using the bashrc file, you can launch tmux when you start your terminal emulator. (i. e. urxvt -e tmux)}}
 
   
 
=== 非ログインシェルを起動する ===
 
=== 非ログインシェルを起動する ===
384行目: 401行目:
 
=== tmux のウィンドウをタブのように使う ===
 
=== tmux のウィンドウをタブのように使う ===
   
  +
以下の設定を {{ic|~/.tmux.conf}} に追加することで tmux のウィンドウをタブのように使うことができます ([[rxvt-unicode/ヒントとテクニック#urxvtq とタブ|urxvt のタブ拡張]]を参照)。仮想タブはターミナルエミュレータから独立しているという利点があります。
The following settings added to {{ic|~/.tmux.conf}} allow to use tmux windows like tabs, such as those provided by the reference of these hotkeys — [[rxvt-unicode#urxvtq_with_tabbing|urxvt's tabbing extensions]]. An advantage thereof is that these virtual “tabs” are independent of the terminal emulator.
 
   
 
#urxvt tab like window switching (-n: no prior escape seq)
 
#urxvt tab like window switching (-n: no prior escape seq)
393行目: 410行目:
 
bind -n C-right swap-window -t +1
 
bind -n C-right swap-window -t +1
   
  +
もちろん、ターミナルなど他のアプリケーションのホットキーと重複させてはいけません。そのような場合、ターミナルのタブ機能を無効化して置き換えるほうが良いでしょう。
Of course, those should not overlap with other applications' hotkeys, such as the terminal's. Given that they substitute terminal tabbing that might as well be deactivated, though.
 
   
 
It can also come handy to supplement the EOT hotkey {{ic|Ctrl+d}} with one for tmux's detach:
 
It can also come handy to supplement the EOT hotkey {{ic|Ctrl+d}} with one for tmux's detach:
399行目: 416行目:
 
bind-key -n C-j detach
 
bind-key -n C-j detach
   
  +
=== 様々なウィンドウのセッションを同時に操作するクライアント ===
=== Clients simultaneously interacting with various windows of a session ===
 
   
In [http://mutelight.org/articles/practical-tmux Practical Tmux], Brandur Leach writes:
+
[http://mutelight.org/articles/practical-tmux Practical Tmux] で、Brandur Leach は以下のように書いています:
   
  +
: 複数のクライアントを一つのセッションにアタッチする際、Screen と tmux の挙動は少々異なっています。Screen では、各クライアントがセッションに接続され、セッションの中で別々のウィンドウが表示されます。tmux では、一つのセッションに接続された全てのクライアントが同じウィンドウを表示します。
: Screen and tmux's behaviour for when multiple clients are attached to one session differs slightly. In Screen, each client can be connected to the session but view different windows within it, but in tmux, all clients connected to one session must view the same window.
 
 
: This problem can be solved in tmux by spawning two separate sessions and synchronizing the second one to the windows of the first, then pointing a second new session to the first.
 
: This problem can be solved in tmux by spawning two separate sessions and synchronizing the second one to the windows of the first, then pointing a second new session to the first.
   
483行目: 500行目:
 
: Therefore, when my computer looses network connectivity, all "foo.something" clients are killed while "foo" remains. I can then call "rsc foo" to continue work from where I stopped.
 
: Therefore, when my computer looses network connectivity, all "foo.something" clients are killed while "foo" remains. I can then call "rsc foo" to continue work from where I stopped.
   
  +
=== ターミナルタイプにあわせて TERM 変数を修正する ===
=== tmux の起動時に設定を変更 ===
 
  +
[[#適切なターミナルの設定|tmux で TERM 変数を固定で設定]]する代わりに、ターミナルエミュレータのタイプにあわせて適切な TERM ({{ic|screen}} または {{ic|screen-256color}}) を設定することができます:
   
  +
{{hc|~/.tmux.conf|
By default tmux reads {{ic|~/.tmux.conf}} only if it was not already running. To have tmux load a configuration file afterwards, execute:
 
  +
## set the default TERM
  +
set -g default-terminal screen
  +
  +
## update the TERM variable of terminal emulator when creating a new session or attaching a existing session
  +
set -g update-environment 'DISPLAY SSH_ASKPASS SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY TERM'
  +
## determine if we should enable 256-colour support
  +
if "[[ ${TERM} =~ 256color || ${TERM} == fbterm ]]" 'set -g default-terminal screen-256color'
  +
}}
  +
  +
{{hc|1=~/.zshrc|2=
  +
## workaround for handling TERM variable in multiple tmux sessions properly from http://sourceforge.net/p/tmux/mailman/message/32751663/ by Nicholas Marriott
  +
if [[ -n ${TMUX} && -n ${commands[tmux]} ]];then
  +
case $(tmux showenv TERM 2>/dev/null) in
  +
*256color) ;&
  +
TERM=fbterm)
  +
TERM=screen-256color ;;
  +
*)
  +
TERM=screen
  +
esac
  +
fi
  +
}}
  +
  +
=== tmux を再起動せずに変更した設定を適用する ===
  +
  +
デフォルトでは tmux は起動時にしか {{ic|~/.tmux.conf}} を読み込みません。起動した後に設定ファイルを tmux に読み込ませるには、次を実行:
   
 
tmux source-file <path>
 
tmux source-file <path>
   
This can be added to {{ic|~/.tmux.conf}} as e. g.:
+
{{ic|~/.tmux.conf}} に以下のように追加することもできます:
   
 
bind r source-file <path>
 
bind r source-file <path>
   
  +
^: を実行してから以下を入力する方法もあります:
You can also do ^: and type :
 
 
source .tmux.conf
 
source .tmux.conf
   
  +
===既存のセッションに新しいセッションがアタッチされるたびにプログラムを実行するテンプレートスクリプト===
===Template script to run program in new session resp. attach to existing one===
 
   
 
This script checks for a program presumed to have been started by a previous run of itself. Unless found it creates a new tmux session and attaches to a window named after and running the program. If however the program was found it merely attaches to the session and selects the window.
 
This script checks for a program presumed to have been started by a previous run of itself. Unless found it creates a new tmux session and attaches to a window named after and running the program. If however the program was found it merely attaches to the session and selects the window.
515行目: 558行目:
   
 
=== ターミナルエミュレータのウィンドウタイトル ===
 
=== ターミナルエミュレータのウィンドウタイトル ===
  +
tmux ウィンドウで SSH 接続を行うと、ターミナルエミュレータのウィンドウタイトルが {{ic|user@localhost}} のままで {{ic|user@server}} にならないことに気づくかもしれません。タイトルバーを接続先のホストにあわせて変えられるようにするには、{{ic|~/.tmux.conf}} に以下を設定します:
If you SSH into a host in a tmux window, you'll notice the window title of your terminal emulator remains to be {{ic|user@localhost}} rather than {{ic|user@server}}. To allow the title bar to adapt to whatever host you connect to, set the following in {{ic|~/.tmux.conf}}
 
   
 
set -g set-titles on
 
set -g set-titles on
 
set -g set-titles-string "#T"
 
set -g set-titles-string "#T"
   
For {{ic|set-titles-string}}, {{ic|#T}} will display {{ic|user@host:~}} and change accordingly as you connect to different hosts.
+
{{ic|set-titles-string}} {{ic|#T}} {{ic|user@host:~}} を表示して、接続したホストによって表示が変わります。
   
 
=== 自動レイアウト ===
 
=== 自動レイアウト ===
  +
新しい分割を作成したり古い分割を解除する際に、現在選択されているレイアウトは適用されません。修正するには、以下のバインドを追加することで新しい、あるいは既存のペインに現在選択中のレイアウトが適用されるようになります:
When creating new splits or destroying older ones the currently selected layout isn't applied. To fix that, add following binds which will apply the currently selected layout to new or remaining panes:
 
   
 
bind-key -n M-c kill-pane \; select-layout
 
bind-key -n M-c kill-pane \; select-layout
531行目: 574行目:
   
 
[[vim]] ユーザーに使いやすい設定は [https://gist.github.com/anonymous/6bebae3eb9f7b972e6f0] を見て下さい。
 
[[vim]] ユーザーに使いやすい設定は [https://gist.github.com/anonymous/6bebae3eb9f7b972e6f0] を見て下さい。
  +
  +
tmux 2.4 では、以下の部分を:
  +
bind -t vi-copy 'v' begin-selection
  +
bind -t vi-copy 'y' copy-selection
  +
bind -t vi-copy 'Space' halfpage-down
  +
bind -t vi-copy 'Bspace' halfpage-up
  +
  +
以下のように変更してください:
  +
bind-key -T copy-mode-vi 'v' send -X begin-selection
  +
bind-key -T copy-mode-vi 'y' send -X copy-selection
  +
bind-key -T copy-mode-vi 'Space' send -X halfpage-down
  +
bind-key -T copy-mode-vi 'Bspace' send -X halfpage-up
   
 
== 参照 ==
 
== 参照 ==
   
* [https://bbs.archlinux.org/viewtopic.php?id=84157&p=1 BBS topic]
+
* [https://bbs.archlinux.org/viewtopic.php?id=84157&p=1 フォーラムスレッド]
* [http://www.dayid.org/os/notes/tm.html Screen and tmux feature comparison]
+
* [http://www.dayid.org/os/notes/tm.html Screen tmux の機能比較]
* [https://github.com/Lokaltog/powerline powerline], a dynamic statusbar for tmux
+
* [https://github.com/Lokaltog/powerline powerline] - tmux で使えるダイナミックステータスバー
* [https://github.com/tmux-plugins Plugins for tmux]
+
* [https://github.com/tmux-plugins tmux のプラグイン]
   
 
'''チュートリアル'''
 
'''チュートリアル'''
   
 
* [http://mutelight.org/articles/practical-tmux Practical Tmux]
 
* [http://mutelight.org/articles/practical-tmux Practical Tmux]
* [http://www.openbsd.org/faq/faq7.html#tmux Tmux FAQ (OpenBSD)]
+
* [http://www.openbsd.org/cgi-bin/man.cgi?query=tmux man ページ (OpenBSD)]
  +
* [http://blog.hawkhost.com/2010/06/28/tmux-the-terminal-multiplexer/ Tmux チュートリアル Part 1] と [http://blog.hawkhost.com/2010/07/02/tmux-%E2%80%93-the-terminal-multiplexer-part-2 Part 2]
* [http://www.openbsd.org/cgi-bin/man.cgi?query=tmux man page (OpenBSD)]
 
  +
* [https://leanpub.com/the-tao-of-tmux/read ''The Tao of tmux''] - [https://tmuxp.git-pull.com tmuxp] と [https://libtmux.git-pull.com libtmux] を開発した Tony Narlock による電子書籍
* [http://blog.hawkhost.com/2010/06/28/tmux-the-terminal-multiplexer/ Tmux tutorial Part 1] and [http://blog.hawkhost.com/2010/07/02/tmux-%E2%80%93-the-terminal-multiplexer-part-2 Part 2]
 

2017年10月12日 (木) 23:15時点における版

関連記事

Tmux はターミナルマルチプレクサです: 一つのスクリーンから別々のプログラムが動作する多数のターミナル (またはウィンドウ) を作成・アクセス・制御することができます。tmux はスクリーンから分離させてバックグラウンドで動作させることができ、後でまた繋ぎ直すことができます。

Tmux は GNU Screen の代わりになるものとして ISC ライセンスで配布されています。似ているところもありますが、プログラムには様々な違いが存在します。tmux の FAQ ページ を見て下さい。

目次

インストール

公式リポジトリにある tmux パッケージをインストールしてください。任意で tmux-bash-completionAUR をインストールすることで tmux の bash 補完を使うことができます。

設定

ユーザーごとの設定ファイルは ~/.tmux.conf に、グローバルな設定ファイルは /etc/tmux.conf に配置されます。

キーバインド

デフォルトでは、コマンドのキーバインドには Ctrl-b が前に付きます。例えば、ウィンドウを縦に分割するには Ctrl-b+% を押します。

ウィンドウを複数のペインに分割した後、ペインのサイズを変更するには、プレフィックスキー (例: Ctrl-b) を押してから、Ctrl を押しながら、Left/Right/Up/Down を押します。ペイン間の移動も同じ方法で行い、矢印キーの代わりに o を使います。

ヒント: screen と同じキーバインドを使いたい場合は /usr/share/tmux/screen-keys.conf を設定ファイルにコピーしてください。

キーバインドは tmux.conf の bind と unbind コマンドで変更することができます。例えばデフォルトのプレフィックスキーである Ctrl-bCtrl-a に変更するには設定ファイルに以下のコマンドを追加します:

unbind C-b
set -g prefix C-a
bind C-a send-prefix
ヒント: 特殊文字はクォートで囲ってプレフィックスとして使ってください。Ctrl の代わりに Alt (Meta) を使うこともできます。例: set -g prefix m-'\'

さらに、ウィンドウ間の移動をする方法には以下のものもあります:

Ctrl-b l (直前に選択されていたウィンドウに移動します)
Ctrl-b w (すべてのウィンドウ、及びウィンドウの番号の一覧を表示します)
Ctrl-b <ウィンドウの番号> (指定された番号のウィンドウに移動する。なお、デフォルトでは 0 - 9 の番号が割り当てられています)
Ctrl-b q  (ペインの番号を表示する。表示された番号を入力すると対応するペインに移動できます)

Tmux にはウィンドウを探す機能とそれへのキー割り当てがあり、多数のウィンドウを容易に行き来することができます:

Ctrl-b f <ウィンドウの名前> (名前でウィンドウを検索する)
Ctrl-b w (ウィンドウの一覧からウィンドウを選択する)

コピーモード

tmux ウィンドウには複数のモードが存在します。デフォルトのモードではウィンドウにアタッチされたターミナルに直接アクセスすることができ、もうひとつのモードはコピーモードです。コピーモードではバッファを移動することができ履歴をスクロールすることが可能です。vi または emacs スタイルのキーバインドが使えます。VISUAL や EDITOR に ‘vi’ が含まれていない場合、デフォルトでは emacs のキーバインドが使われます。

コピーモードに入るには以下を実行:

Ctrl-b [

デフォルトエディタと同じようにバッファを操作することができます。

コピーモードを終了するには、以下のキーバインドのどちらかを使います:

vi モード:

q

emacs モード:

Esc

URL のブラウズ

tmux の中で URL をブラウズするには urlviewAUR をインストールして設定する必要があります。

新しいターミナルの中で:

bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; run-shell "$TERMINAL -e urlview /tmp/tmux-buffer"

新しい tmux ウィンドウの中で (新しいターミナルは不要です):

bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; new-window -n "urlview" '$SHELL -c "urlview < /tmp/tmux-buffer"'

適切なターミナルの設定

256色

256色のターミナルを使っている場合、tmux で適切なターミナルを設定する必要があります。tmux 2.1 から、tmux または tmux-256color が適切な設定値です。tmux.conf で以下のように設定できます:

set -g default-terminal "tmux-256color"

もしくは screenscreen-256color などに設定する場合もあります:

set -g default-terminal "screen-256color"

また、.bashrc に以下を追加することで強制的に tmux から256色をサポートしているターミナルを認識させることができます:

alias tmux="tmux -2"

24ビットカラー

Tmux はバージョン 2.2 から24ビットカラーをサポートしています ([1])。使用しているターミナルが24ビットカラーをサポートしている場合 (gist を参照)、ターミナルを terminal-overrides 設定に追加してください。例えば、Termite を使っているのであれば以下のように追加します:

set -ga terminal-overrides ",xterm-termite:Tc"

他のターミナルの場合、xterm-termite を適切なターミナルのタイプに置き換えてください ($TERM に保存されています)。Tc の terminfo 拡張について詳しくは tmux(1) の man ページを見てください。

xterm-keys

tmux.conf で xterm-keys を有効にするには、以下の行を追加してください:

set-option -g xterm-keys on

tmux.conf で xterm-keys を有効にしたら、新しいエスケープコードがアプリケーションから認識されるようにカスタム terminfo を作成して宣言する必要があります。tic で以下のコードをコンパイルして TERM に "xterm-screen-256color" を指定してください:

# A screen- based TERMINFO that declares the escape sequences
# enabled by the tmux config "set-window-option -g xterm-keys".
#
# Prefix the name with xterm- since some applications inspect
# the TERM *name* in addition to the terminal capabilities advertised.
xterm-screen-256color|GNU Screen with 256 colors bce and tmux xterm-keys,

# As of Nov'11, the below keys are picked up by
# .../tmux/blob/master/trunk/xterm-keys.c:
	kDC=\E[3;2~, kEND=\E[1;2F, kHOM=\E[1;2H,
	kIC=\E[2;2~, kLFT=\E[1;2D, kNXT=\E[6;2~, kPRV=\E[5;2~,
	kRIT=\E[1;2C,

# Change this to screen-256color if the terminal you run tmux in
# doesn't support bce:
	use=screen-256color-bce,

他の設定

スクロールバックバッファを10000行に設定:

set -g history-limit 10000

ターミナルエミュレータの設定を上書き:

set -ga terminal-overrides ',xterm*:smcup@:rmcup@'
set -ga terminal-override ',rxvt-uni*:XT:Ms=\E]52;%p1%s;%p2%s\007'

マウスを切り替える:

bind-key m set-option -g mouse on \; display 'Mouse: ON'
bind-key M set-option -g mouse off \; display 'Mouse: OFF'

systemd で自動起動

起動時に tmux サーバーを立ち上げることには複数の利点が存在します。特に、新しい tmux セッションを起動する場合、既にサービスが動作していれば起動時間が短縮されます。

さらに、tmux セッションのカスタマイズは維持され、たとえログインしなくても tmux セッションを永続的にすることができます (tmux の設定をスクリプトで管理している場合やユーザーの tmux セッションを共有する場合などに有用です)。

以下のサービスは特定のユーザーで tmux を起動します (tmux@username.service で起動と同じ):

/etc/systemd/system/tmux@.service
[Unit]
Description=Start tmux in detached session

[Service]
Type=forking
User=%I
ExecStart=/usr/bin/tmux new-session -s %u -d
ExecStop=/usr/bin/tmux kill-session -t %u

[Install]
WantedBy=multi-user.target
ヒント: WorkingDirectory=custom_path を追加することで作業ディレクトリをカスタマイズすることも可能です。

または、(User=%I を使わずに) 上記のファイルを systemd/ユーザーディレクトリに配置することもできます、例: ~/.config/systemd/user/tmux.service。この方法だとユーザーインスタンスの自動起動を有効にしていない場合、ログイン時に tmux サービスが起動します。

セッションの開始

~/.tmux.conf に以下のように記述することでウィンドウがプリロードされた状態で tmux でセッションを開くことができます:

new  -n WindowName Command
neww -n WindowName Command
neww -n WindowName Command

ウィンドウが分割された状態でセッションを起動するには (マルチペイン)、分割したい neww の下に splitw コマンドを追加してください:

new  -s SessionName -n WindowName Command
neww -n foo/bar foo
splitw -v -p 50 -t 0 bar
selectw -t 1 
selectp -t 0

上記の場合、ウィンドウが2つ開いて、2番目のウィンドウは foo/bar という名前で縦に半分に分割されます (50%)。bar の上で foo が実行されます。フォーカスはウィンドウ 2 (foo/bar) のトップペイン (foo) になります。

ノート: .conf で base-index を 1 に指定しないかぎり、セッション・ウィンドウ・ペインの番号は 0 から割り振られます。

複数のセッションを管理したい場合、設定ファイルから別のセッションファイルを読み込んでください:

# initialize sessions
bind F source-file ~/.tmux/foo
bind B source-file ~/.tmux/bar

トラブルシューティング

スクロールの問題

ターミナルで Shift-Page Up/Down を使ってスクロールができない場合、以下の設定で xterm から識別子が始まるターミナルで smcup と rmcup の機能を削除できます:

set -ga terminal-overrides ',xterm*:smcup@:rmcup@'

上記の設定を使うとターミナルエミュレータは tmux のことを picomutt と同じようなフルスクリーンのアプリケーションと認識するようになり [2]、スクロールバックが正しく記録されるようになります。ただし、ウィンドウやペインを切り替えたときに多少表示が崩れるかもしれません。Tmux のネイティブのスクロールバックを使うことを推奨します。

マウスのスクロール

ノート: 以下の設定はセレクションバッファのコピー・ペーストと干渉します。セレクションバッファからコピー・ペーストするときは shift キーを押してください。

マウスホイールでスクロールしたい場合、.tmux.conf で mode-mouse をオンにして下さい:

set -g mouse on

スクロール履歴を設定するには:

set -g history-limit 30000

tmux 2.1 からは、マウスホイールでスクロールするには ~/.tmux.conf に以下のどちらかあるいは両方を追加してみてください:

bind -T root WheelUpPane   if-shell -F -t = "#{alternate_on}" "send-keys -M" "select-pane -t =; copy-mode -e; send-keys -M"
bind -T root WheelDownPane if-shell -F -t = "#{alternate_on}" "send-keys -M" "select-pane -t =; send-keys -M"

上記の設定では一行ずつしかスクロールしませんが、以下を追加することでページ単位でスクロールすることができます:

bind -t vi-copy    WheelUpPane   page-up
bind -t vi-copy    WheelDownPane page-down
bind -t emacs-copy WheelUpPane   page-up
bind -t emacs-copy WheelDownPane page-down

UTF-8 のマウスイベントをターミナルエミュレータがサポートしていない

ターミナルエミュレータが UTF-8 のマウスイベントに対応していない場合、tmux の mouse on オプションを設定しても、ターミナルウィンドウの中で左クリックすると [M#[Ma などの文字列がプロンプトに貼り付けられてしまいます。

この問題を解決するには以下を設定:

set -g mouse-utf8 off

Shift+F6 が Midnight Commander で動作しない

Midnight Commander#ショートカットが機能しないを参照してください。

X クリップボードの統合

ヒント: tmux のプラグイン tmux-yank は同じような機能を提供します。

tmux のセレクションを X クリップボード (や X プライマリ/セカンダリセレクション) にコピーしたり、その逆ができます。以下の tmux 設定ファイルのスニペットは xsel プログラムを使って X クリップボード/セレクションと tmux のセレクションを効果的に統合します:

# Emacs style
bind-key -T copy-mode y send-key -X copy-pipe-and-cancel "xsel -i -p && xsel -o -p | xsel -i -b"
bind-key C-y run "xsel -o | tmux load-buffer - ; tmux paste-buffer"
# Vim style
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xsel -i -p && xsel -o -p | xsel -i -b"
bind-key p run "xsel -o | tmux load-buffer - ; tmux paste-buffer"

xclip を使うことも可能です。xsel と違って現在のロケールに適合しない生のビットストリームの出力も上手く処理されます。ただし、tmux のバッファを読み込んだ後に xclip は標準出力を閉じないため、xclip ではなく xsel を使う方が簡単です。xclip では tmux からコピー作業の完了を知ることができないため、xclip が終了するまで待機することになり、tmux が反応しなくなってしまいます。以下のように xclipSTDOUT/dev/null にリダイレクトすれば解決します:

# Vim style
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -i -sel clip > /dev/null"
bind-key p run "xclip -o -sel clip | tmux load-buffer - ; tmux paste-buffer"

Urxvt の中クリック

ノート: 以下の設定を使うにはマウスのサポートを有効にする必要があります。

中マウスのクリックによって tmux で urxvt のコピーアンドペーストを有効にする非公式の perl 拡張が存在します (公式の FAQ に記載があります)。

まず、perl スクリプトをダウンロードして urxvts の perl ライブラリに配置してください:

wget http://anti.teamidiot.de/static/nei/*/Code/urxvt/osc-xterm-clipboard
mv osc-xterm-clipboard /usr/lib/urxvt/perl/

また、.Xdefaults で perl スクリプトを有効にする必要があります:

~/.Xdefaults
...
*URxvt.perl-ext-common:		osc-xterm-clipboard
...

次に、tmux に新しい関数を指定してマウスのサポートを有効にします:

~/.tmux.conf
...
set-option -ga terminal-override ',rxvt-uni*:XT:Ms=\E]52;%p1%s;%p2%s\007'
set -g mouse on
...

新しい中クリック機能を試すには全ての tmux インスタンスを終了してください。

tmux の中では Shift+中マウスクリックを使うことでクリップボードのセレクションが貼り付けられます。中マウスのクリックだけの場合、tmux のバッファが貼り付けられます。

ヒントとテクニック

デフォルトのセッションレイアウトで tmux を起動

tmuxinator や tmuxp などのセッションマネージャを使うことで共通のセッション設定を簡単に管理できます。

tmuxinator の場合、AUR から tmuxinatorAUR をインストールします。次のコマンドでインストールをテストできます:

tmuxinator doctor

デフォルトのレイアウト値を取得

Tmux を通常通りに起動してウィンドウとペインのレイアウトを好きなように設定してください。設定が完了したら、以下のコマンドを実行することで現在のレイアウト値を取得できます (Tmux セッションの中から実行してください):

tmux list-windows

3つのペインと2つのペインの2つのウィンドウからなるレイアウトの場合、出力は以下のようになります:

0: default* (3 panes) [274x83] [layout 20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]}] @2 (active)
1: remote- (2 panes) [274x83] [layout e3d3,274x83,0,0[274x41,0,0,4,274x41,0,42,7]] @3                                         

コピーする必要があるのは [layout... の後から ... ] @2 (active) を除いた部分です。1番目のウィンドウレイアウトであれば 20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]} をコピーします。

デフォルトの tmux レイアウトを定義する

レイアウト値がわかったら現在の tmux セッションを終了してください。その後、Tmuxinator の設定ファイルを編集して Tmux のデフォルトセッションレイアウトを作成します (以下の例をそのままコピーするのではなく、上のセクションで確認したレイアウト値を使ってください):

~/.tmuxinator/default.yml
name: default
root: ~/
windows:
  - default:
      layout: 20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]}
      panes:
        - clear
        - vim
        - clear && emacs -nw
  - remote:
      layout: 24ab,274x83,0,0{137x83,0,0,3,136x83,138,0,4}
      panes:
        - 
        - 

例では2つのウィンドウを "default" と "remote" として定義しています。適当なレイアウト値を決めてください。各ペインに対して最低でも1行は - を使う必要があります。1番目のウィンドウペインでは、1番目のペインでコマンドライン "clear" を、2番目のペインで "vim" を、3番目のペインで "clear && emacs -nw" を実行します。2番目のウィンドウレイアウトには何も起動コマンドを定義していません。

新しいデフォルトレイアウトをテストするには:

mux default

デフォルトの tmux レイアウトで tmux を自動起動

デフォルトの Tmux セッションレイアウトでターミナルセッションを起動したい場合、以下を編集してください:

~/.bashrc
 if [ -z "$TMUX" ]; then
   mux default          
 fi                     

urxvt で tmux を起動

以下のコマンドで urxvt を起動することで tmux セッションも起動します。.ratpoisonrc ファイルの exec コマンドなどから使うことができます。

urxvt -e bash -c "tmux -q has-session && exec tmux attach-session -d || exec tmux new-session -n$USER -s$USER@$HOSTNAME"

シェルのログイン時に tmux を起動

以下の bash コードを .bashrc のエイリアスの前に追加してください。他のシェルのコードも同じようになります:

~/.bashrc
# If not running interactively, do not do anything
[[ $- != *i* ]] && return
[[ -z "$TMUX" ]] && exec tmux
ノート: This snippet ensures that tmux is not launched inside of itself (something tmux usually already checks for anyway). tmux sets $TMUX to the socket it is using whenever it runs, so if $TMUX isn't set or is length 0, we know we aren't already running tmux.

ログイン時にまずアタッチできないか試してみて tmux が実行されていない場合にのみセッションを作成したい場合、以下のスニペットを追加します:

# TMUX
if which tmux >/dev/null 2>&1; then
    #if not inside a tmux session, and if no session is started, start a new session
    test -z "$TMUX" && (tmux attach || tmux new-session)
fi

以下のスニペットは起動する前に tmux がインストールされているかどうかチェックします。また、ログアウト時に既存の tmux セッションに再アタッチすることで、同一のターミナルからすばやく全ての tmux セッションをシャットダウンできるようにしています。.

# TMUX
if which tmux >/dev/null 2>&1; then
    # if no session is started, start a new session
    test -z ${TMUX} && tmux

    # when quitting tmux, try to attach
    while test -z ${TMUX}; do
        tmux attach || break
    done
fi

既存のデタッチされたセッションにアタッチしたり新しいセッションを起動する場合:

if [[ -z "$TMUX" ]] ;then
    ID="`tmux ls | grep -vm1 attached | cut -d: -f1`" # get the id of a deattached session
    if [[ -z "$ID" ]] ;then # if not available create a new one
        tmux new-session
    else
        tmux attach-session -t "$ID" # if available attach to it
    fi
fi

非ログインシェルを起動する

Tmux は デフォルトログインシェル を起動するようになっていますが、これは複数の副作用があります:

  • fortune を使っている場合、新しいパネルを作成するたびに引用句が表示されることがあります。
  • 新しいパネルを作成するたびに ~/.profile などのログインシェルの設定ファイルが解釈されるため、セッションの初期化時に実行するようにしていたコマンド (例: オーディオレベルの設定) が実行されてしまいます。

この挙動を無効化するには、以下を ~/.tmux.conf に追加してください:

set -g default-command "${SHELL}"

tmux のウィンドウをタブのように使う

以下の設定を ~/.tmux.conf に追加することで tmux のウィンドウをタブのように使うことができます (urxvt のタブ拡張を参照)。仮想タブはターミナルエミュレータから独立しているという利点があります。

#urxvt tab like window switching (-n: no prior escape seq)
bind -n S-down new-window
bind -n S-left prev
bind -n S-right next
bind -n C-left swap-window -t -1
bind -n C-right swap-window -t +1

もちろん、ターミナルなど他のアプリケーションのホットキーと重複させてはいけません。そのような場合、ターミナルのタブ機能を無効化して置き換えるほうが良いでしょう。

It can also come handy to supplement the EOT hotkey Ctrl+d with one for tmux's detach:

bind-key -n C-j detach

様々なウィンドウのセッションを同時に操作するクライアント

Practical Tmux で、Brandur Leach は以下のように書いています:

複数のクライアントを一つのセッションにアタッチする際、Screen と tmux の挙動は少々異なっています。Screen では、各クライアントがセッションに接続され、セッションの中で別々のウィンドウが表示されます。tmux では、一つのセッションに接続された全てのクライアントが同じウィンドウを表示します。
This problem can be solved in tmux by spawning two separate sessions and synchronizing the second one to the windows of the first, then pointing a second new session to the first.

The script “tmx” below implements this — the version here is slightly modified to execute “tmux new-window” if “1” is its second parameter. Invoked as tmx <base session name> [1] it launches the base session if necessary. Otherwise a new “client” session linked to the base, optionally add a new window and attach, setting it to kill itself once it turns “zombie”.

tmx
#!/bin/bash

#
# Modified TMUX start script from:
#     http://forums.gentoo.org/viewtopic-t-836006-start-0.html
#
# Store it to `~/bin/tmx` and issue `chmod +x`.
#

# Works because bash automatically trims by assigning to variables and by 
# passing arguments
trim() { echo $1; }

if [[ -z "$1" ]]; then
    echo "Specify session name as the first argument"
    exit
fi

# Only because I often issue `ls` to this script by accident
if [[ "$1" == "ls" ]]; then
    tmux ls
    exit
fi

base_session="$1"
# This actually works without the trim() on all systems except OSX
tmux_nb=$(trim `tmux ls | grep "^$base_session" | wc -l`)
if [[ "$tmux_nb" == "0" ]]; then
    echo "Launching tmux base session $base_session ..."
    tmux new-session -s $base_session
else
    # Make sure we are not already in a tmux session
    if [[ -z "$TMUX" ]]; then
        echo "Launching copy of base session $base_session ..."
        # Session id is date and time to prevent conflict
        session_id=`date +%Y%m%d%H%M%S`
        # Create a new session (without attaching it) and link to base session 
        # to share windows
        tmux new-session -d -t $base_session -s $session_id
        if [[ "$2" == "1" ]]; then
		# Create a new window in that session
		tmux new-window
	fi
        # Attach to the new session & kill it once orphaned
	tmux attach-session -t $session_id \; set-option destroy-unattached
    fi
fi

A useful setting for this is

setw -g aggressive-resize on

added to ~/.tmux.conf. It causes tmux to resize a window based on the smallest client actually viewing it, not on the smallest one attached to the entire session.

An alternative taken from [3] is to put the following ~/.bashrc:

.bashrc
function rsc() {
  CLIENTID=$1.`date +%S`
  tmux new-session -d -t $1 -s $CLIENTID \; set-option destroy-unattached \; attach-session -t $CLIENTID
}

function mksc() {
  tmux new-session -d -s $1
  rsc $1
}

Citing the author:

"mksc foo" creates a always detached permanent client named "foo". It also calls "rsc foo" to create a client to newly created session. "rsc foo" creates a new client grouped by "foo" name. It has destroy-unattached turned on so when I leave it, it kills client.
Therefore, when my computer looses network connectivity, all "foo.something" clients are killed while "foo" remains. I can then call "rsc foo" to continue work from where I stopped.

ターミナルタイプにあわせて TERM 変数を修正する

tmux で TERM 変数を固定で設定する代わりに、ターミナルエミュレータのタイプにあわせて適切な TERM (screen または screen-256color) を設定することができます:

~/.tmux.conf
## set the default TERM
set -g default-terminal screen

## update the TERM variable of terminal emulator when creating a new session or attaching a existing session
set -g update-environment 'DISPLAY SSH_ASKPASS SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY TERM'
## determine if we should enable 256-colour support
if "[[ ${TERM} =~ 256color || ${TERM} == fbterm ]]" 'set -g default-terminal screen-256color'
~/.zshrc
## workaround for handling TERM variable in multiple tmux sessions properly from http://sourceforge.net/p/tmux/mailman/message/32751663/ by Nicholas Marriott
if [[ -n ${TMUX} && -n ${commands[tmux]} ]];then
        case $(tmux showenv TERM 2>/dev/null) in
                *256color) ;&
                TERM=fbterm)
                        TERM=screen-256color ;;
                *)
                        TERM=screen
        esac
fi

tmux を再起動せずに変更した設定を適用する

デフォルトでは tmux は起動時にしか ~/.tmux.conf を読み込みません。起動した後に設定ファイルを tmux に読み込ませるには、次を実行:

tmux source-file <path>

~/.tmux.conf に以下のように追加することもできます:

bind r source-file <path>

^: を実行してから以下を入力する方法もあります:

source .tmux.conf

既存のセッションに新しいセッションがアタッチされるたびにプログラムを実行するテンプレートスクリプト

This script checks for a program presumed to have been started by a previous run of itself. Unless found it creates a new tmux session and attaches to a window named after and running the program. If however the program was found it merely attaches to the session and selects the window.

#!/bin/bash

PID=$(pidof $1)

if [ -z "$PID" ]; then
    tmux new-session -d -s main ;
    tmux new-window -t main -n $1 "$*" ;
fi
    tmux attach-session -d -t main ;
    tmux select-window -t $1 ;
exit 0

nicklist プラグインを使って irssi を実行する派生版は irssi の ArchWiki ページにあります。

ターミナルエミュレータのウィンドウタイトル

tmux ウィンドウで SSH 接続を行うと、ターミナルエミュレータのウィンドウタイトルが user@localhost のままで user@server にならないことに気づくかもしれません。タイトルバーを接続先のホストにあわせて変えられるようにするには、~/.tmux.conf に以下を設定します:

set -g set-titles on
set -g set-titles-string "#T"

set-titles-string#Tuser@host:~ を表示して、接続したホストによって表示が変わります。

自動レイアウト

新しい分割を作成したり古い分割を解除する際に、現在選択されているレイアウトは適用されません。修正するには、以下のバインドを追加することで新しい、あるいは既存のペインに現在選択中のレイアウトが適用されるようになります:

bind-key -n M-c kill-pane \; select-layout
bind-key -n M-n split-window \; select-layout

Vim フレンドリな設定

vim ユーザーに使いやすい設定は [4] を見て下さい。

tmux 2.4 では、以下の部分を:

bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection
bind -t vi-copy 'Space' halfpage-down
bind -t vi-copy 'Bspace' halfpage-up

以下のように変更してください:

bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-selection
bind-key -T copy-mode-vi 'Space' send -X halfpage-down
bind-key -T copy-mode-vi 'Bspace' send -X halfpage-up

参照

チュートリアル