Ubuntu – Automatically get different terminal colors each time I open terminal

automationcolorscommand line

I often find myself opening three terminals and I really like the look and feel of having distinct color palettes on each.

I have a few color palettes saved and I would like the default to progress through my saved profiles each time I open a terminal, so that if I open 3 they are each different colors without me having to manually change profile on 2.

Any thoughts?

Thanks!

Best Answer

Functional Version

Instructions:

The Script considers you're using gnome-terminal, which is the default Ubuntu terminal.

Before running the script, open the gnome-terminal and create some profiles (Edit>Preference>Profiles) with different settings as you wish (background color, text color, ..). You can name them Profile1, Profile2, Profile3 and so on. Create enough Profiles to cover the quantity of Terminals that will be opened, but if a higher number of terminals are opened, the default profile will be used.

The script creates a file ~/.Bash_Color_Changer, which it depends on, since it will tell the script if the terminal was opened regularly or after a call on .bashrc.

Add the script to the end of your ~/.bashrc file.

Script:

Add to .bashrc:

#Change color according to the number of Bash shells opened
#Creates the .Bash_Color_Changer file if it's not present
if ! [ -f ~/.Bash_Color_Changer ]; then
    echo ORIGINAL > ~/.Bash_Color_Changer
fi

#Array holding the name of the profiles: Substitute it for the names you're using
Color_counter=(Profile1 Profile2 Profile3)
#Finds out the number of opened bashs counting the lines containing "bash"
#in the pstree function. (-c deactivates compact display to avoid it showing
#lines with "2*[bash]" instead of one for each bash)
Number_of_bashs=$(($(pstree -c | grep "bash" | wc -l)-1))

#Checks if the terminal being opened was opened by the user or by
#the script, and act according to it
if [ $(cat ~/.Bash_Color_Changer) = ORIGINAL ]; then 
    if ((Number_of_bashs < ${#Color_counter[*]})); then
        echo COPY > ~/.Bash_Color_Changer
        gnome-terminal --tab-with-profile-internal-id=${Color_counter[${Number_of_bashs}]} 
        exit
    fi
else 
    echo ORIGINAL > ~/.Bash_Color_Changer
fi

Tested but not extensively. Enjoy!