0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

tmuxで一時的に作成した別paneでコマンドを実行する

Last updated at Posted at 2023-06-19

はじめに

tmuxを使っている時、「今のpaneを維持したまま、コマンドを別paneで簡単に実行したい」と思ったので実装してみました。

動作環境

バージョン
OS Mac OS X 13.4 Ventura
shell zsh 5.9 (x86_64-apple-darwin22.1.0)
tmux tmux 3.3a

実装

sp, spl, vsp, vsplの4つのコマンドを実装しました。

# tmuxで新しくペインを作成してコマンドを実行
# 水平分割
function sp() {
  _sp "$*"
}

# 水平分割してless
function spl() {
  _sp "$* | less"
}

function _sp() {
  tmux split-window "$*"
}

# 垂直分割
function vsp() {
  _vsp "$*"
}

# 垂直分割してless
function vspl() {
  _vsp "$* | less"
}

function _vsp() {
  tmux split-window -h "$*"
}

実装の内容としては、コマンドの引数をそのまま tmux split-window に渡して実行しています。

sp, spl がpaneの水平分割、 vsp , vspl が垂直分割です。
lがついているものとついていないものの差は、入力したコマンドに対して less をかけているかどうかです。
なぜ less をかけているかというと、 tmux split-window に渡したコマンドがすぐに終わってしまうコマンドだと一瞬でpaneが終了してしまい、内容を確認できないためです。
man ls のようにユーザー側でアクションを起こさないと終了しないコマンドなら spvsp でよいですが、 ls のように勝手に終了するコマンドの場合は splvsplless をかけてやる必要があります。

完成!

tmux-split-ex.gif

こんな感じです。
一時的にpaneを分割し、分割したpaneでコマンドを実行し、コマンドが終了するとpaneが閉じます。

終わりに

今までは別paneでコマンドを実行したい場合は

  1. paneを分割
  2. そのpaneでコマンドを実行
  3. paneを閉じる

の手順が必要でしたが、その手間が省けるようになりました。

だいたい望み通りの挙動をしているのですが、あとはコマンドが色のついたテキストを出力する場合に色がなくなってしまうのをなんとかしたいなと思います。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?