Highlight the current search match in man pages

highlightingmansearch

When I open a man page, for example man chmod and I hit / for searching a string in the page I can see all the matching strings highlighted.

Pressing n or N will focus on the next/previous matching string.

Is there a way to highlight the current match (the one with the focus) differently from all the matching strings?

Best Answer

This is controlled via MANPAGER.

Manual pages defaults to the pager of less -is.

You can just highlight the current selection:

man -P "less -isg" <command>

Or set the MANPAGER variable:

export MANPAGER="less -isg"

You could also use vim where the cursor is highlighted so the current selection will appear differently.:

 man -P "sh -c \"col -b | vim -c 'set ft=man ts=8 nomod nolist nonu' \
    -c 'nnoremap i <nop>' \
    -c 'nnoremap <Space> <C-f>' \
    -c 'noremap q :quit<CR>' -\"" <command>

Taken from Zameer Manji:

  1. ft=man enables the coloring of the man page.
  2. ts=8 ensures the width of tab characters matches less.
  3. nomod removes the modification warning when trying to quit.
  4. nonu removes line numbers.
  5. nolist disables listchars so trailing whitespace and extra tabs are not highlighted.
  6. nnoremap i ensures that we do not accidentally enter insert mode when viewing the man page.

Plus my own option to use space to paginate and quit with q:

-c 'nnoremap <Space> <C-f>'
-c 'noremap q :quit<CR>'

Set it as your default pager by adding it to your default profile:

export MANPAGER="sh -c \"col -b | vim -c 'set ft=man ts=8 nomod nolist nonu' \
    -c 'nnoremap i <nop>' \
    -c 'nnoremap <Space> <C-f>' \
    -c 'noremap q :quit<CR>' -\""
Related Question