27

This question already has an answer here:

I am looking for a way to dynamically set the urxvt window title based on the command input.

Let's take an example.

If I run mplayer http://66.197.229.245:8082 in an urxvt window, I would like the title of this window to be set to mplayer http://66.197.229.245:8082 or even better Terminal | mplayer.

Or if I run journalctl -b I want the window title to be set to Terminal | journalctl -b or better Terminal | journalctl.

Is this possible?

marked as duplicate by Gilles 'SO- stop being evil', slm, jasonwryan, rahmu, Anthon Dec 7 '13 at 4:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

31

Assuming you're using Bash as your shell you can set this variable.

PROMPT_COMMAND='echo -ne "\033]0;Terminal | mplayer\007"'

Examples

   ss #1

   ss #2

   ss #3

Including the previous command in the title?

If you want a more elaborate title bar then you could use this method to dynamically set the currently running command in the title bar.

 $ trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG

Example

   ss #4

   ss #5

What you're looking for

To get what you want you'll need to do some work to parse out just the name of the executable, for example. So you'll have to make some decisions as to what part of the command you want. You could use .. | awk '{print $1}' to get the name of just the command.

Making it permanent

If you want to make this the default behavior via your /etc/bashrc or $HOME/.bashrc files, you'll likely need to follow @simon's suggestions in this SO Q&A titled: Bash - Update terminal title by running a second command.

Simon's answer is as follows:

function settitle () {
    export PREV_COMMAND=${PREV_COMMAND}${@}
    echo -ne "\033]0;${PREV_COMMAND}\007"
    export PREV_COMMAND=${PREV_COMMAND}' | '
}

export PROMPT_COMMAND=${PROMPT_COMMAND}';export PREV_COMMAND=""'

trap 'settitle "$BASH_COMMAND"' DEBUG

Redirection of output

As referenced in this SO Q&A titled: Why is my DEBUG trap executed (w/ content redirected) in { echo foo; echo bar; } >file? there's an answer there by @Charles Duffy which recommends redirecting output to STDERR or even better /dev/tty. His answer has good guidances on both these methods.

References

  • Thanks! I added trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG to my .bashrc to have this setting permanently. But now for every new urxvt terminal opened, I am getting this output: 0;%s@%s:%s" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" . How can I suppress that? – orschiro Dec 6 '13 at 14:21
  • Incorporate it into the prompt, $PS1 would be the way to go. – slm Dec 6 '13 at 14:36
  • I still cannot get it working. I tried assigning the trap command to a variable to add it to PS1. But how do I include the DEBUG in the variable assignment? For now my .bashrc looks as follows: pastebin.com/jqbE7W2Y – orschiro Dec 6 '13 at 15:07
  • The funny looking output is because the DEBUG trap interferes with your $PROMPT_COMMAND, which is also trying to set the title. I worked around it with a bit of awk: trap 'echo $BASH_COMMAND |awk '\''!/\007/ {printf "\033]0;%s\007", $0}'\' DEBUG – sqweek Nov 17 '15 at 5:56
  • 1
    Could you add an explanation of how this works? I have a sneaking suspicion it's an escape code of some sort because \033 is 0x1B, the escape indicator, but the ] seems to indicate that it's console-specific, and I can't find the specific thing for Bash anywhere. – Nic Hartley May 20 '16 at 21:30

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