Less search: show match with context before match (within less)

lessman

I would like to modify the search behavior when inside less: By default next match is shown at the very top line. Instead show me next match of word at 5th line of screen/ show 4 lines before match. Like so:

yadda yadda
yadda
more yadda
more of the yadda
Some sentence with word (<-- match!)
yadda goes on

This is somewhat similar to grep -B4: print NUM lines of leading context before matching lines.

Less is by default my manpages pager.

Best Answer

Type -j5 Enter to set the target line for searches to 5 (the default setting is 1 which is the top line).

To make this the default, add -j5 to the default less options. One way to do that is to include -j5 in the LESS environment variable set e.g. in your .profile or .environment. Another way is to put

#env
LESS = -j5

in ~/.lesskey and run the command lesskey.

Related Question