37

我想使用一个脚本来打开一个有 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 会话?

4

6 回答 6

33

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

我已经使用了一段时间,并且在大多数情况下都有很好的体验。

于 2013-03-29T16:41:41.217 回答
24

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

正如 shellter 评论的那样,set -vx在启动序列的早期暂时包含命令是找出错误发生位置的好方法。
如果您发现-vx输出过于冗长,您可以尝试“printf 调试”(手动将调试语句添加到您的启动文件,直到您可以准确缩小导致错误的行):

  • echo start of .bashrcandecho end of .bashrc放在您的开始/结束处,.bashrc以查看错误是否在您的.bashrc. 如果没有,请检测您的其他启动文件:.bash_profile// .bash_login.profile如果错误发生在该文件之前,那么问题可能出在/etc/profile.
  • 一旦您知道发生错误时正在处理哪个文件,请在每个“主要块”或行周围添加更多调试输出,以缩小负责的部分/行。
  • 这些错误实际上可能不在您的启动文件本身中,而是在它运行的脚本中。

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


应该没有必要在提供给or的shell-commandcd参数中使用类似的命令。tmux new-sessiontmux new-window

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

†</sup> 这是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 的方式与普通的不同bash -i(它更类似于bash -l,它使用你的.bash_profile/ .bash_login/.profile而不是(只是)你的.bashrc)。

于 2011-12-19T04:52:32.113 回答
9

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

于 2013-05-09T16:40:31.937 回答
9

对于那些不安装 ruby​​ 的人, tmuxp在 Python 中可用,并且支持 Teamocil 和 Tmuxinator 样式的配置文件(在 yaml 和 json 中)。

更不用说它背后有一个非常可爱的库 https://github.com/tony/libtmux


 pip install --user tmuxp

为 python 用户安装程序添加 ~/.local/bin/ 到你的 $PATH

-

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

-

 tumxp load workbench
于 2016-08-08T21:47:41.840 回答
3

我一辈子都无法让 -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"
于 2017-10-17T22:30:37.540 回答
0

为了简单地使用多个窗格打开 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  |
-----------------------------------
于 2022-01-04T20:16:17.587 回答