【问题标题】:How do I create a tmux session with multiple windows already opened?如何创建已打开多个窗口的 tmux 会话?
【发布时间】:2018-02-26 22:09:55
【问题描述】:

我已经尝试了几乎所有可以在网上找到的东西,但没有任何效果。我尝试了以下方法,通常的结果是一个只有一个窗口的新 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
}


同样只在 .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
}


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

在没有第一行的情况下调用 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


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

.bashrc

alias tmx='tmux source-file "~/.tmux/mysession"'

.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


我做错了什么?

【问题讨论】:

    标签: bash tmux


    【解决方案1】:

    您需要在分离模式下创建会话(-d);否则,您的脚本会阻塞,直到您从新会话中分离出来。同样,您的脚本将在tmux attach-session 之后阻塞,直到您分离,因此您需要先选择正确的窗口。请注意,您可以使用-dnew-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
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-03
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 2019-01-17
      • 2018-06-08
      相关资源
      最近更新 更多