Keep the Window's Name Fixed in Tmux

Tmux window title keeps renaming

Check whether your PS1 (plus PS2, PS3 or PS4 if those are set) is changing the title:

printf %q "$PS1" | grep -F '\\033'

Tmux running rename keeps old window name - how to I clear it?

You can remove the default value by adding something like this to your .tmux.conf:

unbind ,
bind-key , command-prompt -p (rename-window) "rename-window '%%'"

This will:

  • clear the current bind for the , key
  • re-bind the , key to the command-prompt function
  • specifies a prompt message via -p (rename-window)
  • ends with a command specifier, which uses the input value (%%) as a parameter to the rename-window function

To emulate the existing behaviour, it would look like:

bind-key , command-prompt -I #W -p (rename-window) "rename-window '%%'"

...which tells command-prompt to use the current window name (#W, an alias for #{window_name}) as the initial, default value of the command prompt.

Prevent tmux from displaying/replacing user-namespace from window's name

I found the answer here
The tmux command for this is:

set-option -g allow-rename off

Tmux name window at creation and use the name in zsh command

One problem is that only the first %% in the template is replaced. The other is that a cd command done in a subshell will be lost when you return to the parent shell. For bash you can try a binding something like this:

bind-key c command-prompt -p "window name:" \
"new-window -c '#{pane_current_path}' 'myfn \"%%\"'"

This runs a bash script myfn that must be in your PATH to do the work. It contains:

#!/bin/bash
name=$1
tmux rename-window "$name"
cd ../"$name" || echo "failed to cd to $name" >&2
exec bash -i

Remember to chmod +x the script. It calls tmux to rename the window, and then you would have your z code to find and cd to a directory. In this example, I just look in ../ for it. The final exec ensures we replace the current shell by an interactive one.

For zsh, you can do something similar, but if you want myfn to be found in your fpath you need new-window to run zsh with the -i option, as zsh only does this for interactive shells. So use a binding like

bind-key c command-prompt -p "window name:" \
"new-window -c '#{pane_current_path}' 'exec zsh -ic \"myfn %%\"'"

Here I've added an exec to avoid having a pointless parent shell, and since -c must be followed by the command as a single string I've put myfn and %% inside the same double-quotes. You'll need to add more quoting if your name can contain whitespace etc.

The myfn zsh script can be the same, with bash replaced by zsh.
I don't know enough about zsh to avoid the final exec zsh -i which is needed to stop the zsh -ic shell from terminating.

How do I set tmux window name from within a Bash script?

# rename window name of current window
tmux rename-window newname

# rename another window
tmux rename-window -t <target> new-name

The parameter <target> could be

  • window name
  • window index
  • window name or index with session prefix: <session>:<window>


Related Topics



Leave a reply



Submit