Search code examples
terminalansi-escapeterminfo

Reset only the foreground color using terminfo


By using the ANSI sequence Esc[39m in a terminal, it is possible to clear the foreground color without altering other attributes like bold, underline, or the background color. For example:

echo -e "\e[31;1mRed and bold.\e[39m Bold only."

I would like to retrieve this sequence from a terminfo capability, but I'm unable to find it; when trying with setaf 9, it switches to bright colors by giving the sequence Esc[91m:

$ tput setaf 1 | xxd
00000000: 1b5b 3331 6d                             .[31m
$ tput setaf 9 | xxd
00000000: 1b5b 3931 6d                             .[91m

The only capability I found to reset the foreground color is sgr0, but it resets all the other attributes as well.

Is it possible to access these capabilities from terminfo ?

  • Default foreground Esc[39m;
  • Default background Esc[49m;

Solution

  • You'll have to define it yourself. X/Open doesn't define it, and there's no established use for it. Assuming you're using ncurses (which is extensible), that can be done by modifying a terminal description and making up your own name for it, e.g.,

    infocmp -x > myinfo.src
    printf '\tresetf=\\E[39m,\n' >> myinfo.src
    tic -x myinfo.src
    

    (which would generally create your copy in $HOME/.terminfo/).