How to set terminal title dynamically to the current working directory?

By | October 24, 2013

It is sometimes helpful to set a terminal window title from a script, so that you can put a couple of reminders of how to do things there. The Xfce4-terminal preference tells me it can be done by setting the “Dynamically-set title” position, but never says how to dynamically put text in the title bar.

Using Zsh

As I’m gradually switching to Zsh, this short post explains how to dynamically set pwd, or current working directory, to xfce terminal in Zsh. Actually, it only contains two steps:

  1. open ~/.zshrc
  2. add the following code
case $TERM in
    precmd () {print -Pn "\e]0;%~\a"}

Let me explain this piece of monstrosity a little bit. I think the most difficult part is

precmd () {print -Pn "e]0;%~a"}.

In XTerm, window and icon titles may be changed in a running xterm by using XTerm escape sequences.

  • ESC]0;stringBEL — Set icon name and window title to string
  • ESC]1;stringBEL — Set icon name to string
  • ESC]2;stringBEL — Set window title to string

where ESC is the escape character, and BEL is the bell character. In ASCII, you can use e (or �33) and a (or �07) to represent them respectively. Therefore, if I would like to choose option 1, the escape sequence will be “e]0;stringa”. This is very similar to the string enclosed in the braces above. In Zsh, %~ represents current working directory. So together with the XTerm escape sequences, we got e]0;%~a.

The remaining part is straightforward. In Zsh, precmd will be executed before each prompt. This means when you open a new terminal, whether in a new window or in a new tab, this command will be run first. And print -Pn prints the sequence to the $PROMPT parameter without adding a newline.

So in summary, precmd () {print -Pn "\e]0;%~\a"} means, before each prompt, set the icon name and window title to the current working directory. Of course the above code is limited to xterm in zsh script. But following the same logic, you can adjust it to other terminals (by adding more cases like eterm and konsole).

Using Bash

Also you can change it into bash or csh script by modifying it to their specific grammars. For your convenience, I am provoding a Bash script as well. You need to add it into ~/.bashrc.

case $TERM in
    PS1="[e]0;wa]\$ "
    PROMPT_COMMAND='echo -ne "${USER}@${HOSTNAME}:${PWD/#$HOME/~}"'

In the above code, PS1 has same effect as print -Pn in zsh, which displays a string in the terminal title. Here w means current working directory as well. In bash, PROMPT_COMMAND will be executed just before displaying the PS1. Therefore it is kinda equivalent to precmd in zsh.

10 thoughts on “How to set terminal title dynamically to the current working directory?

  1. Javran

    Two backslashes are missing in your zsh print command, the right version should be:

    print -Pn “\e]0;%~\a”

  2. dickface

    first in google and a wrong answer. thanks for wasting my time.

  3. J.B.

    Thanks for your good solution!
    How to set terminal title dynamically to the last part of current working directory?
    For example, I’m in ~/Document/Workspace, I just want the title to be Workspace.

    %~ represents current working directory. How to get the last part of the directory?


    1. Melebius

      You can use %1~ to get the current directory name only. There are links to the documentation in the explanation part of the article.

  4. Melebius

    I had to add the following line after precmd() definition in order to make it work in Zsh 5.1.1.


    1. Melebius



      Or rename precmd to my_precmd just like I did.

  5. Pingback: xfce4 terminal のタイトルを自動的にカレント・ディレクトリにする | ゴルディアスの涙目

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.