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


当前回答

我试着用了这一页上的所有想法,但没有一个我喜欢。我只是想要一个解决方案,当我的终端打开时,用一组特定的窗口启动tmux。我还希望它是幂等的,即打开一个新的终端窗口接管tmux会话从前一个。

上面的解决方案通常倾向于打开多个tmux会话,而我只想要一个。首先,我把这个添加到我的~/.bash_profile中:

tmux start-server
if [[ -z "$TMUX" ]]
then
  exec tmux attach -d -t default
fi

然后我在我的~/.tmux.conf中添加了以下内容:

new -s default -n emacs /usr/local/bin/emacs
neww -n shell /usr/local/bin/bash
neww -n shell /usr/local/bin/bash
selectw -t 1

现在,每当我启动一个终端或启动tmux或其他什么,我要么重新连接到我现有的所需设置(名为default的会话),要么用该设置创建一个新会话。

其他回答

你应该在你的tmux配置文件(~/.tmux.conf)中指定它,例如:

new mocp
neww mutt

new -d
neww
neww

(打开一个会话,2个窗口,第一个启动mocp,第二个启动mutt,另一个分离会话,3个空窗口)。

您可以编写一个小型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-continuum将自动保存您的tmux会话,并在下次tmux启动时加载它,应该比这里的一些自定义脚本解决方案更容易设置。

特点: 持续节约tmux环境 当计算机/服务器打开时自动启动tmux tmux启动时自动恢复

要按需保存会话,您可以使用tmux- resurrection插件。运行tmux continuum还需要tmux- resurrection

tmux- resurrection保存tmux环境中的所有小细节 因此,它可以在系统重新启动后(或当您重新启动时)完全恢复 感觉像这样)。无需配置。你应该感觉像你自己 永远不要放弃tmux。

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

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

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

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

更新:tmux命令后的“$2”的目的是,您可以传递额外的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文件执行权限:-) 现在无论在哪里,我都要跑!