LoginSignup
0
0

More than 5 years have passed since last update.

terminal の全ての tab で特定のコマンドを実行する

Posted at

概要

現在開いている terminal の全ての tab に特定のコマンドを実行させる script を書きました.
ただし以下の注意点があります.
1. Mac 専用
2. プロセス実行中の tab には適用されない

動機

そもそもこれを作ろうと思った動機は, source ~/.zshrc を全ての tab に適用したいから.
PATH や alias を割りと頻繁に更新するので, そのときに全ての tab に source ~/.zshrc が反映されないのが不便だなと思った.

手順

ではこれを実現する手順を以下に記します.

1. スクリプトを配置する dir を作成

mkdir ~/scripts

2. スクリプトを作成

~/scripts/apply_all_tabs.sh
function apply_cmd() {
  local cmd=$@

  osascript \
    -e 'tell application "Terminal"' \
      -e "do script \"$cmd\" in window 1 " \
      -e 'set terms to every window in application "Terminal"' \
      -e 'repeat with term in terms' \
        -e 'set tTabs to every tab in term' \
        -e 'repeat with tTab in tTabs' \
          -e 'if tTab is not busy then' \
            -e "do script \"$cmd\" in tTab" \
          -e 'end if' \
        -e 'end repeat' \
      -e 'end repeat' \
    -e 'end tell' 
}
apply_cmd $@

3. alias をはる (~/.bashrc or ~/.zshrc or etc.. を編集)

alias aat="~/scripts/apply_all_tabs.sh"

4. 更新

source ~/.bashrc

5. 試す

aat echo hello

↑ で全ての tab で echo hello が実行されていれば OK

0
0
1

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
0