It seems there are a lot of ways to control the colors you see in a terminal.

Colors can be controlled by the terminal emulator program you're using (e.g. in Terminator or the defualt Gnome Terminal's preferences), the bash shell (with $PS1 $LS_COLORS, etc), and through whatever other program you're using in the terminal like (vim, git, etc.). It seems there is some kind of hierarchy here where the color settings from the programs run in your terminal override the colors set in your terminal emulator's settings - e.g. colors set in vim override colors set in Terminator's preferences.

What determines what colors you see in your terminal? Any advice for how to learn about how this coloring works - main concepts or resources?

  • Assuming you have Ubuntu 18.04 or never, this is explained – much more briefly than the already posted answer here, though – in GNOME Terminal's Help pages, under the section "Color schemes". – egmont May 17 '20 at 10:41

There isn't a hierarchy per se. Instead you just have to understand what's really going on underneath the hood.

For this, it's important to know something about the history and what a terminal emulator is doing. Namely, it is literally emulating an old hardware terminal, such as the VT100. For example, the Wikipedia article on the GNOME Terminal indicates it is emulating the Xterm terminal emulator (which is more software), which itself was emulating the VAXStation 100 (which is a physical thing). These old terminals were serial devices---they saw the world as a stream of input, which is being generated by whatever program is currently running in the foreground in your terminal session.

So to get color, programs outputting to the terminal need a way to signal things like "this next text should be red." This is indicated by feeding the terminal an escape code, which tells the terminal the immediate next few characters are meant to be interpreted as commands, and not echoed for the user to see. In particular, to get color, the command means something like "Switch to displaying text in so-and-so-color" and later "Go back to displaying text in normal color."

If you read the article I linked, you'll see the earliest terminals supported selecting from 8 colors each in the foreground and background. The combined 16 colors are precisely what you are setting in the Colors dialogue in the GNOME Terminal preferences. This only makes sense because you are dealing with a software emulator, so you can exercise control of which actual colors should be chosen for the 16 colors the escape sequences can choose from. For another example, to configure Urxvt in the same way, you might modify your X resources file.

Later on terminals added support for choosing from among 256 colors and later 24-bit colors. These were implemented as new types of escape sequences. Terminal emulators don't typically let you customize these colors because they are supposed to be "real" colors, and not just a particular list of whichever 16 colors you like.

Some modern software, like Vim, will let you decide which type of color protocol you want to use. Usually you get the most precise control over colors when you use 16 colors because you can set them in your emulator's preferences, at the cost that your software only gives you 16 colors (e.g., that is the subject of this blog post). Sometimes software attempts to determine what level of color support your terminal emulator has, and then you have to fiddle with environmental variables to convey this information to that software. For instance, this vim page mentions you might want to set the envar TERM to the value xterm-256color if you want Vim to use the 256 colors approach.

(Edit: This is why you might think Vim is "overriding" your terminal settings. It isn't. It is probably just using a different type of color protocol, instead of the 16 colors you set.)

The environmental variables you refer to are used for specific purposes. E.g. PS1 is your shell prompt. When you see mangled-looking text like "[\033[01;32m]\u@\h[\033[00m]" in your PS1, you are looking at (a textual representation of) the precise escape sequences you want to use, some of which are commands to switch colors. The variable LS_COLORS is used similarly, but those settings are specific to the program ls.

Ultimately the colors are determined by the settings of your terminal emulator, and which escape sequences (and which type of color protocol) your user program is using.

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.