使用活动窗格密码自动更新 tmux 状态栏 [英] Auto-update tmux status bar with active pane pwd

查看:38
本文介绍了使用活动窗格密码自动更新 tmux 状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 tmux 中,状态栏通常会显示窗口列表中某个窗格的当前工作目录.例如,如果我在一个窗口中有两个窗格,并且这两个窗格具有不同的工作目录,是否可以使用我当前关注的窗格的当前工作目录自动更新状态栏?

In tmux, the status bar normally shows the current working directory of a pane in the window list. If I have for example two panes in a window, and the two panes have different working directories, is it possible to automatically update the status bar with the current working directory of the pane I’m currently focused on?

澄清一下,如果我有一个有两个窗格的窗口,第一个窗格在 ~ 中,第二个窗格在 ~/Sites 中,我想要状态栏中的窗口列表,当我专注于第一个窗格时显示 1:~,当我专注于第二个窗格时显示 1:~/Sites.

To clarify, if I have a window with two panes, and the first pane is in ~ and the second pane is in ~/Sites, I would like the window list in the status bar to say 1:~ when I am focused on the first pane, and 1:~/Sites when I am focused on the second pane.

推荐答案

提示下的 Tmux 窗格 PWD

有几种方法可以做到这一点.我自己做.最简单也最可定制的方法是设置一个 tmux 可以访问的全局变量.

Tmux pane PWD at the prompt

There are several ways that you can do this. I do it myself. The easiest and most customisable way is to set a global variable that tmux can access.

首先将其添加到您的 .bashrc.zshrc 文件中,以在每次提示后设置 PWD 变量:

First add this to your .bashrc or .zshrc file, to set the PWD variable after every prompt:

# create a global per-pane variable that holds the pane's PWD
export PS1=$PS1'$( [ -n $TMUX ] && tmux setenv -g TMUX_PWD_$(tmux display -p "#D" | tr -d %) $PWD)'

现在,制作一个显示该变量的脚本,例如~/bin/display_tmux_pane_pwd.sh:

Now, make a script that displays this variable such as ~/bin/display_tmux_pane_pwd.sh:

#!/bin/bash
tmux showenv -g TMUX_PWD_$(tmux display -p "#D" | tr -d %)  | sed 's/^.*=//'

剩下的就是将它添加到.tmux.conf中的satis-bar:

All that is left is to add this to the satis-bar in .tmux.conf:

set -g status-left '#(~/bin/display_tmux_pane_pwd.sh)'

切换窗格后可能需要一段时间才能更新,因此您可以使用此命令进行更改.默认情况下,它每 15 秒更新一次,这将使其为 5 秒.随心所欲地更改它.

It may take awhile to update after switching panes, so you can change that with this command. By default it updates every 15 seconds, this will make it 5 seconds. Change it as you like.

set -g status-interval 5

<小时>

其他程序中的 Tmux-pane PWD

有时打开一个窗格或窗口并立即执行程序而不是启动另一个 shell 很有用(例如 tmux new-window vim).这样,当您关闭该程序时,您也会关闭窗口.不幸的是,我上面描述的方式需要提示才能广播 PWD 的状态.但是,在许多程序中,您可以很容易地解决这个问题.这是我的 .vimrc 文件中的示例,以便 vim 在更改缓冲区时更新 PWD 状态.


Tmux-pane PWD in other programs

Sometimes it is useful to open up a pane or window and immediately execute a program instead of booting up another shell (e.g. tmux new-window vim). This way, when you close that program you also close the window. Unfortunately, the way I describe above requires a prompt in order to broadcast the status of PWD. However, in many programs, you can work around this fairly easily. Here's an example of what is in my .vimrc file so that vim updates the PWD status whenever it changes buffers.

if exists("$TMUX")
    " Get the environment variable
    let tmux_pane_name_cmd = 'tmux display -p \#D'
    let tmux_pane_name = substitute(system(g:tmux_pane_name_cmd), "\n", "", "")
    let tmux_env_var = "TMUX_PWD_" . substitute(g:tmux_pane_name, "%", "", "")
    unlet tmux_pane_name tmux_pane_name_cmd
    function! BroadcastTmuxCwd()
        let filename = substitute(expand("%:p:h"), $HOME, "~", "")
        let output = system("tmux setenv -g ".g:tmux_env_var." ".l:filename)
    endfunction
    autocmd BufEnter * call BroadcastTmuxCwd()
endif

这篇关于使用活动窗格密码自动更新 tmux 状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆