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


当前回答

我试图创建一个复杂的窗格网格,不得不一遍又一遍地处理切换和分割窗格。以下是我的体会:

tmux new-session \;

让你开始新的疗程。要将其水平或垂直分割,请使用split-window -h或-v,如下所示:

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

创建3个窗格,如下所示:

------------
|          |
|----------|
|    |     |
------------

要在该窗格中运行命令,只需添加发送键'my-command'命令和执行命令的C-m:

tmux new-session \; \
  send-keys 'tail -f /var/log/monitor.log' C-m \; \
  split-window -v \; \
  split-window -h \; \
  send-keys 'top' C-m \; 

结果会话应该是这样的。

------------
|  tail    |
|----------|
|    | top |
------------

现在,我再次尝试细分左下角的窗格,因此可以使用last-pane返回,或者在更复杂的窗口中,使用select-pane -t 1,其中1是按从0开始创建的顺序创建的窗格的编号。

tmux new-session \; \
  send-keys 'tail -f /var/log/monitor.log' C-m \; \
  split-window -v \; \
  split-window -h \; \
  send-keys 'top' C-m \; \
  select-pane -t 1 \; \
  split-window -v \; \
  send-keys 'weechat' C-m \;

这是否。基本上你只需要知道如何使用分割窗口和选择窗格。通过-p 75传递拆分窗口创建的窗格的百分比大小也很方便,这样可以更好地控制窗格的大小。

tmux new-session \; \
  send-keys 'tail -f /var/log/monitor.log' C-m \; \
  split-window -v -p 75 \; \
  split-window -h -p 30 \; \
  send-keys 'top' C-m \; \
  select-pane -t 1 \; \
  split-window -v \; \
  send-keys 'weechat' C-m \;

是什么导致会话看起来像这样

------------------
|      tail      |
|----------------|
|          | top |
|----------|     |
| weechat  |     |
------------------

其他回答

tmuxp支持JSON或YAML会话配置和python API。一个用YAML语法创建新会话的简单tmuxp配置文件是:

session_name: 2-pane-vertical
windows:
  - window_name: my test window
    panes:
      - pwd
      - pwd

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

new  -n home
neww -n emacs
neww -n puppet
neww -n haskell
neww -n ruby
selectw -t 1
:~$ 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  

有一个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启动时自动恢复

这个脚本启动一个名为“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