I am using a terminal with a white/light background, but output of a command I used contains text with white color (i.e., foreground color = white). Is there any way (e.g., using shell script?) to map all white foreground to black foreground, and keeping the other colors unchanged?

  • Below link will help you to get your question answered how-to-change-the-output-color-of-echo-in-linux Commented Apr 19, 2017 at 5:38
  • The best would be if you could fix your script not to change the foreground to black or white without changing the background as well - it should change to the default foreground instead.
    – egmont
    Commented Apr 19, 2017 at 18:17

3 Answers 3

your_script | perl -pe 's/\e\[((?:0;)?)97m/\e[${1}30m/'

Feed your script output to the above given Perl code which transforms all escape color sequences for white foregroud => black fg.

  • More generally colcrt strips display formatting from a text stream. Of course, if the command you already ran cannot easily be run again (because it has external effects which are undesirable, or it takes a long time, etc) this doesn't help at all.
    – tripleee
    Commented Apr 19, 2017 at 6:56
  • I found a solution on my system (iterm on Mac) based on your solution: your_script | perl -pe 's/\e\[1m/\e[0;30m/g'. Thanks!
    – AhLeung
    Commented Apr 25, 2017 at 8:13

The precise features of your terminal will differ between versions and implementations. Some traditional terminals offer very little by way of individual window customization, while many desktop OSes have spiffy terminals where you can freely adjust the colors of an individual window by opening the preferences.

As a quick and dirty workaround, selecting the text with the mouse often applies a different color palette which might help. If it doesn't, pasting the text into another window where you have the colors you want is usually a small additional step.

  • 1
    It is possible to remap colors in xterm. There's no GUI for it but there are escape sequences (printf '\e]4;%d;%s' "$color_number" "$color_name_or_hash_rgb"). Commented Apr 19, 2017 at 22:58
  • @Gilles Thanks! I changed the phrasing somewhat.
    – tripleee
    Commented Apr 20, 2017 at 3:04

It depends on the terminal. In xterm, you can change the mapping of color numbers to color names with the escape sequence OSC 4 ; NUMBER ; NAME BEL. Explicit white is color 7, so:

printf '\e]4;7;black\a'

This affects the color whether it's used as a background or foreground. It's a rendering setting, i.e. it affects all output past and future.

Few other terminals support this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .