Make Less highlight search patterns instead of italicizing them
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
andrmso
, 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/
Related videos on Youtube
sgp667
Updated on September 18, 2022Comments
-
sgp667 over 1 year
To my understanding man uses
less
as a pager, and when searching for keywords usingless
it "highlights" keywords with italics. I find that really inconvenient, so I'd like to change this to something like vim'sset 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 withless
.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
). Theless
man page does not mention this. (I get strange results with this solution)-
iyrin over 9 yearsIt 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 over 9 yearsSometime I use
man man | vim -
to take advantage of my vim configuration, keys and functions -
user1338062 almost 9 yearsDid 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 almost 9 yearsI'm afraid I am still struggling with this
-
-
sgp667 over 9 yearsas 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 over 8 yearsI 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 over 7 yearsthis 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 over 7 yearsI've noted that I had the problem with tmux <2.0 and don't have the problem since tmux v2.
-
Marlun almost 7 yearsThis 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 almost 7 yearsHowever 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 almost 7 yearsIn 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 almost 7 years
export | less
functions as a workaround too -
sshow about 5 yearsGreat finding. I now have
set -g default-terminal "xterm-256color"
in my ~/.tmux.conf on my CentOS 7 box (and others). -
aDroid about 5 yearsCheck also to see if
screen-256color
exists: tryls /usr/share/terminfo/s/
.