如何在不同目录中启动多个窗口的 tmux?
我想使用脚本打开一个有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Tmuxinator 对此也非常有用。基本上,您可以像这样创建安装文件:
然后您可以开始一个新会话:
我已经使用它有一段时间了,并且在很大程度上获得了良好的体验。
Tmuxinator is also really good for this. Basically you create setup files like so:
Then you can start a new session with:
I've been using it for a while and have had a good experience for the most part.
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-session
或tmux 提供的 shell-command 参数中那样的
。cd
命令新窗口当从命令行使用
new-session
和new-window
时(即完成后),新窗口将“继承”†当前工作目录通过tmux
二进制文件,而不是通过绑定或在 tmux-:
提示符处)。根据 CHANGES 文件,从 tmux 0.6 开始(至少对于 new-window 来说)似乎就是这种情况。† 这是 tmux 介导的继承,而不是传递 cwd 的通常机制的父子继承。
该脚本适用于 tmux 1.5:
这也可能(作为副作用)减轻您的 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):echo start of .bashrc
andecho 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
.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 eithertmux new-session
ortmux new-window
.A new window will “inherit”† the current working directory when using
new-session
andnew-window
from the command line (i.e. when done through thetmux
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 fornew-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:
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 tobash -l
, which uses your.bash_profile
/.bash_login
/.profile
instead of (just) your.bashrc
).对于那些没有 ruby 的人tmuxp 在 Python 中可用并支持Teamocil 和 Tmuxinator 风格的配置文件(yaml 和 json 格式)。更不用说还有一个非常贴心的库这一切的背后 https://github.com/tmux-python/libtmux
pip:
上图:将
~/.local/bin/
添加到您的$PATH
中,用于 python 用户安装程序apt (Debian / Ubuntu / Mint):
Brew:
配置
您可以直接加载会话:例如
tmuxp load ./myworkspace.yaml
最后,在基于项目的配置中:
请参阅有关
tmuxp load
了解更多信息。更多配置示例:https://tmuxp.git-pull.com/configuration/examples。 html
For those who don't have rubytmuxp 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:
Above: Add
~/.local/bin/
to your$PATH
for python user install programsapt (Debian / Ubuntu / Mint):
Brew:
Configuration
You can load sessions directly: e.g.
tmuxp load ./myworkspace.yaml
And finally, in your project-based config:
See documentation on
tmuxp load
for more information.More configuration examples: https://tmuxp.git-pull.com/configuration/examples.html
您可以使用 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.
我一生都无法让 -c 工作,所以我通过创建一个通过 send 运行的脚本来解决这个问题 - 这允许我在每个 tmux 会话中做任何我想做的事情。如果它对其他人有帮助,这里是:
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:
为了简单地使用多个窗格打开 tmux 并运行一些命令,我创建了以下 bash 文件:
运行 bash 文件以获取以下内容:
To simply open tmux with multiple panes and run some commands, I created the following bash file:
Run the bash file to get the following:
这对我有用。每个窗格在不同的目录中打开
This works for me . Each pane opens in a different directory