我如何设置tmux,使它启动与指定的窗口打开?


当前回答

我已经创建了这个脚本。它不需要tmuxinator, ruby或其他。它只是一个bash脚本,可配置:

一个名为config的文件应该包含如下内容:

combo=()
combo+=('logs' 'cd /var/log; clear; pwd')
combo+=('home' 'cd ~; clear; pwd')

bash代码应该是:

#!/bin/bash

if [ -r config ]; then
    echo ""
    echo "Loading custom file"
    . config
else
    . config.dist
fi

tmux start-server

window=0
windownumber=-1

for i in "${combo[@]}"; do

    if [ $((window%2)) == 0 ]; then
        name=${i}
        ((windownumber++))
    else
        command=${i}
    fi

    if [ ${combo[0]} == "${i}" ]; then
        tmux new-session -d -s StarTmux -n "${name}"
    else
        if [ $((window%2)) == 0 ]; then
            tmux new-window -tStarTmux:$windownumber -n "${name}"
        fi
    fi

    if [ $((window%2)) == 1 ]; then
        tmux send-keys -tStarTmux:$windownumber "${command}" C-m
    fi

    ((window++))
done

tmux select-window -tStarTmux:0
tmux attach-session -d -tStarTmux

其他回答

tmuxp支持JSON或YAML会话配置和python API。一个用YAML语法创建新会话的简单tmuxp配置文件是:

session_name: 2-pane-vertical
windows:
  - window_name: my test window
    panes:
      - pwd
      - pwd

试试兄弟,这是一个项目引导器。

它提供了与tmux交互的简单api。

它看起来是这样的:

#!/bin/sh
# @ project_name/tasks/init.sh
structure project_name
  window editor
    run 'vim'
  window terminal
    run 'echo happy coding ...'
focus editor
connect project_name

要启动一个项目,所有人需要做的是运行以下命令-

兄弟开始<project_name>

检查下面的截屏,它展示了如何开始与兄弟和使用它的tmux api。

https://user-images.githubusercontent.com/8296449/50532502-2cf3e400-0b6e-11e9-84af-c5f77a04e5cc.gif

从我的“得到”。脚本,我每天早上都会调用它来运行一堆后续的“get”。XXX”的任务来刷新我跟踪的软件。有些是自动退出。其他的则需要在get完成后进行更多的交互(比如请求构建emacs)。

#!/bin/sh
tmux att -t get ||
tmux \
  new -s get -n capp \; \
  send-keys 'get.capp' C-m \; \
  neww -n emacs \; \
  send-keys 'get.emacs' C-m \; \
  neww -n git \; \
  send-keys 'get.git' C-m \; \
  neww -n mini \; \
  send-keys 'get.mini' C-m \; \
  neww -n port \; \
  send-keys 'get.port' C-m \; \
  neww -n rakudo \; \
  send-keys 'get.rakudo' C-m \; \
  neww -n neil \; \
  send-keys 'get.neil && get.neil2 && exit' C-m \; \
  neww -n red \; \
  send-keys 'get.red && exit' C-m \; \
  neww -n cpan \; \
  send-keys 'get.cpan && exit' C-m \; \
  selectw -t emacs

我已经创建了这个脚本。它不需要tmuxinator, ruby或其他。它只是一个bash脚本,可配置:

一个名为config的文件应该包含如下内容:

combo=()
combo+=('logs' 'cd /var/log; clear; pwd')
combo+=('home' 'cd ~; clear; pwd')

bash代码应该是:

#!/bin/bash

if [ -r config ]; then
    echo ""
    echo "Loading custom file"
    . config
else
    . config.dist
fi

tmux start-server

window=0
windownumber=-1

for i in "${combo[@]}"; do

    if [ $((window%2)) == 0 ]; then
        name=${i}
        ((windownumber++))
    else
        command=${i}
    fi

    if [ ${combo[0]} == "${i}" ]; then
        tmux new-session -d -s StarTmux -n "${name}"
    else
        if [ $((window%2)) == 0 ]; then
            tmux new-window -tStarTmux:$windownumber -n "${name}"
        fi
    fi

    if [ $((window%2)) == 1 ]; then
        tmux send-keys -tStarTmux:$windownumber "${command}" C-m
    fi

    ((window++))
done

tmux select-window -tStarTmux:0
tmux attach-session -d -tStarTmux

创建新会话时,可以使用tmux钩子执行命令。具体来说,这是使用会话创建的钩子实现的。

例如,你可以在~/.tmux.conf文件中添加以下代码,将屏幕分成三部分,top在右下角运行:

set-hook -g session-created 'split -h ; split -v top'

这种方法的优点是,您不必以任何特殊的方式(即shell脚本或别名)运行tmux来获得所需的结果。您还可以将其与tmux new-session -A -s mysession结合使用,这样钩子命令只在您第一次创建会话时运行,而不是在后续的附件上运行。

该功能是由d15d54c2c在2016年添加的。包含它的第一个发行版是2.4。