29

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?

orschiro
  • 1,005

1 Answers1

34

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

slm
  • 369,824
  • 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 06 '13 at 14:21
  • Incorporate it into the prompt, $PS1 would be the way to go. – slm Dec 06 '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: http://pastebin.com/jqbE7W2Y – orschiro Dec 06 '13 at 15:07
  • 1
    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 05: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. –  May 20 '16 at 21:30
  • this works fine, however the trap breaks the use of coloring. see this question https://unix.stackexchange.com/questions/368935/updating-terminal-title-in-debug-trap-breaks-coloring – phil294 Jun 03 '17 at 13:43
  • What terminal is this answer using ? (does it matter ?) How does this work, what are \033]0; and \007 ? What does trap do which makes it helpful ? My PROMPT_COMMAND is "history -a" and I'd like to keep that part, how does that interact with your answer ? I find this answer easy to use, but hard to understand. (Looks tailored to help vampires :D) – Nikana Reklawyks Jul 12 '17 at 17:31
  • BTW -- do we need the exports here? My expectation reading this code is that the variables are all used inside the same process, so just directly assigning them (without any explicit declare or local to prevent them from being shell-global) should suffice. (Happy to propose my own edit, if that works -- I believe my rep on this site is low enough that it would go in the review queue for your approval). – Charles Duffy Jan 23 '18 at 21:26
  • You'll need to make sure the DEBUG trap's output goes to /dev/tty to avoid causing bugs such as Why is my DEBUG trap executed (w/ content redirected) in { echo foo; echo bar; } >file? – Charles Duffy Mar 26 '19 at 11:52