来自 fish shell 别名的 Tmux 刷新客户端调用

Tmux refresh client call from fish shell alias

我希望 tmux 窗格标题在我启动后立即刷新 htop,所以我在我的 config.fish:

中添加了别名
alias h "htop;tmux refresh-client -S"

但它什么也没做。我也尝试过延迟:

alias h "htop;sleep 0.1;tmux refresh-client -S"

那也没用 - tmux 仍然只在默认间隔后刷新,这对我来说太长了,你只能将它减少到 1 秒,不能更短。

我做错了什么,我有可能做我想做的事吗?

当我们从等式中删除 alias 时,也许这更容易理解:

echo banana; sleep 5s; echo sausage

会回显"banana",等待5秒后才打印"sausage",所以

htop; tmux refresh-client -S

会运行htop,等到它结束了然后然后运行tmux refresh-client -S,此时fish会是前台进程再次.


必须要做的是让 shell 与 tmux 集成。现在,显然 tmux 有一个 Names and titles 的转义序列,所以

printf '\ekhtop\e\' # \e is 3 - the escape character

将 window 标题更改为 "htop"。

Fish 有可以绑定函数的事件,所以像

function tmux_name --on-event fish_preexec
    printf '\ek%s\e\' "$argv" # the argument for preexec is the commandline about to be executed
end

将始终将 tmux window 名称设置为命令行。命令完成后不会重置它,所以我们需要第二个函数

function tmux_reset_name --on-event fish_postexec
     # $argv for postexec is also the commandline
     # so we can't use it. Just hardcode "fish".
     printf '\ek%s\e\' fish 
end

并不是说这是完美的或任何东西 - 即使对于非常 short-running 命令它仍然会设置标题,即使对于长命令它也会使用完整的命令行(可能只使用 $argv[1]会更好)。

请注意,这些函数必须在 config.fish 或由它明确来源的文件(或 ~/.config/fish/conf.d/)中定义,因为函数文件是自动加载的,所以 fish不会知道这个事件。