In command-line, custom text and background colors by directory
When in command-line, I need to set custom text and background colors by directory. Example:
cd /home/someuser/Documents [ENTER] # I have black text white background
cd /home/someuser/Public [ENTER] #I have white text on black background
How to achieve this?
One solution is to overwrite cd
with a function.
Let us say that I saved the function in a file named cd
at the $HOME
. Now I can start using it by $ source cd
and then $ cd ~/Documents
etc.
function cd(){
builtin cd "$@";
case "$PWD" in
"$HOME/Documents")
echo -ne "\033]10;#000000\007"
echo -ne "\033]11;#FFFFFF\007"
;;
"$HOME/Public")
echo -ne "\033]10;#FFFFFF\007"
echo -ne "\033]11;#000000\007"
;;
*)
# Any other place
echo -ne "\033]10;#FFFFFF\007"
echo -ne "\033]11;#000000\007"
;;
esac
}
Note that you can omit the $HOME/Public
part because # Any other place
would handle it anyway. I just leave it there as a reference so you can add any other color if you like.