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


当前回答

使用tmuxinator—它允许您配置多个会话,并且您可以在任何给定时间选择启动哪个会话。您可以在特定的窗口或窗格中启动命令,并为窗口指定标题。下面是一个开发Django应用程序的例子。

配置文件示例:

# ~/.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

请参阅上面链接的README以获得完整的解释。

其他回答

使用tmuxinator—它允许您配置多个会话,并且您可以在任何给定时间选择启动哪个会话。您可以在特定的窗口或窗格中启动命令,并为窗口指定标题。下面是一个开发Django应用程序的例子。

配置文件示例:

# ~/.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

请参阅上面链接的README以获得完整的解释。

插件tmux-continuum将自动保存您的tmux会话,并在下次tmux启动时加载它,应该比这里的一些自定义脚本解决方案更容易设置。

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

要按需保存会话,您可以使用tmux- resurrection插件。运行tmux continuum还需要tmux- resurrection

tmux- resurrection保存tmux环境中的所有小细节 因此,它可以在系统重新启动后(或当您重新启动时)完全恢复 感觉像这样)。无需配置。你应该感觉像你自己 永远不要放弃tmux。

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

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

对于我名为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文件执行权限:-) 现在无论在哪里,我都要跑!

这个脚本启动一个名为“e”的会话和三个窗口

#!/bin/sh 
tmux new-session -s e   -n etc -d 'cd /etc; bash -i'
tmux new-window  -t e:1 -n home   'cd ~; bash -i'
tmux new-window  -t e:2 -n log   'cd /var/log; bash -i'

tmux select-window -t e:1
tmux -2 attach-session -t e

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