GNOME Terminal - process name in tab title

9

4

How can I put the current running process name into a GNOME Terminal tab title (or title bar when there's only one tab)?

-- UPDATE --

To clarify, I want the tab title to update when I run a process, for example:

# title is currently "bash"
$ find / -name foo # while searching for foo, title is "find"
$ # title is once again "bash"
$ less /proc/cpuinfo # title changes to "less"
$ man ls # title changes to man
$ # title returns to "bash"

adurity

Posted 2009-09-16T21:20:00.813

Reputation: 822

Answers

8

Found it. This site provides a good explanation of a solution.
In your bashrc, it would look like:

case "$TERM" in
xterm*|rxvt*)
    set -o functrace
    trap 'echo -ne "\e]0;$BASH_COMMAND\007"' DEBUG
    PS1="\e]0;\s\007$PS1"
    ;;
*)
    ;;
esac

Personally, I don't think I would add it to my bashrc, because the DEBUG combined with trace does throw out a load of garbage when all your shell starts. If you can live with that, it actually does work. It does display the entire command, however, not just the first word.

DaveParillo

Posted 2009-09-16T21:20:00.813

Reputation: 13 402

why the PS1-assignment? it results in glibberish for me, and it doesnt really seem necessary here...? – phil294 – 2017-05-05T02:41:40.840

2Thank you for asking a good question. This looked like a simple question at first...

After the first 10 minutes, it was starting to drive me nuts!

That's why I thought it was a good question - sounds simple, but forced me to learn something deeper about the interaction between the shell, the terminal and signals. – DaveParillo – 2009-09-21T17:10:19.443

3

Well, since everyone already seems to know David Pashley's solution I'm kind of surprised it took me so long to find this one because it's almost as old.

This solution actually takes care of bash-completion spamming garbage.

To be clear: I did nothing on my own here but research. All credit goes to Marius Gedminas.

This works perfectly for me with Gnome-Terminal/Terminator

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'

    # Show the currently running command in the terminal title:
    # http://www.davidpashley.com/articles/xterm-titles-with-bash.html
    show_command_in_title_bar()
    {
        case "$BASH_COMMAND" in
            *\033]0*)
                # The command is trying to set the title bar as well;
                # this is most likely the execution of $PROMPT_COMMAND.
                # In any case nested escapes confuse the terminal, so don't
                # output them.
                ;;
            *)
                echo -ne "\033]0;${USER}@${HOSTNAME}: ${BASH_COMMAND}\007"
                ;;
        esac
    }
    trap show_command_in_title_bar DEBUG
    ;;
*)
    ;;
esac

Also this is a cross-post because I just found out about it and wanted to share and I think it's useful here as well.

user173944

Posted 2009-09-16T21:20:00.813

Reputation:

2

The below should work. I have the function in a .bash_functions file, and source it in the .bashrc file before setting $PROMPT_COMMAND.

function term_title
{
        history 1 | awk '{print $2}';
}

PROMPT_COMMAND='echo -ne "\033]0;"$(term_title)"\007"'

user4358

Posted 2009-09-16T21:20:00.813

Reputation:

1You're close, but I don't have much better. This will give you the last executed command, but not really what adurity is after. I thought just changing term_title to : if [ jobs ]; then history 1 | awk '{print $2}'; else echo -ne 'bash em' fi would work, but it's inconsistent about catching processes with short lifetimes and it still doesn't switch back when the process is finished. I don't know about the innerworkings of xterm to know how to mess with those triggers/ events. – DaveParillo – 2009-09-18T01:21:50.050

bash executes $PROMPT_COMMAND just before displaying the prompt, so this is the best I can do with this approach. There might be other triggers that can execute right after you hit Enter, but I don't know of them. – None – 2009-09-18T08:01:04.580

2

in zsh you just define your 'precmd' function. see here.

akira

Posted 2009-09-16T21:20:00.813

Reputation: 52 754