Skip to main content

How to set terminal title in Ubuntu 18.04 LTS

In older Ubuntu versions, you could have just right-clicked on the Ubuntu Terminal window's title bar and set any title you would like. But unfortunately after Ubuntu 18.04 LTS this feature is gone. I used to love this feature because I'm multiple tabs in the single terminal window kind of a guy.

I usually like to work in multiple named terminal tabs like below.



By default, in newer Ubuntu version, it is showing just the current directory.


Let's see how we can do this.

Ubuntu prompt

In ubuntu's Bash, there's an environment variable $PS1 which is responsible for the details that the command line prompts.

You'll be able to echo this and see what's inside it.

If I echo it it will print something like this.

echo $PS1

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

If you really want to understand what this means, you can refer this page


Updating the terminal title

In order to update the terminal title, we'll have to provide a special char sequence with the title.

Let's say I want to update my current terminal as "test1"

Then I have to set the PS1 variable with the below char sequence.

PS1=$PS1"\[\e]0;test1\a\]"



Now, I've updated the title. Let's try to decipher the above char sequence.

According to the above-mentioned documentation;

\[  begin a sequence of non-printing characters

And it turns out to be 
\e]0; is the char sequence for setting the terminal title. Bash identifies this sequence and set the tile with the following characters. Number 0 turns out to be the value to reference the title property.

test1 is the tile we gave

\a is the ASCII bell character, also in this case, it marks the end of the tile to read from Bash.

\]  end a sequence of non-printing characters


Now, we might have done our job, but I don't want to remember all these characters. Instead, I'd like to use a simple function or a command to set my terminal title.


Let's write a simple function when I can use it to change my title.




function stitle(){
 if [ -z "$PS1_BACK" ];  # set backup if it is empty
 then
  PS1_BACK="$PS1"
 fi

 TITLE="\[\e]0;$*\a\]"
 PS1="${PS1_BACK}${TITLE}"
}

Here, I've backed up the previous value of the PS1 variable into $PS1_BACK just in case, I don't want to lose any of the existing values or functionality.

Then I'm setting a new variable called TITLE with my above-mentioned magic character sequence.

Here, you will find the $* variable instead of the actual text I want to use. It means all possible parameters in Bash. In our case, it would be any argument that I'm going to provide for this function.

To use this frequently you can append this to your ~/.bashrc file.

Open the .bashrc file in your home directory with your favorite text editor and append the above function at the end of it. Save it and close it out.


To use it immediately you can source it to the current terminal.

source ~/.bashrc


Then we are good to go!

stitle <the title of your terminal tab>





This function will be automatically available to use in your next reboot without having to source it.



Comments

Post a Comment

Popular posts from this blog

VLC skins

It's been a while from my first blog post, so today I thought to make a blog post about skins in VLC player. I think you all are familiar with VLC player if not, here's the link for the website www.videolan.org/vlc/index.htm l In brief, VLC player is a free and opensource player that can play almost all the types of media formats. Even though it is a such a nice player, it looks kind of old and rusty, like the windows classic look. But you can make it prettier by just adding some new skins to it.  You can download skins for the player from here http://www.videolan.org/vlc/skins.php Then place the downloaded skin file (.vlt file) in the " skin " folder in the installation directory.           eg:- in windows:    C:\Program Files\VideoLAN\VLC\skins           or in linux systems:  ~/.local/share/vlc/skins Then, open the VLC player and go to the preferences. ...

Java, how to create a list with a single element

 I wanted to create a Java List with a single element. Yet, I wanted to add more elements later. So, I was looking for a couple of ways to do this. So far there are multiple elegant ways to create a list with a single element with a one-liner. Not so much for a modifiable list though. Here's what I gathered so far. Followings are a few ways of creating a list with strictly a single entry. Can't add more elements. 1. Collections.singletonList() This returns an immutable list that cannot be modified or add more elements. // An immutable list containing only the specified object. List<String> oneEntryList = Collections. singletonList ( "one" ) ; oneEntryList.add( "two" ) ; // throws UnsupportedOperationException 2.  Arrays.asList() This returns a fixed-size list consisting of the number of elements provided as arguments. The number of elements provided would be an array hence the size is fixed to the length of the array. // Returns a fixed-size list List...

Find command: How to find and delete 0 bytes files in Linux

In Linux, "find" command is a powerful command that we can use not only to find files. I've used it for different requirements, so thought of posting some of the convenient usages of find commands in a series of posts. For some application issue, I've got one of my directories in Ubuntu system swarmed with empty files with 0 bytes. Some of the files got data in them but some of them were empty. I just wanted to delete the empty files and it wasn't possible to identify them by file names or dates. So find command comes to rescue. find . -size 0 -type f  If I needed to isolate the files by file extension, we can use something like this. find . -size 0 -type f -name "*.tar" You can get the file count by piping it to wc command. find . -size 0 -type f -name "*.tar" | wc -l The awesome thing about the find command is you can use it for deleting files just by adding the -delete flag to the command. find . -size 0 -type f -name "*.tar" -dele...