我如何设置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文件执行权限:-) 现在无论在哪里,我都要跑!

其他回答

这对我很有用。创建5个具有给定名称的窗口,并自动选择到主窗口。

new  -n home
neww -n emacs
neww -n puppet
neww -n haskell
neww -n ruby
selectw -t 1

如果你只是想把屏幕分成2个窗格(水平),你可以运行这个命令(不需要tmux或shell脚本):

tmux new-session \; split-window -h \;

你的屏幕会是这样的:

[ks@localhost ~]$                      │[ks@localhost ~]$ 
                                       │
                                       │
                                       │
                                       │
                                       │
                                       │
                                       │
                                       │
                                       │
                                       │
                                       │
[10] 0:ks@localhost:~*                  "localhost.localdomain" 19:51 31-янв-16

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

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

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

使用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 new-session "tmux source-file ~/session1"  

session1

neww
split-window -v 'ipython'  
split-window -h  
new-window 'mutt'  

在.bashrc中创建别名

:~$ echo `alias tmux_s1='tmux new-session "tmux source-file ~/session1"'` >>~/.bashrc  
:~$ . ~/.bashrc  
:~$ tmux_s1