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.

enter image description here

function cd(){

    builtin cd "$@";

    case "$PWD" in

        echo -ne "\033]10;#000000\007"
        echo -ne "\033]11;#FFFFFF\007"
        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"

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.

  • 1
    pushd and plenty of other options would confuse this. Aug 4 '20 at 4:40
  • Yeah, that's correct. I only considered cd as OP suggested.
    – Ravexina
    Aug 4 '20 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.