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– Arulpandiyan VadivelApr 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.– egmontApr 19, 2017 at 18:17
3 Answers
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.– tripleeeApr 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!– AhLeungApr 25, 2017 at 8:13
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.
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.
-
1It 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"
). Apr 19, 2017 at 22:58 -