26

I would love to be able to have my tmux window title automatically renamed to prompt_command, ps1 or just the hostname of a machine I ssh to. having 9 windows opened labeled "ssh" is really useless. Doing sysadmin work I open new screens and ssh around to much to manually rename them.

One thing I noticed is tmux updates the xterm window title so I feel like it has to know.

Any help? I would even be willing to go back to screen if I could get this feature.

11 Answers 11

16

I'm not aware of any way to make it look at your PS1 directly.

However, tmux understands the same commands to set the window name as screen does.

So you can define a function like this in your ~/.bashrc or ~/.zshrc:

settitle() {
    printf "\033k$1\033\\"
}

and then call settitle from anywhere.

For example, you could include it in your PS1 variable, e.g.

PS1='$HOST:$PWD$(settitle $HOST:$PWD)$ '

or via PROMPT_COMMAND:

PROMPT_COMMAND='$(settitle $HOST:$PWD)'
# and don't change PS1

Now I understand you have tmux running on your desktop, and you want ssh commands to have the hostname rather than ssh, that's much easier.

Given you've added settitle to your local ~/.bashrc, all you want to do is add this too:

ssh() {
    settitle "$*"
    command ssh "$@"
    settitle "bash"
}

Replace bash with zsh, or something else more appropriate if necessary.

5
  • This really dont name the window title to the hostname of remote box with out having the .bashrc setup on the remote box. Thank you though I did learn some slick.
    – user68782
    Mar 4 '11 at 3:31
  • If all you want is the hostname on your local side, that's much easier. Hang on a sec...
    – Mikel
    Mar 4 '11 at 3:50
  • brilliant! thank you. Sorry my inability to explain my request resulted in two questions. again, Thanks!
    – user68782
    Mar 4 '11 at 5:12
  • As tmux changes its window title on a whim, this can very quickly be overwritten by normal changes to the window title by tmux. Oct 8 '12 at 18:43
  • Thanks! I used PROMPT_COMMAND='settitle $HOSTNAME:$PWD' - since with @Mikel's code I got no hostname and a -bash: : No such file or directory error before each command prompt. It seems to me that the $() would make bash try to execute whatever settitle returns. (I'm running bash on linux.)
    – tuomassalo
    Feb 9 '18 at 13:34
40

tmux rename-window -t${TMUX_PANE} "Title Text"

This is the proper way to set tmux titles in a window. The $TMUX_PANE variable is set by tmux and is used to differentiate between different panes.

2
  • 2
    As it's the active window, I don't think the -t${TMUX_PANE} is necessary (at least it wasn't for me). Dec 5 '12 at 17:03
  • 10
    @Christopher If you're running, say, sleep 3 and switch windows, the prompt would otherwise issue the command to the wrong window when sleep completes. (This is the reason I came here, +1).
    – quornian
    Apr 2 '13 at 2:04
20

Just for people who came here by searching how to change the title of a tmux session:

Ctrl + B, $

This will give you a prompt, where you can rename the active session.

To change the active window use komma instead:

Ctrl + B, ,

see: How do I rename a session in tmux?

1
  • Exactly, what I was searching for. Thank you Jun 29 at 5:30
9

Combining both Mikel's and UtahJarhead's answers, I used the following in .zshrc to solve this problem:

ssh() {
    tmux rename-window "$*"
    command ssh "$@"
    exit
}

I have automatic window renaming enabled by default, and I can't find a way to reenable it after exiting the remote host. Thus, I just exit the window entirely -- not an issue for my workflow. If you'd prefer to rename it to, say, 'bash', you could replace the exit line with tmux rename-window "bash".

1
  • 1
    I am using tmux rename-window hostname -s after the command ssh "@$", it basically "resets" the title to my "gateway" machine.
    – andrei
    May 16 '13 at 6:31
8

Instead of exit or tmux rename-window "bash" you can use

ssh() {
    if [ "$(ps -p $(ps -p $$ -o ppid=) -o comm=)" = "tmux" ]; then
            tmux rename-window "$*"
            command ssh "$@"
            tmux set-window-option automatic-rename "on" 1>/dev/null
    else
            command ssh "$@"
    fi
}

This re-activates the normal function that renames automatically window for next commands.

The if block prevents from (without it) renaming tmux current window from ssh commands used on other shells (out of tmux).

2
  • 1
    I've gone for a if env | grep -q "TMUX_PANE"; then instead of the ps -p... stuff, but same principle.
    – atomicules
    Apr 18 '18 at 7:52
  • 1
    Most common shells have $PPID defined, FWIW. But I prefer to use [[ -n "${TMUX_PANE:-}" ]] as the test - that way the whole test is done with string comparison builtins; no fork or subshell required. :)
    – dannysauer
    Aug 24 '20 at 15:32
1

I know this has been answered a long time ago, but I thought I would add what I have fiddled with and found (based on the accepted answer).. I have added this to the /etc/bashrc of all my servers (easy to do with fabric, puppet, etc)

settitle() {
    printf "\033k$1\033\\"
}
bash_prompt_command() {
    settitle "`hostname -s`:$PWD"
}
PROMPT_COMMAND=bash_prompt_command

And it sets the window name automatically in screen or tmux.

2
  • we use internal dns, so hostname -s removes the extra domain stuff, so that server1.internal.local shows up as just server1
    – Brian
    Feb 26 '13 at 16:11
  • It's ideal to always use hostname -s and hostname -f if you want just the short or fully-qualified hostnames, respectively. So, thumbs-up for that part. Though, technically man(1) hostname (see the FILES section) and man(5) hostname both indicate that there should probably not be periods in the contents of /etc/hostname. ;)
    – dannysauer
    Aug 24 '20 at 15:45
1

I would note in all of these examples with:

command ssh "$@"

You might want to grab the exitcode, and exit the function with it, otherwise things like:

ssh server false

Will return 0.

command ssh"$@"
_exitcode=$?
#other code
exit $_exitcode

Will exit ssh with the return code of the ssh.

1
1

Another solution would be to rename the active window to its previous name, after the ssh session :

ssh() {

local code=0
local ancien

ancien=$(tmux display-message -p '#W')

if [ $TERM = tmux -o $TERM = tmux-256color ]; then

    tmux rename-window "$*"

    command ssh "$@"

    code=$?
else
    command ssh "$@"

    code=$?
fi

tmux rename-window $ancien

return $code
}
0

This works for in tmux 2.1 and with zsh locally and on servers:

ssh() {
    tmux set-option allow-rename off 1>/dev/null
    tmux rename-window "ssh/$*"
    command ssh "$@"
    tmux set-option allow-rename on 1>/dev/null
}

I had to disable the allow-rename option manually before changing the windows name - otherwise it got changed to the current path on most of my servers (also using zsh there). The good thing is: if you reenable the allow-rename option it works immediately.

0

Add this to .profile or .bashrc

# Change window name for `tmux`
ssh() {
    if [ "$(ps -p $(ps -p $$ -o ppid=) -o comm=)" = "tmux" ]; then
        #tmux rename-window "$(echo $* | cut -d . -f 1)"
        tmux rename-window "$(echo $* | cut -d @ -f 2)"
        command ssh "$@"
        tmux set-window-option automatic-rename "on" 1>/dev/null
    else
        command ssh "$@"
    fi
}
0

First create a (callback) function to set/restore tmux window name like this, the default name here to restore is bash.

set_tmux_window_name()
{
    [[ -z "$TMUX" ]] && return
    [[ -z "$1" ]] && tmux rename-window "bash" || tmux rename-window $1
}

and then create a ssh wrapper which will restore the tmux window name back to bash as you defined once the ssh wrapper exited due to signals like KILL/TERM/ABORT/..

ssh_wrapper ()
{ 
    trap set_tmux_window_name SIGINT SIGQUIT SIGILL SIGABRT SIGEMT SIGFPE SIGKILL SIGBUS SIGSEGV SIGTERM

    ssh_cmd=$@
    [[ "$ssh_cmd" = "" ]] && return
    eval $ssh_cmd
}

now set the tmux window name before setting up the ssh connection

alias ssh1='set_tmux_window_name "ssh1"; ssh_wrapper username@hostname'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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