如何创建已打开多个窗口的 tmux 会话?

Nes*_*ess 4 bash tmux

我已经尝试了几乎可以在网上找到的所有内容,但没有任何效果。我尝试了以下方法,通常的结果是一个只有一个窗口的新 tmux 会话。



只需在 .bashrc 中。

.bashrc

tmx () {
    tmux new-session -A -s SessionName
    tmux new-window -n Win1
    tmux new-window -n Win2
    tmux new-window -n Win3
    tmux new-window -n Win4
    tmux attach-session -d -t SessionName # with and without this line
    tmux select-window -t Win1 # with and without this line
}
Run Code Online (Sandbox Code Playgroud)

再次仅在 .bashrc 中。

.bashrc

tmx () {
    tmux new-session -A -s SessionName ||
    tmux \
        neww -s Win1 \; \
        neww -s Win2 \; \
        neww -s Win3 \; \
        neww -s Win4 \; \
        selectw -t Win1
}
Run Code Online (Sandbox Code Playgroud)

以下尝试将是我的首选方法,因为它对我来说最有意义。

在没有第一行的情况下调用 tmux 会使所有其他行导致“找不到会话”错误发生。这是没有意义的,因为我们不应该调用 tmux 来访问这些东西吗?我最初的计划是创建一个会话并让这个文件自动设置我的 tmux。

.tmux.conf

new-session -A -s SessionName
new-window -t Win1
new-window -t Win2
new-window -t Win3
new-window -t Win4
attach-session -d -t SessionName # with and without this line
select-window -t Win1 # with and without this line
Run Code Online (Sandbox Code Playgroud)

这种方法,无论是使用别名还是制作函数,通常都会导致“连接服务器失败”。但是当摆弄它足以让它不发生时,它会产生与其他相同的结果。

.bashrc

alias tmx='tmux source-file "~/.tmux/mysession"'
Run Code Online (Sandbox Code Playgroud)

.tmux/mysession

new-session -A -s SessionName
new-window -t Win1
new-window -t Win2
new-window -t Win3
new-window -t Win4
attach-session -d -t SessionName # with and without this line
select-window -t Win1 # with and without this line
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

che*_*ner 9

您需要在分离模式 ( -d) 中创建会话;否则,您的脚本会阻塞,直到您与新会话分离。同样,您的脚本将在tmux attach-session您分离之前阻塞,因此您需要先选择正确的窗口。请注意,您可以-d使用new-window避免使每个新窗口成为当前窗口,完全不需要调用select-window

是的,-d经常使用。

tmx () {
    # Use -d to allow the rest of the function to run
    tmux new-session -d -s SessionName
    tmux new-window -n Win1
    # -d to prevent current window from changing
    tmux new-window -d -n Win2
    tmux new-window -d -n Win3
    tmux new-window -d -n Win4
    # -d to detach any other client (which there shouldn't be,
    # since you just created the session).
    tmux attach-session -d -t SessionName
}
Run Code Online (Sandbox Code Playgroud)