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
  xterm*)
    precmd () {print -Pn "\e]0;%~\a"}
    ;;
esac

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
  xterm*)
    PS1="[e]0;wa]\$ "
    PROMPT_COMMAND='echo -ne "${USER}@${HOSTNAME}:${PWD/#$HOME/~}"'
    ;;
esac

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.

5 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”

    Reply
  2. dickface

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

    Reply
  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?

    Thanks!

    Reply

Leave a Reply

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