Tmux: How to configure tmux to display the current working directory of a pane on the status bar?

走远了吗. 提交于 2019-12-04 10:19:05

问题


I am new to tmux and I am trying to edit my tmux.conf file to have the left side of the status bar reflect:

[SessionName] [CurrentPane] [CurrentWorkingDirectory]

I am able to display the SessionName and CurrentPane. However I can't get to display the CurrentWorkingDirectory.

I've tried several #(shell command) options:

  1. #(tmux select-pane -t :.#P; pwd) : But this prints some other $PWD variable which does NOT reflect the current directory of the bash session in the current pane.

  2. #(tmux select-pane -t :.#P; tmux send-keys pwd Enter) Firstly, although it did print the CurrentWorkingDirectory if I'm in a terminal. It prints this in the terminal and NOT in the status bar like how I want it. Secondly, It entered "pwd Enter" every 15 seconds whether or not I was in a terminal, which was a hassle to reverse if your not as quick (like I am).

I've tried these options but to no avail, is it possible to do what I want? and how?


回答1:


There is a variable for that, which doesn't seem to be in the manpage but is mentioned in the development version. For me, it works in the 1.8 release of tmux.

set -g status-left "#{pane_current_path}"

Note that it also works when you put it in the window-status. Each window status will mention respective working directories.

setw -g window-status-format "#{pane_current_path}".



回答2:


I'm not sure how to do this in bash, but in zsh, there's a hook that gets run before every command. In your .zshrc:

precmd () {
    tmux set -qg status-left "#S #P $(pwd)"
}

This will run that tmux command everytime you run a command. Hope this helps. Since bash doesn't have a precmd, I'm not sure how to do this.




回答3:


Unfortunately, the proposed solution does not work for version 1.7 - "official version" for OpenSuse 12.3, but I managed to find a solution:
In /etc/tmux.conf:

setw -g window-status-current-format "#T(#I:#W#F)"  
setw -g window-status-format "#T(#I:#W#F)"  

Here #T - tells to display current pane title, which can be set with some escape sequence. For doing this at each shell command, put somewhere in .bashrc:

[[ -n "$TMUX" ]] && PROMPT_COMMAND='echo -n -e "\e]2;${PWD/${HOME}/~}\e\\"'  

This works for me on OpenSuse 12.3, tmux 1.7, bash 4.2.53.



来源:https://stackoverflow.com/questions/16624752/tmux-how-to-configure-tmux-to-display-the-current-working-directory-of-a-pane-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!