11

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?

15

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

    "$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.

| improve this answer | |

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.