30

To my understanding man uses less as a pager, and when searching for keywords using less it "highlights" keywords with italics. I find that really inconvenient, so I'd like to change this to something like vim's set hlsearch where the found pattern has a different background.

I attempted to run man -P vim systemd but that quit with error status 1, so it looks like I'm stuck with less.

There was nothing that I was able to find in man less that helped (instead I found out that option -G will turn off highlighting all together which is even worse than italics).

That being said does anyone know how to achieve search highlighting (change background color) in man pages?

FYI I run Ubuntu 14.10

I came across this question seems to ask about the same thing but I am not sure if I follow how does this work (LESS_TERMCAP_so). The less man page does not mention this. (I get strange results with this solution)

4
  • It means you place that line in your ~/.bashrc file. export LESS_TERMCAP_so=$'\E[30;43m' See this answer as well unix.stackexchange.com/questions/38634/…
    – iyrin
    Jan 15, 2015 at 2:52
  • 1
    Sometime I use man man | vim - to take advantage of my vim configuration, keys and functions
    – JJoao
    Jan 15, 2015 at 8:45
  • Did you figure out a solution to this? I have the exact same problem on one machine, and I can't figure out what's the difference. Above LESS_TERMCAP_so variable causes the whole file to turn into orange background... May 23, 2015 at 7:14
  • I'm afraid I am still struggling with this
    – sgp667
    May 23, 2015 at 17:42

4 Answers 4

22

Found an answer over on the superuser: https://superuser.com/questions/566082/less-doesnt-highlight-search

Looks like it has to do with your TERM setting. For example, less highlighting acts normally (white background highlight) when in a normal gnome-terminal window, but when I'm in tmux, italics happens. The difference for me is that TERM is being set to "screen" when in tmux, but "xterm-256color" when not. When I set "TERM=xterm-256color" in the tmux window, highlighting in less goes back to background highlighting.

5
  • I already managed to forget about this question but this is exciting news. As soon as I get my hand on the computer I will test this out!
    – sgp667
    Dec 29, 2015 at 15:56
  • 1
    this is an impractical solution, because other applications depend on TERM being set to screen* when within tmux, such as WeeChat. When not set to screen* in tmux, WeeChat has weird rendering issues. Aug 26, 2016 at 11:05
  • I've noted that I had the problem with tmux <2.0 and don't have the problem since tmux v2. Jan 13, 2017 at 11:23
  • Great finding. I now have set -g default-terminal "xterm-256color" in my ~/.tmux.conf on my CentOS 7 box (and others).
    – sshow
    Feb 21, 2019 at 14:43
  • 1
    Check also to see if screen-256color exists: try ls /usr/share/terminfo/s/.
    – aDroid
    Apr 2, 2019 at 16:35
15

The mention of LESS_TERMCAP_so was incomplete. That is less's special environment variable used to override the termcap so (standout) capability. To use this capability, you have to provide a se (standend) capability as well.

The terminfo(5) manual page gives a summary of these features for terminfo (smso/rmso) and termcap (so/se) names:

   enter_standout_mode           smso       so        begin standout mode
   exit_standout_mode            rmso       se        exit standout mode  

Its section on highlighting explains:

If your terminal has one or more kinds of display attributes, these can be represented in a number of different ways. You should choose one display form as standout mode, representing a good, high contrast, easy-on-the-eyes, format for highlighting error messages and other attention getters. (If you have a choice, reverse video plus half-bright is good, or reverse video alone.) The sequences to enter and exit standout mode are given as smso and rmso, respectively.

If you want to use color for standout, you have to provide a corresponding LESS_TERMCAP_se which resets color. This is relatively simple to do as long as you do not expect to use colors in the manual page for other reasons (such as using groff's SGR color feature).

Assuming the value suggested in a comment:

export LESS_TERMCAP_so=$'\E[30;43m'

then you could reset that for most terminals using

export LESS_TERMCAP_se=$'\E[39;49m'

By the way, the reason for the italics is that the terminal description for GNU screen uses the standard escape sequence for italics as its own standout/standend capabilities. Some of that is discussed in the terminal database entry.

4
  • This was the first solution that actually worked for me (in tmux on a Mac). I just add both of the above export line into my bash config file.
    – Marlun
    May 10, 2017 at 4:23
  • However if I write "export" to view all my environment variables everything after these lines becomes highlighted and the terminal keeps being highlighted so I have to close it down and start a new one.
    – Marlun
    May 16, 2017 at 8:03
  • In cases like that, I put the variables into a script which calls the program that needs the particular variables. The altered variables only apply within the script. May 16, 2017 at 8:55
  • export | less functions as a workaround too
    – Marlun
    May 17, 2017 at 19:31
4

Hit ESCu to turn off search highlighting in less after a search; a new search will turn it on again, so to permanently turn search highlighting off for a session hit -G.

Alternately put LESS='-G' in your environment, or run man like so:

LESS='-G' man less

Ironically this is all documented in the less manpage...

You can also put the following in the environment, e.g. in your .bash_profile:

export MANPAGER='less -G'
1
  • as I explained in my question I don't want to turn it of completly, I want to change how it appears on my screen. My question might have been misleading bacuse someone edited it for me.
    – sgp667
    Jan 21, 2015 at 15:57
2

For tmux I set TERM to tmux-256color. Amongst other nice things, this has the "usual" reverse highlighting in less. Works well when using something modern like fedora, or cygwin.

$ cat ~/.tmux.conf
⋮
set-option -g default-terminal tmux-256color

The ncurses packages on Centos however do not have the two tmux definitions (tmux and tmux-256color). I imported them quite easily from fedora. Slight wrinkle is that Centos' ncurses is too old to understand fedora's terminfo files directly.

On fedora:

$ infocmp tmux256-color >temp.txt

On Centos:

$ scp fedora-machine:temp.txt .
$ tic temp.txt

Then inside tmux:

$ TERM=tmux-256-color man tmux

Result!

FYI the tic command puts the compiled terminal description into your personal terminfo database, i.e., ~/.terminfo/t/tmux-256color (in this case). Feel free to move this to the global database if that's what you want:

$ sudo mv ~/.terminfo/t/tmux-256color /usr/share/terminfo/t/

You must log in to answer this question.

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