21

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?

3 Answers 3

30

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}".
6
  • 1
    This renames the pane only when a new pane is made for me Commented Sep 10, 2015 at 3:00
  • My status bar was too short so it was truncating the path. Use set -g status-left-length 85 to increase the length
    – h-rai
    Commented May 15, 2019 at 2:27
  • 1
    Works for tmux 2.1: set-option -g status-right '#{pane_current_path}' Also to increase interval of updating current dir at status I use setting: set-option -g status-interval 1 Commented Aug 11, 2019 at 13:12
  • @WillHardwick-Smith is it only when a new pane is made? I thought that at first, but after some experimenting realized it was only when I SWITCHED panes. Then I realized this was a good thing! For panes in the foreground it will show the running command, for panes in the background it will show the directory
    – Max Coplan
    Commented Sep 25, 2019 at 1:39
  • 3
    For the current window, it's "window-status-current-format", and for every other window, it's "window-status-format", at least in tmux 3.1. I also use b:pane_current_path to only have the basename and not the whole path
    – kaios
    Commented Sep 29, 2021 at 13:31
7

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.

2
  • Thanks for your help. Because I'm novice to Linux and began learning in bash, I don't want to make the switch to zsh unless I really must. Hence the reason I prefer a solution for bash.
    – rajames
    Commented May 21, 2013 at 15:27
  • I did however found a way to make run precmd with bash using a trick by Glyph Lefkowitz. A solution was found here. Thanks!
    – rajames
    Commented May 21, 2013 at 15:45
4

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.

1
  • Yes, of course! The spaces are required after [[ and before ]]. I copied a line from the terminal window after the test and did not notice that the spaces that this spaces have disappeared when displaying.
    – avb1003
    Commented Mar 28, 2016 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.