Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

thanks.

share|improve this question

5 Answers 5

up vote 5 down vote accepted

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.

share|improve this answer
    
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. –  UtahJarhead Oct 8 '12 at 18:43

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.

share|improve this answer
1  
As it's the active window, I don't think the -t${TMUX_PANE} is necessary (at least it wasn't for me). –  Christopher Dec 5 '12 at 17:03
    
@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

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".

share|improve this answer
    
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

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.

share|improve this answer
    
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

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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