如何在不同目录中启动多个窗口的 tmux?

发布于 2024-12-21 12:05:35 字数 1464 浏览 3 评论 0原文

我想使用脚本打开一个有 6 个窗口的 tmux 会话,每个窗口位于不同的目录中。我从 我找到的一个脚本并首先尝试了这个:

tmux new-session -s xyz   -n etc -d 'cd /etc'
tmux new-window  -t xyz:1 -n var    'cd /var/log'

但我很快发现这不会像我预期的那样工作 - 窗口将在 shell 命令完成后关闭。

所以我的下一个想法是启动一个像这样的新 shell:

tmux new-session -s xyz   -n etc -d 'cd /etc; bash -i'
tmux new-window  -t xyz:1 -n var    'cd /var/log; bash -i'
tmux new-window  -t xyz:2 -n var2   'cd /var/log; bash -i'
tmux new-window  -t xyz:3 -n var3   'cd /var/log; bash -i'
tmux new-window  -t xyz:4 -n var4   'cd /var/log; bash -i'
tmux new-window  -t xyz:5 -n var5   'cd /var/log; bash -i'
tmux new-window  -t xyz:6 -n var6   'cd /var/log; bash -i'

tmux select-window -t xyz:1
tmux -2 attach-session -t xyz

这几乎可以工作。但是,如果我启动超过 4 个窗口,启动后我经常会在其中一个窗口中看到以下 bash 错误:

bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected

我不知道为什么会发生这种情况,但我仍然认为我做得不对。有没有更好的方法在多个目录上设置 tmux 会话?

I want to use a script to open a tmux session with 6 windows, each in a different directory. I started with a script I found and tried this first:

tmux new-session -s xyz   -n etc -d 'cd /etc'
tmux new-window  -t xyz:1 -n var    'cd /var/log'

But I soon found out that this will not work as I expected — the window will be closed after the shell command completes.

So my next idea was to start a new shell like this:

tmux new-session -s xyz   -n etc -d 'cd /etc; bash -i'
tmux new-window  -t xyz:1 -n var    'cd /var/log; bash -i'
tmux new-window  -t xyz:2 -n var2   'cd /var/log; bash -i'
tmux new-window  -t xyz:3 -n var3   'cd /var/log; bash -i'
tmux new-window  -t xyz:4 -n var4   'cd /var/log; bash -i'
tmux new-window  -t xyz:5 -n var5   'cd /var/log; bash -i'
tmux new-window  -t xyz:6 -n var6   'cd /var/log; bash -i'

tmux select-window -t xyz:1
tmux -2 attach-session -t xyz

This almost works. But if I start more than about 4 windows, I frequently see the following bash errors in one of the windows after startup:

bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected

I have no idea why this happens, but I still think that I’m not doing this right. Is there a better way to set up a tmux session over several directories?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

似梦非梦 2024-12-28 12:05:35

Tmuxinator 对此也非常有用。基本上,您可以像这样创建安装文件:

# ~/.tmuxinator/project_name.yml
# you can make as many tabs as you wish...

project_name: Tmuxinator
project_root: ~/code/rails_project
socket_name: foo # Not needed. Remove to use default socket
rvm: 1.9.2@rails_project
pre: sudo /etc/rc.d/mysqld start
tabs:
  - editor:
      layout: main-vertical
      panes:
        - vim
        - #empty, will just run plain bash
        - top
  - shell: git pull
  - database: rails db
  - server: rails s
  - logs: tail -f logs/development.log
  - console: rails c
  - capistrano:
  - server: ssh me@myhost

然后您可以开始一个新会话:

mux project_name

我已经使用它有一段时间了,并且在很大程度上获得了良好的体验。

Tmuxinator is also really good for this. Basically you create setup files like so:

# ~/.tmuxinator/project_name.yml
# you can make as many tabs as you wish...

project_name: Tmuxinator
project_root: ~/code/rails_project
socket_name: foo # Not needed. Remove to use default socket
rvm: 1.9.2@rails_project
pre: sudo /etc/rc.d/mysqld start
tabs:
  - editor:
      layout: main-vertical
      panes:
        - vim
        - #empty, will just run plain bash
        - top
  - shell: git pull
  - database: rails db
  - server: rails s
  - logs: tail -f logs/development.log
  - console: rails c
  - capistrano:
  - server: ssh me@myhost

Then you can start a new session with:

mux project_name

I've been using it for a while and have had a good experience for the most part.

水染的天色ゝ 2024-12-28 12:05:35

shell 错误可能是由于启动文件(或它们运行的​​某些文件)中的某些问题造成的。

正如 shellter 所评论的,在启动序列的早期阶段暂时包含命令 set -vx 是找出错误发生位置的好方法。
直到您可以准确缩小导致错误的行范围):

  • 如果您发现 -vx 输出过于冗长,您可以尝试“printf 调试”(手动将调试语句添加到启动文件中, >echo start of .bashrc 和 echo end of .bashrc.bashrc 的开头/结尾处查看 期间是否发生错误.bashrc。如果没有,请检测其他启动文件:.bash_profile/.bash_login/.profile。如果错误发生在该文件之前,则问题可能出在 /etc/profile 中。
  • 一旦您知道发生错误时正在处理哪个文件,请在每个“主要块”或行周围添加更多调试输出,以缩小负责的部分/行的范围。
  • 这些错误实际上可能不在启动文件本身中,而是在它运行的脚本中。

注意:这些调试添加需要是临时的,因为如果您使用自动登录的程序(例如rsync、基于 SSH 的 Git 访问等),它们会导致问题,因为这些程序需要“干净的”连接,不存在这种调试噪音。


不需要使用像在为 tmux new-sessiontmux 提供的 shell-command 参数中那样的 cd 命令新窗口

当从命令行使用 new-sessionnew-window 时(即完成后),新窗口将“继承”当前工作目录通过 tmux 二进制文件,而不是通过绑定或在 tmux-: 提示符处)。根据 CHANGES 文件,从 tmux 0.6 开始(至少对于 new-window 来说)似乎就是这种情况。

这是 tmux 介导的继承,而不是传递 cwd 的通常机制的父子继承。

该脚本适用于 tmux 1.5:

#!/bin/bash
# var for session name (to avoid repeated occurences)
sn=xyz

# Start the session and window 0 in /etc
#   This will also be the default cwd for new windows created
#   via a binding unless overridden with default-path.
cd /etc
tmux new-session -s "$sn" -n etc -d

# Create a bunch of windows in /var/log
cd /var/log
for i in {1..6}; do
    tmux new-window -t "$sn:$i" -n "var$i"
done

# Set the default cwd for new windows (optional, otherwise defaults to session cwd)
#tmux set-option default-path /

# Select window #1 and attach to the session
tmux select-window -t "$sn:1"
tmux -2 attach-session -t "$sn"

这也可能(作为副作用)减轻您的 shell 启动错误,因为 tmux 启动 shell 的方式与普通 shell 不同bash -i (它更类似于 bash -l,它使用您的 .bash_profile/.bash_login/ .profile 而不是(只是)你的.bashrc)。

The shell errors are probably due to some problem in your startup files (or something they run).

As shellter commented, temporarily including the command set -vx early in your startup sequence is a good way to find out where the errors are occurring.
If you find the -vx output too verbose, you could try “printf debugging” (manually adding debug statements to your startup files until you can narrow down exactly which lines are causing the errors):

  • Put echo start of .bashrc and echo end of .bashrc at the start/end of your .bashrc to see if the error occurs during your .bashrc. If not, instrument your other startup files: .bash_profile/.bash_login/.profile. If the errors happen before that file, then the problem may be in /etc/profile.
  • Once you know which file is being processed when the errors occur, add more debug outputs around each “major block” or line to narrow down the responsible section/line.
  • The errors may not actually be in your startup file itself, but in a script that it runs.

Note: These debug additions need to be temporary since they will cause problems if you ever use a program that makes automated logins (e.g. rsync, SSH-based Git access, etc.) since these programs expect a “clean” connection without such debugging noise present.


There should be no need to use cd command like that in the shell-command argument given to either tmux new-session or tmux new-window.

A new window will “inherit” the current working directory when using new-session and new-window from the command line (i.e. when done through the tmux binary, instead of via a binding or at a tmux-: prompt). According to the CHANGES file, it looks like this has been the case since tmux 0.6 (at least for new-window).

This is tmux-mediated inheritance, not the parent–child inheritance that is the usual mechanism for passing along the cwd.

This script works for me with tmux 1.5:

#!/bin/bash
# var for session name (to avoid repeated occurences)
sn=xyz

# Start the session and window 0 in /etc
#   This will also be the default cwd for new windows created
#   via a binding unless overridden with default-path.
cd /etc
tmux new-session -s "$sn" -n etc -d

# Create a bunch of windows in /var/log
cd /var/log
for i in {1..6}; do
    tmux new-window -t "$sn:$i" -n "var$i"
done

# Set the default cwd for new windows (optional, otherwise defaults to session cwd)
#tmux set-option default-path /

# Select window #1 and attach to the session
tmux select-window -t "$sn:1"
tmux -2 attach-session -t "$sn"

This might also (as a side-effect) alleviate your shell startup errors since the way tmux starts a shell is different from a plain bash -i (it is more akin to bash -l, which uses your .bash_profile/.bash_login/.profile instead of (just) your .bashrc).

凌乱心跳 2024-12-28 12:05:35

对于那些没有 ruby​​ 的人 tmuxp 在 Python 中可用并支持Teamocil 和 Tmuxinator 风格的配置文件(yaml 和 json 格式)。

更不用说还有一个非常贴心的库这一切的背后 https://github.com/tmux-python/libtmux

pip:

pip install --user tmuxp

上图:将 ~/.local/bin/ 添加到您的 $PATH 中,用于 python 用户安装程序

apt (Debian / Ubuntu / Mint):

sudo apt install tmuxp

Brew:

brew install tmuxp

配置

# ~/.tmuxp/workbench.yaml
session_name: workbench
windows:
- name: workbench
  layout: main-vertical
  panes:
  - vim
  - pane
  - pane
tmuxp load workbench

您可以直接加载会话:例如 tmuxp load ./myworkspace.yaml

最后,在基于项目的配置中:

# tmuxp file aat project/.tmuxp.yaml
tmuxp load ./project

请参阅有关 tmuxp load 了解更多信息。

更多配置示例:https://tmuxp.git-pull.com/configuration/examples。 html

For those who don't have ruby tmuxp is available in Python and supports both Teamocil and Tmuxinator style configuration files (in both yaml and json).

Not to mention there is a very sweet library behind it all https://github.com/tmux-python/libtmux

pip:

pip install --user tmuxp

Above: Add ~/.local/bin/ to your $PATH for python user install programs

apt (Debian / Ubuntu / Mint):

sudo apt install tmuxp

Brew:

brew install tmuxp

Configuration

# ~/.tmuxp/workbench.yaml
session_name: workbench
windows:
- name: workbench
  layout: main-vertical
  panes:
  - vim
  - pane
  - pane
tmuxp load workbench

You can load sessions directly: e.g. tmuxp load ./myworkspace.yaml

And finally, in your project-based config:

# tmuxp file aat project/.tmuxp.yaml
tmuxp load ./project

See documentation on tmuxp load for more information.

More configuration examples: https://tmuxp.git-pull.com/configuration/examples.html

会发光的星星闪亮亮i 2024-12-28 12:05:35

您可以使用 Teamocil 代替。 Teamocil 是一个简单的工具,用于使用 YAML 文件在 tmux 中自动创建会话、窗口和窗格。

You can use Teamocil instead. Teamocil is a simple tool used to automatically create sessions, windows and panes in tmux with YAML files.

生生漫 2024-12-28 12:05:35

我一生都无法让 -c 工作,所以我通过创建一个通过 send 运行的脚本来解决这个问题 - 这允许我在每个 tmux 会话中做任何我想做的事情。如果它对其他人有帮助,这里是:

#!/bin/bash
TMUX_SESSION=mystuff
TOP=~/mydir

tmux new-session -s "$TMUX_SESSION" -n $(pwd) -d

launch_my_window()
{
    WINDOW=$1
    NAME=$2
    SUBDIR=$3

    SCRIPT=$TMPDIR/tmux.sh.$WINDOW.$NAME
    tmux new-window -t "$TMUX_SESSION:$WINDOW" -n "$NAME"
    cat >$SCRIPT <<%%
cd $TOP/$SUBDIR
# do more stuff here
%%
    chmod +x $SCRIPT
    tmux send -t "$TMUX_SESSION:$WINDOW" $SCRIPT ENTER
    sleep 1
}

launch_my_window 1 "stuff"         subdir1
launch_my_window 2 "morestuff"     subdir2
launch_my_window 3 "yetmorestuff"  subdir3
#...

# Select window #1 and attach to the WINDOW
tmux select-window -t "$TMUX_SESSION:1"
tmux -2 attach-session -t "$TMUX_SESSION"

I could not for the life of me get -c to work, so I worked around this by creating a script that is ran via send - this allows me to do whatever I want in each tmux session. In case it helps someone else, here it is:

#!/bin/bash
TMUX_SESSION=mystuff
TOP=~/mydir

tmux new-session -s "$TMUX_SESSION" -n $(pwd) -d

launch_my_window()
{
    WINDOW=$1
    NAME=$2
    SUBDIR=$3

    SCRIPT=$TMPDIR/tmux.sh.$WINDOW.$NAME
    tmux new-window -t "$TMUX_SESSION:$WINDOW" -n "$NAME"
    cat >$SCRIPT <<%%
cd $TOP/$SUBDIR
# do more stuff here
%%
    chmod +x $SCRIPT
    tmux send -t "$TMUX_SESSION:$WINDOW" $SCRIPT ENTER
    sleep 1
}

launch_my_window 1 "stuff"         subdir1
launch_my_window 2 "morestuff"     subdir2
launch_my_window 3 "yetmorestuff"  subdir3
#...

# Select window #1 and attach to the WINDOW
tmux select-window -t "$TMUX_SESSION:1"
tmux -2 attach-session -t "$TMUX_SESSION"
怎会甘心 2024-12-28 12:05:35

为了简单地使用多个窗格打开 tmux 并运行一些命令,我​​创建了以下 bash 文件:

#!/bin/bash
tmux split-window -v -p 30 -c ~/path/to/folder1
tmux split-window -h -p 66 -c ~/path/to/folder2
tmux split-window -h -p 50 'mongod'

运行 bash 文件以获取以下内容:

-----------------------------------
|                                 |
|---------------------------------|
|  folder1  | folder2  |  mongod  |
-----------------------------------

To simply open tmux with multiple panes and run some commands, I created the following bash file:

#!/bin/bash
tmux split-window -v -p 30 -c ~/path/to/folder1
tmux split-window -h -p 66 -c ~/path/to/folder2
tmux split-window -h -p 50 'mongod'

Run the bash file to get the following:

-----------------------------------
|                                 |
|---------------------------------|
|  folder1  | folder2  |  mongod  |
-----------------------------------
相思碎 2024-12-28 12:05:35

这对我有用。每个窗格在不同的目录中打开

alias tmx='tmux new-session -c ~/Pictures \; split-window -v -c /intranet/homenote/ \; split-window -h -c ~/Videos/ \;'

This works for me . Each pane opens in a different directory

alias tmx='tmux new-session -c ~/Pictures \; split-window -v -c /intranet/homenote/ \; split-window -h -c ~/Videos/ \;'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文