如何编写 tmux 脚本,使其自动分割窗口并打开一组文件?

mle*_*312 2 tmux

我是 Tmux 新手。我知道您可以编写脚本来自动化 Tmux,以防您的计算机关闭。我可以写以下内容:

#!/bin/bash

tmux new-session -d -s MY_SESSION_NAME
tmux split-window -h
vim <path to file1>
Run Code Online (Sandbox Code Playgroud)

这只会为 file1 打开一个单独的 vim 编辑器页面,而不是在 tmux 中,也不在任何 tmux split 中。是否可以像这样自动打开文件?

jer*_*ile 5

下面是我的 tmux 会话中的自动化示例。使用 来定位特定会话/窗格/窗口<name>:<window>.<pane>,其中窗口和窗格均从 0 开始编号。 Send-keys是将命令发送到特定 tmux 窗格/窗口的方法。这-d会导致 tmux 以分离模式启动,因此您可以在实际附加之前继续向其发送更多命令。

tmuxstart() {
    tmux new-session -d -s sess >/dev/null
    tmux rename-window -t sess:0 'main'
    tmux splitw -v -p 10 -t sess:0.0
    tmux splitw -h -p 80 -t sess:0.1
    #required; otherwise pane numbering is bs
    tmux select-pane -t sess:0.0
    tmux splitw -h -p 5 -t sess:0.0
    tmux send-keys -t sess:0.2 'sudo htop' Enter
    tmux send-keys -t sess:0.1 'tmux clock -t sess:0.1' Enter
    tmux select-pane -t sess:0.0
    tmux new-window -t sess
    tmux rename-window -t sess:1 'second'
    tmux splitw -v -p 10 -t sess:1.0
    tmux splitw -h -p 80 -t sess:1.1
    tmux select-pane -t sess:1.0
    tmux splitw -h -p 5 -t sess:1.0
    tmux clock -t sess:1.1
    tmux new-window -t sess
    tmux rename-window -t sess:2 'scratch'
    tmux splitw -v -p 10 -t sess:2.0
    tmux select-pane -t sess:2.0
    tmux splitw -h -p 5 -t sess:2.0
    tmux clock -t sess:2.1
    tmux select-window -t sess:0.0
    tmux a -t sess
}
Run Code Online (Sandbox Code Playgroud)