How to automatically rename tmux windows to the current directory

Tmux

Tmux Problem Overview


I would like to have tmux to automatically rename the window with the current working directory (cwd). As it is by default, it names the tab/window as the name of the current process, such as zsh or vim.

When I open a new window in tmux, the name is reattach-to-use-namespace and then it immediately switches to zsh.

tmux tabs

I'm on OS X 10.10.2, I use zshell, and I have tmux 1.9a.

To be clear, I don't want the entire path in the name of the window, just the current directory, so for example, I want projectName, not /Users/username/Development/projectName.

If you want to see my current tmux.conf, here it is.

Tmux Solutions


Solution 1 - Tmux

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}")'

Solution 2 - Tmux

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.

Solution 3 - Tmux

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: https://stackoverflow.com/questions/10986794/unix-remove-part-of-path/31728689#31728689

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.

Solution 4 - Tmux

To get the best of both worlds - window name is path when you're at a shell prompt, but name of executable when you're running something, try this:

set-option -g status-interval 1
set-option -g automatic-rename on
set-option -g automatic-rename-format "#{?#{==:#{pane_current_command},bash},#{b:pane_current_path},#{pane_current_command}}"

Replace "bash" with whatever shell you're using.

Solution 5 - Tmux

I use the following in ~/.tmux.conf to achieve this (working on OSX, zsh, tmux-2.3):

set -g automatic-rename-format '#{pane_current_path}'
set -g status-interval 5

You can set status-interval to 1 to make it respond faster to changing directories.

According to the changelog (https://raw.githubusercontent.com/tmux/tmux/master/CHANGES) this should work in tmux 1.9 and up.

Using ssh into a CentOS machine with tmux 2.3 the window name doesn't change until I press return in the new panel, not sure why that is happening.

Solution 6 - Tmux

Adding this config to your ~/.tmux.conf file should work:

set-option -g window-status-current-format '#I:#{pane_current_path}#F'
set-option -g window-status-format '#I:#{pane_current_path}#F'
set-option -g status-interval 1

It depends however on your Tmux version. I wasn't able to make it work on 1.9a3 (in Cygwin) - but with Tmux 1.8 on Ubuntu (in Vagrant) it worked fine.

Solution 7 - Tmux

Do something like this in a tmux session for zsh shell:

setopt PROMPT_SUBST
export PS1=$'\ek$(basename $(pwd))\e\\> '

If someone uses bash shell:

export PS1="\033k\$(basename \$(pwd))\033\\> "

You can add these commands in the shell initialization file on the condition the $TERM env variable is set to the value "screen"

Solution 8 - Tmux

I am using zsh hook for that

Add following in ~/.zshrc

precmd () {
  if [ -n "$TMUX" ]; then
    tmux set-window-option -q window-status-format "#[fg=cyan bg=cyan] | #[fg=white, bg=cyan] #I | ${PWD##/*/} #[fg=cyan, bg=cyan] | "
    tmux set-window-option -q window-status-current-format "#[fg=cyan, bg=cyan] | #[fg=white, bg=cyan] #I | ${PWD##/*/} #[fg=cyan, bg=cyan] | "
  fi
}

Solution 9 - Tmux

This doesn't strictly answer your question--it doesn't automatically rename an existing tmux session to the current working directory.

Rather, when creating a new session, it names that session after the current working directory.

Here's what I did:

to

~/.aliases

add

alias tm='tmux new -s `basename $PWD`'

Open a new terminal window and type:

tm

This now creates a new tmux session which is named after the current working directory.

Note: This relies on basename which does not exist in Windows.

Solution 10 - Tmux

I am sure that you want use this:

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

enter image description here

Solution 11 - Tmux

To change what you see in the window list you can specify a format when you define the key-binding for the chose-window function like this:

bind-key '"'  choose-window  -F "#{session_name} | #{window_name} - #{b:pane_current_path} (#{pane_current_command})"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionaharris88View Question on Stackoverflow
Solution 1 - TmuxJustin M. KeyesView Answer on Stackoverflow
Solution 2 - TmuxCELView Answer on Stackoverflow
Solution 3 - TmuxCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 4 - TmuxGreg BellView Answer on Stackoverflow
Solution 5 - TmuxHarry MallonView Answer on Stackoverflow
Solution 6 - TmuxJosef CechView Answer on Stackoverflow
Solution 7 - TmuxJuniorCompressorView Answer on Stackoverflow
Solution 8 - TmuxlogcatView Answer on Stackoverflow
Solution 9 - TmuxFreePenderView Answer on Stackoverflow
Solution 10 - TmuxjibancanyangView Answer on Stackoverflow
Solution 11 - TmuxRho PhiView Answer on Stackoverflow