bash 如何使用不同目录中的多个窗口启动 tmux?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8537149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start tmux with several windows in different directories?
提问by Michael H?rtl
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 foundand tried this first:
我想用一个脚本打开一个 tmux 会话,它有 6 个窗口,每个窗口都在不同的目录中。我从我找到的一个脚本开始,并首先尝试了这个:
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.
但是我很快发现这不会像我预期的那样工作——shell 命令完成后窗口将关闭。
So my next idea was to start a new shell like this:
所以我的下一个想法是像这样启动一个新的 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
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:
这几乎有效。但是如果我启动超过 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
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?
我不知道为什么会发生这种情况,但我仍然认为我做得不对。有没有更好的方法来在多个目录上设置 tmux 会话?
采纳答案by Chris Johnsen
The shell errors are probably due to some problem in your startup files (or something they run).
shell 错误可能是由于您的启动文件(或它们运行的某些文件)中的某些问题造成的。
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):
正如 shellter 所评论的,set -vx
在启动序列的早期临时包含该命令是找出错误发生位置的好方法。
如果您发现-vx
输出过于冗长,您可以尝试“printf 调试”(手动将调试语句添加到您的启动文件,直到您可以准确缩小导致错误的行):
- Put
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
. - 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.
- 将
echo start of .bashrc
和echo end of .bashrc
放在您的开始/结束处,.bashrc
以查看在您的.bashrc
. 如果没有,你的仪表等启动文件:.bash_profile
/.bash_login
/.profile
。如果错误发生在该文件之前,则问题可能出在/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.
注意:这些调试添加需要是临时的,因为如果您使用自动登录的程序(例如rsync、基于 SSH 的 Git 访问等),它们会导致问题,因为这些程序期望“干净”的连接而没有这种调试噪音展示。
There should be no need to use cd
command like that in the shell-commandargument given to either tmux new-session
or tmux new-window
.
应该不需要cd
像在shell-command参数中给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 tmux0.6 (at least for new-window
).
新窗口会“继承”吗?使用new-session
和new-window
从命令行(即通过tmux
二进制文件完成,而不是通过绑定或在tmux-:
提示符下)时的当前工作目录。根据 CHANGES 文件,从tmux0.6开始似乎就是这种情况(至少对于new-window
)。
?This is tmux-mediated inheritance, not the parent–child inheritance that is the usual mechanism for passing along the cwd.
? 这是tmux介导的继承,而不是作为传递 cwd 的常用机制的父子继承。
This script works for me with tmux1.5:
这个脚本适用于tmux1.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 tmuxstarts 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
).
因为这样,这可能也是(作为副作用)减轻你的shell启动的错误TMUX启动一个外壳是由一个普通的不同bash -i
(它更像是bash -l
,它使用你的.bash_profile
/ .bash_login
/ .profile
,而不是(只是)你.bashrc
)。
回答by Will
Tmuxinatoris also really good for this. Basically you create setup files like so:
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
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.
我已经使用它一段时间了,并且在很大程度上获得了很好的体验。
回答by Sam Stoelinga
回答by ThorSummoner
For thous who don't install rubytmuxpis available in Python and supports both Teamocil and Tmuxinator style configuration files (in both yaml and json).
对于那些没有安装 ruby 的人,tmuxp在 Python 中可用,并且支持 Teamocil 和 Tmuxinator 风格的配置文件(在 yaml 和 json 中)。
- Source https://github.com/tony/tmuxp
- Docs http://tmuxp.readthedocs.io/en/latest/
- Pypi https://pypi.python.org/pypi/tmuxp/1.0.0
- 来源https://github.com/tony/tmuxp
- 文档http://tmuxp.readthedocs.io/en/latest/
- Pypi https://pypi.python.org/pypi/tmuxp/1.0.0
Not to mention there is a very sweet library behind it all https://github.com/tony/libtmux
更不用说它背后有一个非常可爱的库 https://github.com/tony/libtmux
pip install --user tmuxp
add ~/.local/bin/ to your $PATH for python user install programs
将 ~/.local/bin/ 添加到您的 $PATH 以用于 python 用户安装程序
-
——
# ~/.tmuxp/workbench.yaml
session_name: workbench
windows:
- name: workbench
layout: main-vertical
panes:
- vim
- pane
- pane
-
——
tumxp load workbench
回答by Neil McGill
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:
我一生都无法让 -c 工作,所以我通过创建一个通过发送运行的脚本来解决这个问题 - 这使我可以在每个 tmux 会话中做任何我想做的事情。如果它帮助别人,这里是:
#!/bin/bash
TMUX_SESSION=mystuff
TOP=~/mydir
tmux new-session -s "$TMUX_SESSION" -n $(pwd) -d
launch_my_window()
{
WINDOW=
NAME=
SUBDIR=
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"