tmux関連
Posted:
mac にソースコードからインストール
2024/01時点のbrewのパッケージがsixel未対応だったのでソースからインストール
brew install autoconf automake pkg-config utf8proc
git clone https://github.com/tmux/tmux.git
cd tmux
sh autogen.sh
./configure --enable-sixel --enable-utf8proc
make
sudo make install
/usr/local/bin 配下にインストールされる。
コンテナ環境内のtmuxを使おうとするとターミナルのリサイズを検知しない
2024/02頃、Rancher Desktopで起きたのだが、ターミナル内でコンテナを起動し、コンテナ内のtmuxを起動して使ったところ、tmuxがターミナルのサイズ(COLS, LOWとか)に合わせてリサイズしなかった。
ターミナルはウィンドウのリサイズ時、中のプログラムにSIGWINCH( ウィンドウサイズ変更 )シグナルを送る。 コンテナはうまくこれを拾えていない可能性がある。
mac側のtmuxを利用する分にはうまくいく。
プラグインマネージャ、tpmのインストール
ghq get https://github.com/tmux-plugins/tpm
mkdir -p ~/.config/tmux/plugins
cd ~/.config/tmux/plugins
ln -s ~/workspace/git/github.com/tmux-plugins/tpm ./tpm
tmux.confの最後に下記を追記
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
上記の前に追加したいプラグインを列挙。tmuxの設定を再読込して、prefix + I でインストール。
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-open'
set -g @plugin 'tmux-plugins/tmux-copycat'
追加したプラグイン
- tmux-copycat
- URLをハイライトさせる
- prefix + ctrl-u
- tmux-open
- ハイライトしたURLを開く
- macはopen
- linuxはxdg-opnen
- oキーの振る舞いを変える場合
- set -g @open ‘w3m’
- エディタを変更する場合
- set -g @open-editor ‘C-x’
- ハイライトしたURLを開く
URLを w3m で開きたい
tmux-openはlinuxではxdg-openを用いてURLを開く。 xdg-open( xdg-utils - ArchWiki )はxdg-mime( xdg-utils - ArchWiki )に問い合わせを行い対象を開く。 xdg-mimeの設定はxdg-settingsを用いて行う。
xdg-settings set default-web-browser firefox.desktop
今回、私はX無しのコンソールだけのLinuxでw3mで開きたい。 xdg-open は環境変数 BROWSERを見てくれる。このため下記のように.bashrcなどの設定ファイルで指定すると実行してくれる。
export BROWSER='w3m'
私の場合、更にtmuxで split-window してからw3mで閲覧したかったので、下記のような簡単なスクリプトを作って呼び出している。
#!/bin/bash
tmux split-window -v
tmux send-keys "w3m $1" C-m
ただ、こういったスクリプトを呼び出す場合、xdg-openは「パスの展開は行わない」ので、~/.bin/w3m.bash みたいな指定は駄目で、フルパスで指定する必要があった。
export BROWSER='/home/exampleuser/.config/tmux/w3m.bash'
xdg-open https://google.com/
これでw3mが起動した。 この方法でtmux-openする際には環境変数、tmux設定が動作する必要があるので、一度、再起動なりログアウト、ログインを行った方が良い。 tmux-openで https://google.com をテキスト選択肢、「o」を押すとw3mが起動した。
ここまで来ると「URLをハイライトさせる」動作もスピーディーに行いたい(GUIのブラウザだとマウスのクリックで行ける事でもあるし…) tmux-plugins/tmux-copycat: A plugin that enhances tmux searchを導入すると、prefix + ctrl-u でURLがハイライトした状態になる。 つまり、URLの上で
- prefix + ctrl-u
- o のアクションだけでw3mで開けるようになった。 ただ少し惜しいのがURLの選択範囲で、https://hogehoge.com) ← このカッコまで含まれてしまう。 markdownのURLなどが手軽に開けず、結局、tmuxで選択してから開くようになった。
ただ私の環境( Gentoo )ではデスクトップ環境を入れていないためか、下記の方法ではシンプルに動作しなかった。 上記がうまくいった方法だが、一応、記録として残しておく。
launch-w3m.desktop ファイルの作成: .desktopファイルは、アプリケーションの起動情報を記述したテキストファイルです。例えば launch-w3m.desktopを以下のように作成するアプローチもありえる。
[Desktop Entry]
Type=Application
Name=Launch W3M
Exec=/path/to/launch-w3m.sh %u
Terminal=true
MimeType=text/html;
この launch-w3m.desktop を xdg-settings を用いてデフォルトブラウザとして指定してみる。
xdg-settings set default-web-browser launch-w3m.desktop
ここでエラーになってしまう。
Gentooの場合
2023/12/11時点
emerge: there are no ebuilds built with USE flags to satisfy “>=app-text/xmlto-0.0.28-r3[text(+)]”. app-text/xmltoをtextオプションを付けてコンパイルが必要、かつX関連のパッケージのインストールがそれなりに必要。 それなり、というのはX本体ではなく周辺ツールのインストールは必要で、この辺、X本体への依存関係を入れないのはGentooが良く依存関係を見ているからだと思う。 私の環境だと xdg-utilsのインストール自体はできた。
echo "app-text/xmlto text" > /etc/portage/package.use/xmlto
設定ファイルの省略記法
.tmux.conf に記載する際に利用する set-option(セッションの設定)と set-window-option(ウィンドウの設定)は
set-option => set
set-window-option => setw
Codex の処理中表示と tmux のウィンドウタイトル
Codex CLI を tmux の外で起動すると、端末のウィンドウタイトルに ⠋ のような spinner が出ることがある。
これは Codex 固有の特別な GUI API ではなく、xterm 系で古くから使われている OSC 0/2 の terminal title 更新で実現されていると考えてよい。
# 端末の window title を変更する最小例
printf '\033]2;⠋ working...\007'
ESC ] 2 ; title BEL、または ESC ] 2 ; title ST が terminal title を更新する制御列である。
ここでの ST は string terminator で、実バイト列では ESC \ を使う。
fish の fish_title、zsh の precmd、bash の PROMPT_COMMAND などでディレクトリやコマンド名をタイトルに出す設定も、基本的には同じ層を使う。
tmux が間に入ると、見るべき値が分かれる。
set-titles: tmux が外側の terminal emulator の window title を書き換えるかどうかset-titles-string:set-titles onの時に外側へ出す title の formatallow-rename: pane 内のプログラムから tmux window name を変更することを許可するかautomatic-rename: tmux window name を現在のコマンドなどから自動更新するか#{pane_title}/#T: pane 内のプログラムが OSC title で送ってきた値#W: tmux の window name
Codex の spinner は tmux 内では #{pane_title} として受け取れる。
そのため、tmux の window name 自体を変えたい場合は allow-rename on も候補になるが、普段の window name 管理や directory 表示と衝突しやすい。
自分の設定では、通常は短縮した #{pane_current_path} と #W を表示し、Codex の pane だけ #{pane_title} を表示する方針にした。
#{?#{==:#{pane_current_command},codex},#{pane_title},#(bash ~/.config/tmux/shortpath.sh "#{pane_current_path}") #W}
実セッションでの確認例:
tmux display-message -p 'window_name=#{window_name} pane_title=#{pane_title} pane_current_command=#{pane_current_command} pane_current_path=#{pane_current_path}'
Codex 実行中は、例えば下記のように見えた。
window_name=codex pane_title=⠦ tmuxConfig pane_current_command=codex pane_current_path=/path/to/tmuxConfig
外側の terminal emulator の title も tmux から変えたいなら、下記のようにする。
ただし fish_title など shell 側の title 更新方針と衝突する可能性がある。
set -g set-titles on
set -g set-titles-string '#T'
不定な点として、Codex の実装や wrapper の差により #{pane_current_command} が codex 以外になる可能性はある。
その場合は tmux display-message -p '#{pane_current_command} #{pane_title}' で実値を見て、条件式を合わせる。
現在 pane を見ながら Codex へ相談する
prefix + C-a で現在 pane の下に Codex 用 pane を開く。
実体は ~/.config/tmux/codex-consult-current-pane.fish に置き、main.conf から次のように呼ぶ。
bind C-a run-shell -b '~/.config/tmux/codex-consult-current-pane.fish #{q:pane_id} #{q:window_id} #{q:pane_current_path}'
起動された Codex には次の環境変数を渡す。
CODEX_TMUX_SOURCE_PANE: 相談対象の pane idCODEX_TMUX_SOURCE_WINDOW: 相談対象の window idCODEX_TMUX_SOURCE_PATH: 相談対象 pane の current path
Codex 側では、ユーザーから相談や質問を受けたら、回答の前に毎回現在表示中の画面だけを取得する。
tmux capture-pane -p -e -S - -t "$CODEX_TMUX_SOURCE_PANE"
-S - は scrollback 全体ではなく、現在表示されている範囲を対象にする。
nvim の plugin、設定、shortcut、エラー相談のように、いま画面に出ている情報を見ながら話したい用途に向く。
secrets が表示されている pane では起動しない運用にする。
256 色モード対応ターミナルの場合
256 色の指定を確認する
文字色: for i in {0..255} ; do printf "\x1b[38;5;${i}mcolour${i} "; done
背景色: for i in {0..255} ; do printf "\x1b[48;5;${i}mcolour${i} "; done
tmux に 256 color の使用を強制する -2 オプションを付ける。
power-tmux が入っている時のみ設定反映 if の使い方。
.tmux.conf
if "test -d ~/work/tmux-powerline" "set-option -g status-left '#(~/work/tmux-powerline/status-left.sh)'"
コピーモード時の Next word の移動のデリミタを変更する
コピーモードでのNext wordの区切り文字を変更する
set-window-option -g word-separators ' @"=:,.()[]/'
set-window-option -ag word-separators "'"
端末エミュレータ - Sixel Graphicsを活用したアプリケーションの御紹介 - Qiita この中段にw3mimgでの表示の話がある。 そこで紹介されているのが、 arakiken / w3m / ソース / doc / README.sixel Bitbucket で、これから『この人が当てたパッチ全部』が取得できないだろうか?
~/.tmux.confにスクリプトを埋め込む
.tmux.conf内ではコメントアウトされている。zshrcが実行される際にコメントアウトの箇所がcutで削られ、実行される。
検索スクリプト
2026-05 現在、この古い例の www.google.com/search は w3m 向けの簡素表示としては使えない。
古い MSIE/w3m/Lynx 風 User-Agent では Google 側が Update your browser を返し、現代ブラウザ風 User-Agent では JavaScript / challenge / fallback 系のページになる。
tmux からテキストブラウザへ投げる検索先としては、Google ではなく DuckDuckGo Lite、Brave Search、自前 SearXNG などへ差し替える方が現実的。
#!/usr/bin/ruby
require 'pp'
require 'uri'
require 'kconv'
# if ! system("screen -ls | grep search_result")
# system("gnome-terminal -e '/usr/bin/screen -S search_result -c /usr2/home/exampleuser/dotfiles/.screenrc-fumidai zsh'")
# sleep 2
# end
$hiki_uri = URI.escape("https://www.ponyo.in/hiki/hiki.cgi?#{ARGV.join('+').toeuc}")
$google_uri = URI.escape("http://www.google.com/search?ie=utf-8&oe=utf-8&q=#{ARGV.join(' ')}")
$hatena_uri = URI.escape("http://q.hatena.ne.jp/list?word=#{ARGV.join(' ')}")
$hiki_search_uri = URI.escape("https://www.ponyo.in/hiki/hiki.cgi?c=search_orig&key=#{ARGV.join('+').toeuc}")
$cmd = "tmux send-keys -t w3m \"w3m -N \'#{$hiki_uri}\' \'#{$google_uri}\' \'#{$hatena_uri}\' \'#{$hiki_search_uri}\' \" C-m "
#pp $cmd
system("#{$cmd}")
セッションを指定してコマンドを送信
tmux send-keys -t development 'cd ~/tmp' C-m
tmux send-keys -t w3m "cd /var/tmp/" C-m
tmuxスクリプト
もっと簡単に!tmuxで複数のサーバにSSH接続して同じコマンドを一気に送る
面倒くさいことはスクリプト化します。
#!/bin/bash
tmux start-server
tmux new-session -d -n tmux-ssh-window -s tmux-ssh
for i in $*
do
tmux split-window -v -t tmux-ssh-window
tmux send-keys "ssh $i" C-m
tmux select-layout -t tmux-ssh-window main-horizontal
done
tmux kill-pane -t 0
tmux select-window -t tmux-ssh-window
tmux select-pane -t 0
tmux select-layout -t tmux-ssh-window main-horizontal
tmux set-window-option synchronize-panes on
tmux attach-session -t tmux-ssh
tmuxで、引数で与えられたホストごとにウィンドウを作成してssh接続するシェルスクリプト
#!/bin/bash
|
|SESSION_NAMEmtmux-ssh
|INITIAL_WINDOW_NAME=tmux-ssh-window
|
|### セッションの作成
|tmux start-server
|tmux new-session -d -n $INITIAL_WINDOW_NAME -s $SESSION_NAME
|
|### 引数で与えられたホストにssh接続
|for i in $*
|do
| tmux new-window -n $i
| tmux send-keys "ssh $i" C-m
| tmux swap-window -d -s $INITIAL_WINDOW_NAME -t $i
|done
|
|### 不要な初期ウィンドウを除去
|tmux kill-window -t $INITIAL_WINDOW_NAME
|tmux select-window -t $1
|
|### 作成されたセッションにアタッチ
tmux attach-session -t $SESSION_NAME
tmuxでscreenのlogに相当する機能を実現する
上記の方法でリアルタイムでtmux画面出力のロギングができる。
tmuxinator
複雑なレイアウトにしたい場合は http://tactosh.com/2014/01/tmux-window-pane-tmuxinator/
$ tmux list-window
0: window1* (7 panes) [111x36] [layout be07,111x36,0,0[111x17,0,0{55x17,0,0,26,55x17,56,0[55x8,56,0,29,55x8,56,9,30]},111x18,0,18{55x18,0,18[55x9,0,18,28,55x8,0,28,32],27x18,56,18,31,27x18,84,18,33}]] @11 (active)
layoutの所をコピーすると、複雑なレイアウトが再現できるらしい
# ~/.tmuxinator/sample.yml
name: sample
root: ~/
windows:
- window1:
layout: be07,111x36,0,0[111x17,0,0{55x17,0,0,26,55x17,56,0[55x8,56,0,29,55x8,56,9,30]},111x18,0,18{55x18,0,18[55x9,0,18,28,55x8,0,28,32],27x18,56,18,31,27x18,84,18,33}]
panes:
- pane0:
- vim
- pane1:
- vim
gemインストール
CentOS5.5でRubyGemsを利用する ぱんぴーまっしぐら
yum install ruby-devel
wget -P /usr2/archive/packages/ http://production.cf.rubygems.org/rubygems/rubygems-1.3.5.zip
cd /usr2/archive/tmp/
unzip ../packages/rubygems*
cd rubygems*
ruby setup.rb
ここでrdocインストールの失敗のエラーがでるが、gemコマンド自体はインストールされており、tmuxinatorのインストールもできる。
gemインストール後
[exampleuser@exampleuser-1]% sudo gem install tmuxinator --no-ri --no-rdoc [Line:51 Colum:273]
[sudo] password for exampleuser:
Fetching: tmuxinator-0.5.0.gem (100%)
__________________________________________________________
..........................................................
Thank you for installing tmuxinator
Please be sure to to drop a line in your ~/.bashrc file, similar
to RVM if you've used that before:
[[ -s $HOME/.tmuxinator/scripts/tmuxinator ]] && source $HOME/.tmuxinator/scripts/tmuxinator
also ensure that you've set these variables in your ENV:
$EDITOR, $SHELL
you can run `tmuxinator doctor` to make sure everything is set.
happy tmuxing with tmuxinator!
..........................................................
__________________________________________________________
Successfully installed tmuxinator-0.5.0
1 gem installed
!yamlファイルで定義する
mux new asp
ひな形のyamlファイルが見えるので、編集する
tmuxinator w3m
~/.tmuxinator/配下にymlファイルを設置する。下記のような設定を作ったポイントとしては
- socketはw3mとしている。これはtmuxを入れ子状にできるように
- w3mで開く際、『-N』オプションで複数をタブで開いている
[exampleuser@exampleuser-1] % cat .tmuxinator/w3m.yml [Line:96 Colum:153]
# ~/.tmuxinator/w3m.yml
# you can make as many tabs as you wish...
project_name: w3m
project_root: ~/tmp
socket_name: w3m
tabs:
- office: w3m -N "https://www.ponyo.in/hiki/hiki.cgi?%40Office" "https://www.ponyo.in/hiki/hiki.cgi?%B1%F3%B3%D6%C3%CF%A5%D0%A5%C3%A5%AF%A5%A2%A5%C3%A5%D7"
- 読んでまとめる: w3m -N "https://www.ponyo.in/hiki/hiki.cgi?%40Home" "https://www.ponyo.in/hiki/hiki.cgi?%C6%C9%A4%F3%A4%C7%A4%DE%A4%C8%A4%E1%A4%EB"
起動
mux asp && tmux -L foo source-file ~/.tmux_2nd.conf && tmux new-window -a "tmux -L foo a -t asp"
w3mでsocketを指定して接続。 source-fileはあるセッションの参照する設定ファイルを変更する際に使える
tmux w3m && tmux -L w3m source-file ~/.tmux_2nd.conf && tmux new-window -a "tmux -L w3m attach "
参考
tmux 3.0でコピーモードの設定を行う - りんごとバナナとエンジニア
設定時に参考になるページ
tmuxの基本的な操作方法のまとめ
忘れがちな キーバインド参考
現在のキーバインドの確認は tmux list-keys
達人に学ぶ.tmux.confの基本設定
cheatsheet
ショートカット
私のtmux.conf
日本語 manpage
ぼくのてぃーまっくす - とことんDevOps | 日本仮想化技術のDevOps技術情報メディア
下記のようにも書ける、どちらが良いだろうか。考えてみるとコピー用のコマンドが増える、変わる可能性があると、いくつかifがある上記のやり方の方が素敵だ。
# OS をチェックして適切なコピーコマンドを設定
if-shell "[[ $(uname) == 'Linux' ]]" \
"bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -selection c'" \
"bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'pbcopy'"
tmuxメモ : Tmux openでハイライトしたファイル、URLを開く - もた日記
tmux-openの記事が参考になった。
rothgar/awesome-tmux: A list of awesome resources for tmux
tmuxプラグインの一覧