2

Can I make bash automatically substitute one color code for another? For example, whenever a program outputs text wrapped in color code for bold foreground, I would like bash to actually use code for green background, or something.

The reason I want this is because my terminal emulator (Terminator) doesn't allow me to change the color of bold foreground - for example, echo -e "\e[1;39m Foo" will always output "Foo" in white, regardless of the color palette I set, which produces ugly results. I would like to make bash use \e[1;37m code (bold white, which I can change in Terminator preferences) instead of \e[1;39m.

Edit: from what I understand, doing what I want is more about things like termcap or terminfo than bash itself. Anyone experienced with these two?

Jan Warchoł
  • 3,091
  • 4
    That won't be trivially possible in a »bashish« way, as bash is the wrong program to ask for it. The shell does not interpret output of other programs. It's the terminal emulators task to do this. If your terminal emulator cannot do this for you, you should consider replacing it by some that actually will. What kind of b0rken emulator is this Terminator-thingy‽ ;) – Andreas Wiese Mar 16 '16 at 11:45
  • Or as a horrible kluge you'd need to do something like http://unix.stackexchange.com/questions/269002/can-i-have-a-bash-shell-with-expect-running-in-the-background and then muck around with the control sequences as they fly by. Probably better to change the terminal. – thrig Mar 16 '16 at 14:03
  • @AndreasWiese well, there are things like termcap and terminfo that are supposed to tell other programs what escape sequences to use. Thanks to http://unix.stackexchange.com/a/147/32950 I was able to do just what I want for less - and I was hoping I could do something similar for all other programs. Unfortunately I have trouble finding good examples showing how to use termcap... – Jan Warchoł Mar 16 '16 at 14:50
  • Problem I see here is that less actually interprets the content it displays, resp. it actually displays it. bash just executes commands; those commands are usually responsible for their output themselves. – Andreas Wiese Mar 16 '16 at 15:05
  • Ok, I see that it's not bash's business. But I hope that someone experienced with termcap can tell whether that tool can be of any help to me. – Jan Warchoł Mar 16 '16 at 15:09
  • @AndreasWiese as for Terminator, it's a quite nice emulator apart from this one issue. What do you recommend instead? – Jan Warchoł Mar 16 '16 at 15:10
  • What version of Terminator are you using? I assume 0.97 or 0.98. Those rely on an ancient, unmaintained and quite buggy version of VTE, the actual terminal emulation library. I strongly advise you to change to Terminator's gtk3 branch (in bazaar; no tarball released yet as of now). This one uses a much newer VTE where I'm quite certain that this bug is no longer present. – egmont Mar 16 '16 at 15:33
  • @egmont I tried running terminator from source but failed (maybe because of my unfamiliarity with bazaar). Where should I ask for help with this, here on Stack Exchange or somewhere on Launchpad? – Jan Warchoł Mar 16 '16 at 22:00
  • 1
    I'm afraid termcap/terminfo won't help you either, because that's not where colors go through. The normal solution would be to use a terminal emulator that supports configuring colors. There may be a way by running your program under an intermediate layer like screen or tmux but I don't think either of them support rewriting colors. – Gilles 'SO- stop being evil' Mar 16 '16 at 22:19
  • @Gilles bad luck for me. As there are no other solutions posted, please make it an answer and I will accept it. – Jan Warchoł Mar 19 '16 at 14:18
  • @JanekWarchoł I thought that vte didn't implement color remapping, but it turns out that it does, so most of what you're asking is possible. I'm not sure how precise you need the remapping to be, for example if you need to configure background and foreground independently I think you're out of luck. – Gilles 'SO- stop being evil' Mar 19 '16 at 17:23

1 Answers1

1

Bash has no say about the colors that applications use, except of course what bash itself uses. Applications interact with the terminal; bash only starts them and notices when they finish.

Applications know how to talk with a terminal through the termcap (traditional) or terminfo (modeern) database. Termcap is older than colors, but terminfo does have some knowledge of colors. Look at the terminfo(5) man page for a list of supported capabilities on your system. There's no way to specify control sequences for individual colors, but you can achieve essentially the same result by configuring the appearance of each color through the initc capability. For example this changes the apperance of color 1 (normally red) to bright green:

tput initc 1 0 255 0

The ncurses database doesn't include this capability for terminator or for xterm though (as of version 5.9 on Debian jessie). However these terminal emulators (as well as any other terminal based on vte) do support an escape sequence which can be used for that purpose: OSC 4 ; c ; spec BEL. This is how to make red into bright green:

printf '\e]4;1;#00ff00\a'

The basic colors are numbered 0–7; bold text in those colors uses colors 8–15. You can also use OSC 5 ; 0 ; spec BEL to change the color used for bold in the default color, and so on (see the control sequence list for details).

These settings affect both foreground and backgrouns colors, there's no way to affect foreground and background separately.

To customize the appearance of colors, you would output these control sequences from your .bashrc. That would only affect terminals where you run an interactive instance of bash, not terminals where you start another application directly.

I don't know how this interacts with Terminator's color palette settings.

  • That doesn't answer OP's question (although OP may suppose so). – Thomas Dickey Mar 19 '16 at 17:31
  • @ThomasDickey While it doesn't do exactly what the question asks, it's as close as it gets, isn't it? It isn't clear to me whether that's enough to solve the underlying problem (I don't understand exactly how much control over the attribute combinations is required). – Gilles 'SO- stop being evil' Mar 19 '16 at 17:55
  • @Gilles hmm. printf '\e]4;1;#00ff00\a' does change my red to green (although it changes back as soon as the terminal window looses focus), but printf '\e]5;0;#00ff00\a' (nor any other combination I tried) doesn't affect foreground color (which is not the same as "white" color). – Jan Warchoł Mar 19 '16 at 22:27
  • @Gilles I have tried printf '\e]5;0;#00ff00\a' in terminator, gnome-terminal, konsole, urxvt and xterm, and none worked :-( (while printf '\e]4;1;#00ff00\a' worked everywhere except Konsole). Am I doing something wrong or should we change the answer to say that changing bold fg color is impossible? I'd like to accept it. – Jan Warchoł Mar 23 '16 at 11:48
  • @JanekWarchoł Oh. With xterm, you need to run it with the resource XTerm.vt100.colorBDMode: 0 or the command line option +bdc if you want to use color instead of bold. If you want both bold and color, use the resource XTerm.vt100.veryBoldColors: 255 as well. I don't know if vte has something analogous. – Gilles 'SO- stop being evil' Mar 23 '16 at 12:45