我如何设置tmux,使它启动与指定的窗口打开?
当前回答
创建新会话时,可以使用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。
其他回答
我知道我的解决方案和其他人的很相似,但我找不到任何一个完全符合我的要求的解决方案:
我想在特定的路径上打开标题窗口 我想要尽可能多的窗户和玻璃 我想在每个窗格中运行任意命令,保存在历史记录中 我想要聚焦在脚本末尾的窗口 我希望能快速生成这个 我希望它易于维护
对于我名为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文件执行权限:-) 现在无论在哪里,我都要跑!
这对我很有用。创建5个具有给定名称的窗口,并自动选择到主窗口。
new -n home
neww -n emacs
neww -n puppet
neww -n haskell
neww -n ruby
selectw -t 1
tmuxp支持JSON或YAML会话配置和python API。一个用YAML语法创建新会话的简单tmuxp配置文件是:
session_name: 2-pane-vertical
windows:
- window_name: my test window
panes:
- pwd
- pwd
有一个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启动时自动恢复
你应该在你的tmux配置文件(~/.tmux.conf)中指定它,例如:
new mocp
neww mutt
new -d
neww
neww
(打开一个会话,2个窗口,第一个启动mocp,第二个启动mutt,另一个分离会话,3个空窗口)。