「Tmux」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
35行目: 35行目:
 
}}
 
}}
   
{{Tip|Quote special characters to use them as prefix. You may also use {{ic|Alt}} instead of {{ic|Ctrl}}. For example: {{ic|set -g prefix m-'\'}}}}
+
{{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-'\'}}}}
   
 
Additional ways to move between windows include the following:
 
Additional ways to move between windows include the following:
169行目: 169行目:
   
 
マウスホイールでスクロールしたい場合、{{ic|.tmux.conf}} で mode-mouse をオンにして下さい:
 
マウスホイールでスクロールしたい場合、{{ic|.tmux.conf}} で mode-mouse をオンにして下さい:
setw -g mode-mouse on
+
set -g mouse on
   
 
スクロール履歴を設定するには:
 
スクロール履歴を設定するには:
209行目: 209行目:
 
Then compile the file with {{ic|tic}}. The keys should be working now.
 
Then compile the file with {{ic|tic}}. The keys should be working now.
   
  +
== X クリップボードの統合 ==
== ICCCM selection integration ==
 
   
{{Tip|The tmux plugin [https://github.com/tmux-plugins/tmux-yank tmux-yank] provides similar functionality.}}
+
{{Tip|tmux のプラグイン [https://github.com/tmux-plugins/tmux-yank tmux-yank] は同じような機能を提供します。}}
   
  +
tmux のセレクションを X クリップボード (や X プライマリ/セカンダリセレクション) にコピーしたり、その逆ができます。以下の tmux 設定ファイルのスニペットは {{Pkg|xsel}} プログラムを使って X クリップボード/セレクションと tmux のセレクションを効果的に統合します:
It is possible to copy a tmux paste buffer to an [[Wikipedia:ja:Inter-Client Communication Conventions Manual|ICCCM]] selection, and vice-versa, by defining a shell command which interfaces tmux with an X11 selection interface. The following tmux config file snippet effectively integrates {{Ic|CLIPBOARD}} with the current tmux paste buffer using the program {{Pkg|xclip}}:
 
   
  +
# Emacs style
{{hc|~/.tmux.conf|
 
  +
bind-key -t emacs-copy M-w copy-pipe "xsel -i -p -b"
...
 
  +
bind-key C-y run "xsel -o | tmux load-buffer - ; tmux paste-buffer"
##CLIPBOARD selection integration
 
##Requires prefix key before the command key
 
#Copy tmux paste buffer to CLIPBOARD
 
bind C-c run "tmux save-buffer - <nowiki>|</nowiki> xclip -i -selection clipboard"
 
#Copy CLIPBOARD to tmux paste buffer and paste tmux paste buffer
 
bind C-v run "tmux set-buffer -- \"$(xclip -o -selection clipboard)\"; tmux paste-buffer"
 
}}
 
   
  +
# Vim style
Alternatively you can use the program {{ic|xsel}}, provided by {{Pkg|xsel}}:
 
  +
bind-key -t vi-copy y copy-pipe "xsel -i -p -b"
  +
bind-key p run "xsel -o | tmux load-buffer - ; tmux paste-buffer"
   
  +
{{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:
{{hc|~/.tmux.conf|
 
...
 
##CLIPBOARD selection integration
 
##Requires prefix key before the command key
 
#Copy tmux paste buffer to CLIPBOARD
 
bind C-c run "tmux show-buffer <nowiki>|</nowiki> xsel -i -b"
 
#Copy CLIPBOARD to tmux paste buffer and paste tmux paste buffer
 
bind C-v run "tmux set-buffer -- \"$(xsel -o -b)\"; tmux paste-buffer"
 
}}
 
   
  +
# Vim style
It seems <code>xclip</code> does not close <code>STDOUT</code> after it has read from <code>tmux</code>'s buffer. As such, <code>tmux</code> doesn't know that the copy task has completed, and continues to /await <code>xclip</code>'s termination, thereby rendering the window manager unresponsive. To work around this, you can execute the command via <code>run-shell -b</code> instead of <code>run</code>, you can redirect <code>STDOUT</code> of <code>xclip</code> to <code>/dev/null</code>, or you can use an alternative command like <code>xsel</code>.
 
  +
bind-key -t vi-copy y copy-pipe "xclip -i -sel clip > /dev/null"
  +
bind-key p run "xclip -o -sel clip | tmux load-buffer -"
   
 
=== Urxvt の中クリック ===
 
=== Urxvt の中クリック ===
243行目: 233行目:
 
{{Note|To use this, you need to enable mouse support}}
 
{{Note|To use this, you need to enable mouse support}}
   
There is an unofficial perl extension (mentioned in the official [http://sourceforge.net/p/tmux/tmux-code/ci/master/tree/FAQ FAQ]) to enable copying/pasting in and out of urxvt with tmux via Middle Mouse Clicking.
+
中マウスのクリックによって tmux urxvt のコピーアンドペーストを有効にする非公式の perl 拡張が存在します (公式の [http://sourceforge.net/p/tmux/tmux-code/ci/master/tree/FAQ FAQ] に記載があります)
   
  +
まず、perl スクリプトをダウンロードして urxvts の perl ライブラリに配置してください:
First, you will need to download the perl script and place it into urxvts perl lib:
 
   
 
{{bc|wget http://anti.teamidiot.de/static/nei/*/Code/urxvt/osc-xterm-clipboard
 
{{bc|wget http://anti.teamidiot.de/static/nei/*/Code/urxvt/osc-xterm-clipboard
251行目: 241行目:
 
}}
 
}}
   
  +
また、{{ic|.Xdefaults}} で perl スクリプトを有効にする必要があります:
You will also need to enable that perl script in your .Xdefaults:
 
   
 
{{hc|~/.Xdefaults|
 
{{hc|~/.Xdefaults|
259行目: 249行目:
 
}}
 
}}
   
Next, you want to tell tmux about the new function and enable mouse support (if you haven't already). The third option is optional, to enable scrolling and selecting inside panes with your mouse:
+
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|
 
...
 
...
 
set-option -ga terminal-override ',rxvt-uni*:XT:Ms<nowiki>=</nowiki>\E]52;%p1%s;%p2%s\007'
 
set-option -ga terminal-override ',rxvt-uni*:XT:Ms<nowiki>=</nowiki>\E]52;%p1%s;%p2%s\007'
set-window-option -g mode-mouse on
+
set -g mouse on
set-option -g mouse-select-pane on
 
 
...
 
...
 
}}
 
}}
276行目: 265行目:
 
== Tips and tricks ==
 
== Tips and tricks ==
 
=== デフォルトのセッションレイアウトで tmux を起動 ===
 
=== デフォルトのセッションレイアウトで tmux を起動 ===
  +
Tmux のデフォルトセションレイアウトを設定するには、[[AUR]] から {{AUR|tmuxinator}} をインストールします。次のコマンドでインストールをテストできます:
To setup your default Tmux session layout, you install {{AUR|tmuxinator}} from [[AUR]]. Test your installation with
 
   
 
tmuxinator doctor
 
tmuxinator doctor
383行目: 372行目:
 
{{note|Instead of using the bashrc file, you can launch tmux when you start your terminal emulator. (i. e. urxvt -e tmux)}}
 
{{note|Instead of using the bashrc file, you can launch tmux when you start your terminal emulator. (i. e. urxvt -e tmux)}}
   
  +
=== 非ログインシェルを起動する ===
=== Start a non-login shell ===
 
   
Tmux starts a [http://unix.stackexchange.com/questions/38175 login shell] [http://comments.gmane.org/gmane.comp.terminal-emulators.tmux.user/5997 by default], which may result in multiple negative side effects:
+
Tmux [http://comments.gmane.org/gmane.comp.terminal-emulators.tmux.user/5997 デフォルト] [http://unix.stackexchange.com/questions/38175 ログインシェル] を起動するようになっていますが、これは複数の副作用があります:
  +
* [[fortune]] を使っている場合、新しいパネルを作成するたびに引用句が表示されることがあります。
* Users of [[Wikipedia:fortune (Unix)|fortune]] may notice that quotes are printed when creating a new panel.
 
  +
* 新しいパネルを作成するたびに {{ic|~/.profile}} などのログインシェルの設定ファイルが解釈されるため、セッションの初期化時に実行するようにしていたコマンド (例: オーディオレベルの設定) が実行されてしまいます。
* The configuration files for login shells such as {{ic|~/.profile}} are interpreted each time a new panel is created, so commands intended to be run on session initialization (e.g. setting audio level) are executed.
 
   
To disable this behaviour, add to {{ic|~/.tmux.conf}}:
+
この挙動を無効化するには、以下を {{ic|~/.tmux.conf}} に追加してください:
   
 
set -g default-command "${SHELL}"
 
set -g default-command "${SHELL}"
475行目: 464行目:
 
added to {{ic|~/.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.
 
added to {{ic|~/.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 [http://sourceforge.net/mailarchive/forum.php?thread_name=CAPBqLKEC0MAFR%2BWUYqCuyd%3DKB47HK8CFSuAf%3Dd%3DW2H3F4fpMZw%40mail.gmail.com&forum_name=tmux-users] is to put the following ~/.bashrc:
+
An alternative taken from [http://comments.gmane.org/gmane.comp.terminal-emulators.tmux.user/2632] is to put the following ~/.bashrc:
   
 
{{hc|.bashrc|2=<nowiki>
 
{{hc|.bashrc|2=<nowiki>
523行目: 512行目:
 
exit 0
 
exit 0
   
  +
''nicklist'' プラグインを使って ''irssi'' を実行する派生版は [[Irssi#tmux で irssi と nicklist|irssi の ArchWiki ページ]]にあります。
A derived version to run ''irssi'' with the ''nicklist'' plugin can be found on [[Irssi#irssi_with_nicklist_in_tmux|its ArchWiki page]].
 
   
 
=== ターミナルエミュレータのウィンドウタイトル ===
 
=== ターミナルエミュレータのウィンドウタイトル ===
542行目: 531行目:
   
 
[[vim]] ユーザーに使いやすい設定は [https://gist.github.com/anonymous/6bebae3eb9f7b972e6f0] を見て下さい。
 
[[vim]] ユーザーに使いやすい設定は [https://gist.github.com/anonymous/6bebae3eb9f7b972e6f0] を見て下さい。
 
=== 大量のテキストが出力されたときに tmux がフリーズする ===
 
 
If a program run inside tmux runs amok and starts printing lots of output, tmux tends to hang and {{ic|Ctrl+C}} does not get through. This can be prevented by limiting how much text is printed to the console at any time.
 
 
{{bc|
 
setw -g c0-change-trigger 10
 
setw -g c0-change-interval 250
 
}}
 
   
 
== 参照 ==
 
== 参照 ==

2015年11月28日 (土) 17:13時点における版

関連記事

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

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

インストール

公式リポジトリにある tmux パッケージをインストールしてください。

設定

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

キーバインド

デフォルトでは、コマンドのキーバインドには 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
ヒント: Quote special characters to use them as prefix. You may also use Alt (called Meta) instead of Ctrl. For example: set -g prefix m-'\'

Additional ways to move between windows include the following:

Ctrl-b l (Move to the previously selected window)
Ctrl-b w (List all windows / window numbers)
Ctrl-b <window number> (Move to the specified window number, the default bindings are from 0 – 9)
Ctrl-b q  (Show pane numbers, when the numbers show up type the key to goto that pane)

Tmux has a find-window option & key binding to ease navigation of many windows:

Ctrl-b f <window name> (Search for window name)
Ctrl-b w (Select from interactive list of windows)

コピーモード

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 [

You can navigate the buffer as you would in your default editor.

To quit copy mode, use one of the following keybindings:

vi モード:

q

emacs モード:

Esc

URL のブラウズ

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

Inside a new terminal:

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

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"'

適切なターミナルの設定

If you are using a 256 colour terminal, you will need to set the correct term in tmux. You can do this in tmux.conf:

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

If you enable xterm-keys in your 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 tic and you can use "xterm-screen-256color" as your TERM:

# 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

systemd で自動起動

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.

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 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
ヒント: You may want to add WorkingDirectory=custom_path to customize working directory.

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

セッションの開始

You can have tmux open a session with preloaded windows by including those details in your ~/.tmux.conf:

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

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
neww -n foo/bar foo
splitw -v -p 50 -t 0 bar
selectw -t 1 
selectp -t 0

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).

ノート: Numbering for sessions, windows and panes starts at zero, unless you have specified a base-index of 1 in your .conf

To manage multiple sessions, source separate session files from your conf file:

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

トラブルシューティング

スクロールの問題

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 xterm:

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

This tricks the terminal emulator into thinking Tmux is a full screen application like pico or mutt[1], 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.

マウスのスクロール

ノート: This interferes with selection buffer copying and pasting. To copy/paste to/from the selection buffer hold the shift key.

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

set -g mouse on

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

set -g history-limit 30000

Fix reverse-video/italic mode in urxvt

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 screen_terminfo="screen" by screen_terminfo="screen-256color".

 mkdir $HOME/.terminfo/
 screen_terminfo="screen"

Create a new terminfo adding the italic escape code:

 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

Then, you must add the following line in your tmux.conf. If you already defined default-terminal, just replace it.

 set -g default-terminal "screen-it"

The source of this solution can be found at [2], 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 で動作しない

If the Shift+F6 key combination is not working with either TERM=screen or 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 tic. The keys should be working now.

X クリップボードの統合

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

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

# Emacs style
bind-key -t emacs-copy M-w copy-pipe "xsel -i -p -b"
bind-key C-y run "xsel -o | tmux load-buffer - ; tmux paste-buffer"
# Vim style
bind-key -t vi-copy y copy-pipe "xsel -i -p -b"
bind-key p run "xsel -o | tmux load-buffer - ; tmux paste-buffer"

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 xsel instead of xclip, 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 STDOUT of xclip to /dev/null, like in the following:

# Vim style
bind-key -t vi-copy y copy-pipe "xclip -i -sel clip > /dev/null"
bind-key p run "xclip -o -sel clip | tmux load-buffer -"

Urxvt の中クリック

ノート: To use this, you need to enable mouse support

中マウスのクリックによって 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
...

Next, you want to tell tmux about the new function and enable mouse support (if you haven't already):

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

That's it. Be sure to end all instances of tmux before trying the new MiddleClick functionality.

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 のデフォルトセションレイアウトを設定するには、AUR から tmuxinatorAUR をインストールします。次のコマンドでインストールをテストできます:

tmuxinator doctor

Get the default layout values

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

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)
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]}

デフォルトの 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)

~/.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:
        - 
        - 

The example defines two windows named "default" and "remote". With your determined layout values. For each pane you have to use at least one - 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

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

If you like to start your terminal session with your default Tmux session layout edit

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

urxvt で tmux を起動

Use this command to start urxvt with a started tmux session. I use this with the exec command from my .ratpoisonrc file.

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

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

Simply add the following line of bash code to your .bashrc before your aliases; the code for other shells is very similar:

~/.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.

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.

# 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

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.

# 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

Another possibility is to try to attach to existing deattached session or start a new session:

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
ノート: Instead of using the bashrc file, you can launch tmux when you start your terminal emulator. (i. e. urxvt -e tmux)

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

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

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

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

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

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

The following settings added to ~/.tmux.conf allow to use tmux windows like tabs, such as those provided by the reference of these hotkeys — 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)
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

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 Ctrl+d with one for tmux's detach:

bind-key -n C-j detach

Clients simultaneously interacting with various windows of a session

In Practical Tmux, Brandur Leach writes:

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.

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.

tmux の起動時に設定を変更

By default tmux reads ~/.tmux.conf only if it was not already running. To have tmux load a configuration file afterwards, execute:

tmux source-file <path>

This can be added to ~/.tmux.conf as e. g.:

bind r source-file <path>

You can also do ^: and type :

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.

#!/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 ページにあります。

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

If you SSH into a host in a tmux window, you'll notice the window title of your terminal emulator remains to be user@localhost rather than user@server. To allow the title bar to adapt to whatever host you connect to, set the following in ~/.tmux.conf

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

For set-titles-string, #T will display user@host:~ and change accordingly as you connect to different hosts.

自動レイアウト

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-n split-window \; select-layout

Vim フレンドリな設定

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

参照

チュートリアル