How to automatically rename tmux windows to the current directory

Expanding on what Josef wrote, you can put the basename of the directory in the status using a shell snippet:

# be sure to see note* below
set -g window-status-format '#I:#(pwd="#{pane_current_path}"; echo ${pwd####*/})#F'
set -g window-status-current-format '#I:#(pwd="#{pane_current_path}"; echo ${pwd####*/})#F'

# status bar updates every 15s by default**, change to 1s here 
# (this step is optional - a lower latency might have negative battery/cpu usage impacts)
set -g status-interval 1

*Note that what would be ${pwd##*/} is escaped to ${pwd####*/} since # has special meaning in the format string.

**See here for an example default tmux config.


With tmux 2.3+, the b: format modifier shows the "basename" (or "tail") of a path.

set-option -g status-interval 5
set-option -g automatic-rename on
set-option -g automatic-rename-format '#{b:pane_current_path}'

The FORMATS section of man tmux describes other modifiers, such as #{d:} and even #{s/foo/bar/:}.


With tmux 2.2 or older, the basename shell command can be used instead.

set-option -g status-interval 5
set-option -g automatic-rename on
set-option -g automatic-rename-format '#(basename "#{pane_current_path}")'

Show the top N components

enter image description here

Showing just the basename generates too much ambiguity, but full paths are too much clutter, so I settled for:

the/last/path

instead of:

/a/very/long/the/last/path

or just:

path

.tmux.conf

set-window-option -g window-status-current-format '#[fg=white,bold]** #{window_index} #[fg=green]#{pane_current_command} #[fg=blue]#(echo "#{pane_current_path}" | rev | cut -d'/' -f-3 | rev) #[fg=white]**|'
set-window-option -g window-status-format '#[fg=white,bold]#{window_index} #[fg=green]#{pane_current_command} #[fg=blue]#(echo "#{pane_current_path}" | rev | cut -d'/' -f-3 | rev) #[fg=white]|'

Trick taken from: Remove part of path on Unix

If that still does not solve ambiguity, I go for:

bind-key -r w choose-window -F '#{window_index} | #{pane_current_command} | #{host} | #{pane_current_path}'

Tested on Tmux 2.1, Ubuntu 16.04.

Tags:

Tmux