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:
- open
~/.zshrc
- 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.
Two backslashes are missing in your zsh print command, the right version should be:
print -Pn “\e]0;%~\a”
first in google and a wrong answer. thanks for wasting my time.
It might be. 🙂 I would suggest to use oh-my-zsh (https://github.com/robbyrussell/oh-my-zsh) to save you more time.
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!
It might be easier if you use “bash-it” (https://github.com/Bash-it/bash-it) or oh-my-zsh (https://github.com/robbyrussell/oh-my-zsh)
You can use %1~ to get the current directory name only. There are links to the documentation in the explanation part of the article.
I had to add the following line after precmd() definition in order to make it work in Zsh 5.1.1.
precmd_functions+=(my_precmd)
Correction:
precmd_functions+=(precmd)
Or rename precmd to my_precmd just like I did.
How do we set title on csh?
Pingback: xfce4 terminal のタイトルを自動的にカレント・ディレクトリにする | ゴルディアスの涙目