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


当前回答

你应该在你的tmux配置文件(~/.tmux.conf)中指定它,例如:

new mocp
neww mutt

new -d
neww
neww

(打开一个会话,2个窗口,第一个启动mocp,第二个启动mutt,另一个分离会话,3个空窗口)。

其他回答

我知道我的解决方案和其他人的很相似,但我找不到任何一个完全符合我的要求的解决方案:

我想在特定的路径上打开标题窗口 我想要尽可能多的窗户和玻璃 我想在每个窗格中运行任意命令,保存在历史记录中 我想要聚焦在脚本末尾的窗口 我希望能快速生成这个 我希望它易于维护

对于我名为johndoe的项目,我创建了一个johndoe.conf文件,它本质上是一个bash脚本,在我的配置(~/.config/tmux/tmux-sessions/johndoe.conf)的某处。

这个文件很容易维护,因为它不像我在其他答案中看到的那样有无数的\ everywhere:

# Create a new session named johndoe, with a first window named Main 
# at the specified starting path.
# The -d is for detached mode, which allows me to continue defining the rest of the session 
# before attaching to it. Without -d, tmux would open the client right away and
# ignore the rest of the session definition
tmux new -d -s johndoe -n 'Main' -c ~/dev/own/johndoe
# Simulate the user entering some docker command in the first window available 
# in the target session named (-t) johndoe
tmux send -t johndoe 'docker compose up -d' Enter

# Create a new window in the target session, with the title 'UI run'
tmux neww -t pers -n 'UI run' -c ~/dev/own/johndoe/front-end
# Simulate user entering a command to the first pane
tmux send -t pers:'UI run.0' 'git status --short' Enter 
# Split this window horizontally
tmux split-window -t pers:'UI run' -h -c ~/dev/own/johndoe/front-end
# Simulate user entering a command to the second pane in this window
tmux send -t pers:'UI run.1' 'npm run dev' Enter 

tmux neww -t johndoe -n 'API run' -c ~/dev/own/johndoe/back-end/Api
tmux send -t johndoe:'API run' 'dotnet run --no-build' Enter

# Focus the first window
tmux select-window -t johndoe:'Main'
# Attach the current terminal to the only session available 
# (you might want to add "-t johndoe" here if you need several sessions running in parallel)
tmux a -d

我创建了一个bash/zsh别名来源会话配置:

alias tmuxjohndoe='. ~/.config/tmux/tmux-sessions/johndoe.conf'

我花了适量的调试时间,弄清楚我需要给johndo .conf文件执行权限:-) 现在无论在哪里,我都要跑!

我试着用了这一页上的所有想法,但没有一个我喜欢。我只是想要一个解决方案,当我的终端打开时,用一组特定的窗口启动tmux。我还希望它是幂等的,即打开一个新的终端窗口接管tmux会话从前一个。

上面的解决方案通常倾向于打开多个tmux会话,而我只想要一个。首先,我把这个添加到我的~/.bash_profile中:

tmux start-server
if [[ -z "$TMUX" ]]
then
  exec tmux attach -d -t default
fi

然后我在我的~/.tmux.conf中添加了以下内容:

new -s default -n emacs /usr/local/bin/emacs
neww -n shell /usr/local/bin/bash
neww -n shell /usr/local/bin/bash
selectw -t 1

现在,每当我启动一个终端或启动tmux或其他什么,我要么重新连接到我现有的所需设置(名为default的会话),要么用该设置创建一个新会话。

从我的“得到”。脚本,我每天早上都会调用它来运行一堆后续的“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

有一个tmux插件。

查看tmux- resurrection

Restore tmux environment after system restart. Tmux is great, except when you have to restart the computer. You lose all the running programs, working directories, pane layouts etc. There are helpful management tools out there, but they require initial configuration and continuous updates as your workflow evolves or you start new projects. tmux-resurrect saves all the little details from your tmux environment so it can be completely restored after a system restart (or when you feel like it). No configuration is required. You should feel like you never quit tmux.

或tmux-continuum

特点: 持续节约tmux环境 当计算机/服务器打开时自动启动tmux tmux启动时自动恢复

去https://github.com/remiprev/teamocil看看吧

你可以使用YAML指定你的结构

windows:
  - name: sample-window
    splits:
      - cmd: vim
      - cmd:
        - ipython
        width: 50
      - cmd:
        height: 25