How to dynamically change background color in Konsole

I like a (very) clear visual indicator when I am in a root shell, so I like it when the background of the window is a red color so I don’t do anything too stupid when I sudo to root.

NOTE: I use the fish shell, so you should adapt to what you use.

So I added this to my fish_prompt (both my regular user and root):

function __check_term
    if [ (id -u) = 0 ]
        printf '\x1b]11;#82181A\x1b\\'
    else
        printf '\x1b]11;#303446\x1b\\'
    end
end

Shells like Kitty or Alacritty allow for programmatically changing colors and more, but Konsole does not.

With the above, s soon as I sudo -i the background on the shell changes to red, and when I exit back to my regular user, it changes back to the normal background I have configured. Works in any terminal emulator that supports escape characters, which should be most.

Maybe this helps someone here too.

1 Like