Posted 6 months ago. Visible to the public. Repeats.

Linux: How to make a terminal window title reflect the current path

By default, your terminal emulator (Gnome Terminal, Terminator, etc.) sets some kind of window title to reflect the shell type you are running (e.g. /bin/bash).
This is most often not too helpful, but you can change that from your shell.

To set a specific title, print an escape sequence like this:

Copy
echo -en "\033]0;Hello\a"

You can easily include the current path:

Copy
echo -en "\033]0;$(pwd)\a"

Or, to replace your home directory's part with a tilde:

Copy
echo -en "\033]0;$(pwd | sed -e "s;^$HOME;~;")\a"

Or, to only show the directory name without a path:

Copy
echo -en "\033]0;$(basename `pwd`)\a"

Bash

To make your Bash automatically update your window title whenever you switch directories, simply specify a PROMPT_COMMAND environment variable.

Copy
set-window-title() { echo -en "\033]0;$(pwd | sed -e "s;^$HOME;~;")\a" } if [[ "$PROMPT_COMMAND" ]]; then export PROMPT_COMMAND="$PROMPT_COMMAND;set-window-title" else export PROMPT_COMMAND=set-window-title fi

You may put that into your ~/.bashrc to persist and automatically activate when you open a new terminal window.

ZSH

To make your Bash automatically update your window title whenever you switch directories, add the following line to your precmd function in the ~/.zshrc. In case your are using oh-my-zsh you need to set DISABLE_AUTO_TITLE="true".

Copy
function precmd () { echo -ne "\033]0;$(pwd | sed -e "s;^$HOME;~;")\a" }

Demo video

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Arne Hartherz
Last edit:
about 1 month ago
by Arne Hartherz
Attachments:
terminal-title.gif
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more