如何重新加载定义了多个会话的 tmux 配置文件?

Mik*_*kov 9 linux session tmux

我在 中使用了两个单独的会话tmux,并且在 中有以下内容/etc/tmux.conf

set -g base-index 1

new -s logi -n cmd
neww -n logi "cat /dev/logi | ccze -m ansi -p syslog -C"
splitw -t 1 -v -p 50
selectw -t 2
selectp -t 0

new -s standard -n htop "htop"
neww -n cmd
splitw -t 2 -v -p 50
selectw -t 2 
selectp -t 1
Run Code Online (Sandbox Code Playgroud)

standard通过调用以下命令启动会话:

urxvtc -name 'tmux' -e bash -c 'tmux attach-session -t standard'
Run Code Online (Sandbox Code Playgroud)

如果没有会话,则创建一个,如果有,则附加。如您所见,我有两个窗口,其中一个分为 2 个窗格。当我重新加载配置文件时,我从另一个会话中获得了 2 个额外的窗口,并且都已添加到预先存在的窗口中。此外,以前的窗口收到了一个额外的窗格。两个额外的窗格很清楚,其中任何一个都没有执行的命令 (htop)。

有没有办法以仅应用于附加会话的方式重新加载配置文件?或者我是否必须忘记在使用会话时重新加载配置文件,并且为了应用新设置,我应该使用tmux kill-server并重新启动会话?

sci*_*tor 5

构建一个包装器

我认为您的需求最好通过某种形式的包装器脚本来设置自定义会话。有点像这个答案

它看起来像这样,但您应该根据您的特定需求进行更改。

#!/bin/bash

# test if the session has windows
is_closed(){ 
    sess=$1
    n=$(tmux ls 2> /dev/null | grep "^$sess" | wc -l)
    [[ $n -eq 0 ]]
}

# either create it or attach to it
if is_closed logi ; then
  tmux new -d -s logi -n cmd
  tmux neww -t logi -n logi "cat /dev/logi | ccze -m ansi -p syslog -C"
  tmux splitw -t logi:1 -v -p 50
  tmux selectw -t logi:2
  tmux selectp -t logi:1
fi
if is_closed standard ; then
  tmux new -d -s standard -n htop "htop"
  tmux neww -n cmd -t standard
  tmux splitw -t standard:2 -v -p 50
  tmux selectw -t standard:2 
  tmux selectp -t standard:1
fi
Run Code Online (Sandbox Code Playgroud)

重新加载配置文件

如果在使用 tmux 时对配置文件进行编辑,则可以运行这是提示

tmux source-file /path/to/conf
Run Code Online (Sandbox Code Playgroud)

或者,您可以将其绑定到一个键 .tmux.conf

bind r source-file ${HOME}/.tmux.conf \; display-message "source-file reloaded"
Run Code Online (Sandbox Code Playgroud)

主目录配置

最后,您真的不应该添加重要的自定义,/etc/tmux.conf因为如果您需要使用共享系统,这对其他人没有帮助。相反,我建议您添加任何自定义,~/.tmux.conf因为它是本地的并且特定于您的个人需求。