May 6th 2018

linux, shell

Search man pages with less: Unhighlight search results


The man command uses a pager, that is responsible for pagination, but also search and highlight.
If your pager is less, you can search man pages with /search string.
It will then highlight the search results, and they can be traversed with n/N.
The highlighting helps to find the search string, but it can then be disturbing or even unreadable, and here's how to remove it (Source):

You can use Alt+u to remove the highlight on last search results.
You can highlight them again with Alt+u, it's a toggle.

Switching off the highlight does not switch off the status column, showing marks on each line containing a match, if the column is enabled using options -J or --status-column or keys -J.

To hide the status column, use -+J.

To show the status column, use -J.

(Technically, Alt+u it's equivalent to ESCu on terminal level - that is why the Alt-key is not mentioned in the man page.)


Configure less highlight mode

less is configured via environment variables.
One of them starts the highlight mode.
Instead of insane coloring, you can set it to simple reverse video:
LESS_TERMCAP_so=$(printf "\e[7m")

On my system,. there's a man function in /etc/bash.bashrc which I have modified as follows:

man() {
env LESS_TERMCAP_mb=$(printf "\e[1;31m") \
LESS_TERMCAP_md=$(printf "\e[1;31m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[7m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[1;32m") \
man "$@"
}
#LESS_TERMCAP_so=$(printf "\e[1;44;33m")
# that used to be in the man function for less's annoyingly over-colorful status line.
# changed it to simple reverse video (tput rev)