Make Less highlight search patterns instead of italicizing them

16,240

Solution 1

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.

Solution 2

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.

Solution 3

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'

Solution 4

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/
Share:
16,240

Related videos on Youtube

sgp667
Author by

sgp667

Updated on September 18, 2022

Comments

  • sgp667
    sgp667 over 1 year

    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)

    • iyrin
      iyrin over 9 years
      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/…
    • JJoao
      JJoao over 9 years
      Sometime I use man man | vim - to take advantage of my vim configuration, keys and functions
    • user1338062
      user1338062 almost 9 years
      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...
    • sgp667
      sgp667 almost 9 years
      I'm afraid I am still struggling with this
  • sgp667
    sgp667 over 9 years
    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
    sgp667 over 8 years
    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!
  • Vegard Larsen
    Vegard Larsen over 7 years
    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.
  • artfulrobot
    artfulrobot over 7 years
    I've noted that I had the problem with tmux <2.0 and don't have the problem since tmux v2.
  • Marlun
    Marlun almost 7 years
    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
    Marlun almost 7 years
    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.
  • Marius
    Marius almost 7 years
    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.
  • Marlun
    Marlun almost 7 years
    export | less functions as a workaround too
  • sshow
    sshow about 5 years
    Great finding. I now have set -g default-terminal "xterm-256color" in my ~/.tmux.conf on my CentOS 7 box (and others).
  • aDroid
    aDroid about 5 years
    Check also to see if screen-256color exists: try ls /usr/share/terminfo/s/.