如何在 tmux 启动时创建布局并运行命令? [英] How to create a layout and run commands in at tmux launch?
问题描述
我正在尝试编写一个可以运行/源代码的脚本,以便 tmux 设置特定的布局并运行命令.我有一些结果.
I am trying to write a script I can run/source so tmux set a specific layout and run commands. I have some results.
这是我目前所写的:
selectp -t 1
splitw -v -p 15
splitw -h -p 50
selectp -t 1
send-keys 'cd ~/code/octoly' Enter
send-keys 'vim .' Enter
selectp -t 2
send-keys 'cd ~/code/octoly' Enter
send-keys 'drails c' Enter
new-window -d -n server -c ~/code/octoly
selectw -t 2
send-keys 'fd' Enter
splitw -h -p 50
send-keys 'cd ~/code/octoly' Enter
send-keys 'drails s' Enter
selectp -t 1
splitw -v -p 50
send-keys 'cd ~/code/octoly' Enter
send-keys 'be guard' Enter
或多或少是创建第二个窗口和窗格.虽然窗格没有给我我想要的.它们都在第一个窗口中创建,在第二个窗口中没有创建.
What works more or less is the creation of the second window and the panes. Though the panes does not give me what I want. They are all created in the first window, none are created in the second.
这是我运行的启动 tmux:
Here is what I run to start tmux:
tmux new 'tmux move-window -t 99 \; source-file ~/.tmux/session_octoly'
此外,我真正迷失的地方是,如果我一个一个地手动运行每个命令,它就会给我想要的东西.
Furthermore, where I'm really lost is that if I run each command by hand one by one, it gives me what I want.
我在这里遗漏了什么?
推荐答案
首先,您在 new-window
中使用了 -d
标志使新窗口成为当前窗口:
First of all, you're using the -d
flag in new-window
that does not make the new window the current window:
如果指定了 -d,会话不会使新窗口成为当前窗口.
If -d is given, the session does not make the new window the current window.
但是,我可能会以不同的方式启动 tmux
,使用 tmux -f
标志来使用可以执行您想要的操作的临时配置文件.
在您的环境中运行之前请检查命令.
However I would probably spin up tmux
in a different way, using tmux -f
flag to use an ad-hoc config file that does what you want.
Please check the commands before running it in you're environment.
# File: ~/.tmux/octoly.conf
# Load default .tmux.conf
source-file ~/.tmux.conf
# setup octoly session
new-session -s octoly -n editor -d -c ~/code/octoly
send-keys 'vim .' Enter
split-window -v -p 15 -c ~/code/octoly
send-keys 'drails c' Enter
split-window -h -p 50 -c ~/code/octoly
# Select vim pane
select-pane -t 1
# create second window
new-window -n server -c ~/code/octoly
send-keys 'fd' Enter
split-window -h -p 50 -c ~/code/octoly
send-keys 'drails s' Enter
select-pane -t 1
split-window -v -p 50 -c ~/code/octoly
send-keys 'be guard' Enter
# Optional step, reselect window 1 (the one with vim)
select-window -t editor
然后您需要使用以下命令启动 tmux
:
Then you need to launch tmux
using:
tmux -f ~/.tmux/octoly.conf attach
另一种选择是构建一个发送相同命令的 bash 脚本.
Another alternative would be to build a bash script sending the same commands.
注意:我使用了 -c
标志来指定起始目录,而不是每次执行 cd
命令时都运行.鉴于每个命令都在同一个文件夹中运行,您可以在 split-window
命令中将它们取出.
Note: I've used the -c
flag to specify the start directory instead of running every time a cd
command. Given that every command is run in the same folder you can take them out in the split-window
commands.
这篇关于如何在 tmux 启动时创建布局并运行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!