Linux – Change Search Highlight Color In Less

colorslesslinuxpagertermcap

I am a bit lost here on customizing the search/find highlight color or font format if you will. I understand that using LESS_TERMCAP_* we can alter the font format of less display. Full capabilities are here.

But I couldn't get how to change the search highlight! I want to add background color and change fore color to make them different from the standard output colors. Negating the standard output is preferred.

Also, one more question. A parameter with special characters (like @0), how can we write their LESS_TERMCAP_ variable?

It's getting really annoying not being able to even guess which one to use to change the colors! Thanks in advance for shedding some light on the matter.

Best Answer

There is a list of different termcaps global variables that are read by less; the relevant ones are found in the code as:

tmodes("so", "se", &sc_s_in, &sc_s_out, "", "", &sp);
tmodes("us", "ue", &sc_u_in, &sc_u_out, sc_s_in, sc_s_out, &sp);
tmodes("md", "me", &sc_b_in, &sc_b_out, sc_s_in, sc_s_out, &sp);
tmodes("mb", "me", &sc_bl_in, &sc_bl_out, sc_s_in, sc_s_out, &sp);

The tmodes function prefixes its first two arguments with LESS_TERMCAP_ and uses the value of the env variable with that name for, as described by user jimmij:

  • so standout, se exit standout,
  • us underline, ue exit underline,
  • md bold, me exit underline,
  • mb blinking, me exit blinking (and underline).

You can have your matches appearing in red using:

$ export LESS_TERMCAP_so=$(echo -e '\e[1;91m')
$ export LESS_TERMCAP_se=$(echo -e '\e[0m')

See this Wikipedia page for more on ANSI escape sequences (including background coloring, bold, ...).

It's worth noting that less reads much more LESS_TERMCAP_* variables than just these (for keys, for instance).