tmux関連

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を 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/kaoru/.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の上で

  1. prefix + ctrl-u
  2. 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

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で削られ、実行される。

検索スクリプト

#!/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/inoue/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インストール後

[inoue@inoue-1]% sudo gem install tmuxinator --no-ri --no-rdoc                                                                                                                                                                                            [Line:51 Colum:273]
[sudo] password for inoue:
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』オプションで複数をタブで開いている
[inoue@inoue-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の基本設定

cheetsheet

ショートカット

私の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プラグインの一覧