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


当前回答

首先,我有@jasonwryan的方法,但如果你有超过2个配置,它会让人困惑。

所以我创建了一个别名函数:

tmx () {
    tmux "$2" source-file "$HOME/.tmux/$1.conf";
}

在~ /。Tmux / I有多个会话用于许多用途。例如,我在不同的公司工作,他们拥有彼此的开发环境。因此,使用上面的alias函数,我可以简单地调用:tmx company1并加载我需要的配置。

更新:tmux命令后的“$2”的目的是,您可以传递额外的tmux参数。

其他回答

我是这样做的:

#!/bin/bash

function has-session {
  tmux has-session -t name_of_my_session 2>/dev/null
}

if has-session ; then
  echo "Session already exists"
else
  cd /path/to/my/project
  tmux new-session -d -s name_of_my_session 'vim'
  tmux split-window -h -p 40 start_web_server
  tmux split-window -v
  tmux attach-session -d -t name_of_my_session
fi

我的每个项目都有一个文件。你也可以把它们分组,一些用于工作,一些用于业余项目。

也可以将其移动到~/bin文件夹,将其添加到PATH并给出tmux_my_awesome_project名称。然后你就可以在每个地方运行它了。

您可以编写一个小型shell脚本,用所需的程序启动tmux。我在一个称为dev-tmux的shell脚本中有以下内容。开发环境:

#!/bin/sh
tmux new-session -d 'vim'
tmux split-window -v 'ipython'
tmux split-window -h
tmux new-window 'mutt'
tmux -2 attach-session -d

所以每次我想启动我最喜欢的开发环境时,我都可以这样做

$ dev-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  |     |
------------------

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

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

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

试试兄弟,这是一个项目引导器。

它提供了与tmux交互的简单api。

它看起来是这样的:

#!/bin/sh
# @ project_name/tasks/init.sh
structure project_name
  window editor
    run 'vim'
  window terminal
    run 'echo happy coding ...'
focus editor
connect project_name

要启动一个项目,所有人需要做的是运行以下命令-

兄弟开始<project_name>

检查下面的截屏,它展示了如何开始与兄弟和使用它的tmux api。

https://user-images.githubusercontent.com/8296449/50532502-2cf3e400-0b6e-11e9-84af-c5f77a04e5cc.gif