26

Is there a way to set the gnome-terminal title from within the terminal itself without having to right click on the tab. Something like:

active-terminal --title "Foo"

There was a related question previously with an answer that almost lets you do this: How to change Gnome-Terminal title? but that doesn't set the gnome-terminal tab title only the window title.

1
  • There appears to be a long-standing bug that affects this functionality in current versions of Ubuntu. See here
    – Elder Geek
    Sep 9, 2019 at 17:31

3 Answers 3

47

The following will set the terminal's title to "New terminal title":

echo -en "\033]0;New terminal title\a"

You will probably also have to change the environment variable PS1, first though, otherwise your changes won't show up because it will reset the title after each command. The default .bashrc that ships with Ubuntu contains the following line:

PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"

... the "\e]0;" code tells it to write everything up to the "\a" in both the title and icon-name properties. You need to remove that and set it to something like this (i.e. without the \e]0; code):

PS1="${debian_chroot:+($debian_chroot)}\u@\h \w\a$ "

Then any changes that you make with the above echo command will change the terminal title. If you're going to use this a lot, you can throw it into a function in your ~/.bashrc file:

set_term_title(){
   echo -en "\033]0;$1\a"
}

Then you can just set the title to "kittens" from the command line by doing:

set_term_title kittens

(You have to restart bash though after editing .bashrc, for your changes to take effect)

3
  • Oh great! I would never have figured that out. I changed it so set_term_title exports the updated PS1 variable, before setting the title since I'm sure I'd like to keep the default behavior unless I'm explicitly setting the title. :)
    – Kit Sunde
    Mar 19, 2011 at 13:49
  • 3
    I have updated my /etc/bash.bashrc with the PS1 you specified, but after setting the title, using the command also from your answer, nothing updates. I am using Ubuntu 11.04. Anything changed?
    – Sander
    Jun 7, 2011 at 6:50
  • On Ubuntu 12.04, I find that rather than PS1="${debian_chroot:+($debian_chroot)}\u@\h \w\a$ ", I need to use PS1="${debian_chroot:+($debian_chroot)}\u@\h \w$ ", that is, removing the \a, or else I get line-wrapping problems, like those described here: bbs.archlinux.org/viewtopic.php?id=48910 Mar 10, 2015 at 3:47
0

Function declarations don't work in all shells, so another approach is to declare an alias like so:

alias cd    'cd \!*; echo -en "\033]0;`pwd`\a"'

This command in particular causes the title to change to whatever the pwd is.

Of course you will want to initialize the title when you start the terminal, so don't forget to include gnome-terminal --title.

I use a perl script to helps me determine all the argument values first and then it invokes a new terminal like so:

my $cmd = "gnome-terminal $window_with_profile $geometry $zoom $working_directory $temp_argv $title &";
system($cmd);

But how you want to initialize those values is up to you...

Your welcome to take the following code and tweak it for your own personal use if you want:

    #!/usr/bin/perl
    #use strict;
    use Getopt::Long;

    my $progName = "term.pl";

    =pod

    =head1 OBJECTIVE: open a gnome-terminal with a given path and a new background color

     #1# In order to use this script you first need to set up 10 different terminal profiles each named "theme1" - "theme10"
        Edit... profiles... new... theme1
        Each theme should use a different color scheme...

     The themes are later called with --window-with-profile=theme$int
     This script then selects the next one one to open based on the number saved in the ~/.term_theme_counter file.

     ### The argument "." opens the terminal with the same dir as you are currently in. 
         Without it, the terminal opens to "~". Use --working-directory=<DIR> for others. 
         Also, -dir=<DIR> has been added for convenience

     ### You can still pass additional gnome-terminal arguments like: --tab_with_profile etc into the command 

     ### Also see gnome-terminal --help 
            and gconf-editor and gconftool-2  --> /apps/gnome-terminal/profiles/ 
            for editing terminal props

      EXAMPLES:

      term.pl .
      term.pl /cadtools/tech/

    Credits: This script was written by Damian Green over the years but first posted publicly in 2013   

    =cut

    sub usage{
        system("perldoc $progName");
    };

    my $opt_h = "";
    my $geometry = "";
    my $zoom = "";
    my $window_with_profile = "";
    my $working_directory = "";
    my $temp_argv = " @ARGV ";

    #my $counter = int(rand(10));
    ###lets keep a running counter instead
    my $counter = 0;

    my $home = $ENV{"HOME"};
    $home = abs_path($home);

    my $counter_file = "$home/.term_theme_counter";
    if (-f $counter_file){
        open (INFILE, "< $counter_file");
        my @contents = <INFILE>;
        close INFILE;
        $counter = @contents[0];
    }else{
        open (OUTFILE, "> $counter_file");
        print OUTFILE $counter; 
        close OUTFILE;
    }

    $counter++;
    if ($counter > 10){
        $counter = 1;
    }   
        open (OUTFILE, "> $counter_file");
        print OUTFILE "$counter\n";
        close OUTFILE;

    use Cwd 'abs_path';
    my $pwd = abs_path();#expands /cadtools to /data/mmc/emc/cadtools_lnx/cadtoolsmy 
    my $title_path = ""; 

    ### first of all pull out the "." if there is one...
    if ($temp_argv =~ m/(\s+)(\.)(\s+)/){
        my $arg = $1.$2.$3;
        my $val = $2;
        $temp_argv =~s/\Q$arg\E/ /;                     #<- remove the arg from the temp_argv
        unless ($temp_argv =~ m/--working_directory/){
            $working_directory = "--working-directory=$pwd";#<- #<- set the new working dir
        }
        $title_path = $pwd;
    #}elsif ($temp_argv =~ m/(\s+)(\S+)(\s+)/ and -d $2){
    }elsif ($temp_argv =~ m/(\s+)((?!-)\S+)(\s+)/ and -d $2){
        my $arg = $1.$2.$3;
        my $val = $2;
        $val = abs_path($val);
        $temp_argv =~s/\Q$arg\E/ /; 
        unless ($temp_argv =~ m/--working_directory/){
            $working_directory = "--working-directory=$val";
        }
        $title_path = $val;
    }elsif ($temp_argv =~ m/(\s+)(--?dir=)(\S+)(\s+)/ and -d $3){# and -d $2){
        my $arg = $1.$2.$3.$4;
        my $val = $3;
        $val = abs_path($val);
        $temp_argv =~s/\Q$arg\E/ /; 
        unless ($temp_argv =~ m/--working_directory/){
            $working_directory = "--working-directory=$val";
        }
        $title_path = $val;
    }elsif($temp_argv !~ m/--working_directory/){
        $working_directory = "--working-directory=$home";
        $title_path = "$home";
    }

    if($temp_argv =~ m/(\s+)(--?geometry=)(\S+)(\s+)/){
        $geometry = $3;
        my $arg = $1.$2.$3.$4;
        $temp_argv =~s/\Q$arg\E/ /; 
    }
    if($temp_argv =~ m/(\s+)(--?window-with-profile=)(\S+)(\s+)/){
        $window_with_profile = $3;
        my $arg = $1.$2.$3.$4;
        $temp_argv =~s/\Q$arg\E/ /; 
    }
    if($temp_argv =~ m/(\s+)(--?zoom=)(\S+)(\s+)/){
        $zoom = $3;
        my $arg = $1.$2.$3.$4;
        $temp_argv =~s/\Q$arg\E/ /; 
    }
    if($temp_argv =~ m/(\s+)(--?h)(elp)?(\s+)/){
        &usage(); exit;
    }

    if (!$geometry){
        $geometry = "--geometry=150x30+180+500";
    }else{
        $geometry = "--geometry=$geometry";
    }
    if (!$zoom){
        $zoom = "--zoom=1";
        ### some machines have a small zoom by default and so you can adjust it here for different machines if you want.
    }else{
        $zoom = "--zoom=$zoom";
    }
    if (!$window_with_profile){
        ### if gnome themes arent working on your machine, you may have to comment the following line out...
        $window_with_profile = "--window-with-profile=theme$counter";
    }else{
        $window_with_profile = "--window-with-profile=$window_with_profile";
    }

    my $title = "--title=$title_path";

    my $cmd = "gnome-terminal $window_with_profile $geometry $zoom $working_directory $temp_argv $title &"; #--sm-client-id=greend12

    print "$cmd\n";
    system($cmd);
-1

In Ubuntu 12.10 ( I am not sure about previous versions) there is the following line in default .bashrc:

If this is an xterm set the title to user@host:dir
 case "$TERM" in
 xterm*|rxvt*)
 PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
  *)
;;
esac

Therefore, to have the title in the form you want, you have just to edit the value of PS1 at this part. If you want, for example, to have the title as the name of the current directory just change \u@\h: to \w to \W

Not the answer you're looking for? Browse other questions tagged .