Automatically updating tmux status bar with pwd active panel

In tmux, the status bar usually displays the current working directory of the panel in the window list. If I have, for example, two panels in a window, and two panels have different working directories, is it possible to automatically update the status bar with the current working directory of the Im panel that is currently focused on?

To clarify, if I have a window with two panels, and the first panel is in ~ , and the second panel is in ~/Sites , I would like 1: ~ when I focus on the first in the list of windows in the status bar panels, and 1: ~ / Sites when I focus on the second panel.

+7
tmux
source share
5 answers

Tmux PWD Command-Line Panel

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

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

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

Now create a script that displays this variable, for example ~/bin/display_tmux_pane_pwd.sh :

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

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

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

It may take some time to update after switching panels, so you can change this with this command. By default, it is updated every 15 seconds, it will take 5 seconds. Change it as you like.

 set -g status-interval 5 

Tmux-Pane PWD in other programs

Sometimes it’s useful to open a panel or window and immediately start the program instead of loading another shell (for example, tmux new-window vim ). Thus, when you close this program, you also close the window. Unfortunately, the way I describe above requires a hint for translating PWD status. However, in many programs you can get around this fairly easily. Here is an example of what is in my .vimrc file so that vim updates the status of PWD 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 
+5
source share

In addition to the previous answer, I would like to add that you do not need to rely on the state-interval option. The expectation of seeing change is not very elegant. You can manually update the event status bar:

 tmux refresh-client -S 

I use this option after switching the panel / window / session. In my tmux configuration you will find, for example, to switch panels:

 bind -rk select-pane -U\; refresh-client -S bind -rj select-pane -D\; refresh-client -S bind -rl select-pane -R\; refresh-client -S bind -rh select-pane -L\; refresh-client -S 

I wrote about this earlier: manual update panel .

+4
source share

If you want the name of the current window to reflect the name of the directory, here is a modified version of the original response, which does not require an immediate script call from tmux and updates:

 export PS1=$PS1'$( [ -n $TMUX ] && tmux rename-window $(basename $PWD))' 

Note that this means that you can no longer display the current process name. It doesn't matter to me.

+2
source share

I solved this by adding this to my .zshrc (or .bashrc):

cd() { builtin cd $1 tmux refresh-client -S }

Then bind tmux refresh-client -S with toggle tmux refresh-client -S as others did.

0
source share

This is another way to do this.

Work ENV:

OS

  • OSX 10.14.5

Terminal

Tmux

  • tmux 2.9a

Example status field on the right

~/.tmux.config to call an external bash script in the above example, the file is here: ~/.tmux_path.sh

~ / .Tmux.config

 set -g status-interval 1 set -g status-right-length 150 set -g status-right "#(~/.tmux_path.sh #{pane_current_path}) %Y-%m-%d %H:%M:%S" 

An example bash script that reads # {pane_current_path) arg. basename starting path and just displays the current one. This is good if used as a window identifier.

~ / .Tmux_path.sh

 #!/bin/sh printf '\033%s\007' $(basename $1) # show full path use $1 instead of $(basename $1) 

Make the script executable

 chmod +x ~/.tmux_path.sh 

Window Naming Example

  • Note: use the same ~/.tmux_path.sh top

~ / .Tmux.conf

 set -g status-interval 1 set -g window-status-current-format "[#[fg=white] #(~/.tmux_path.sh #{pane_current_path})]" set -g window-status-format "#[fg=black] #(~/.tmux_path.sh #{pane_current_path})" 
0
source share

All Articles