Make “less” jump point to center of search result rather than top

less

If one searches in less by typing /search term and jumping to search results with n and N the view of the text jumps so that the occurrence of the search term is at top of the terminal. Displaying the half of text before and after the occurrence is much more useful and I'd like to know how to achieve it.

Best Answer

Use the -j option. For instance, if you use less -j3, the match will be displayed on the 3rd line. To do what you want, i.e. display the match at the center of the terminal:

less -j$((LINES/2))

or since less 397, one can specify a percentage within the screen:

less -j.5

Note: To do that permanently, i.e. for all invocations, you can put this -jn option in the LESS environment variable.

This also affects commands to go to line N, e.g. line N will appear on the 3rd line of the terminal with -j3. A minor drawback is that one can get spurious empty lines before the beginning of the file, e.g. with the key <. EDIT: I've written a patch to avoid this problem; see this message of my bug report for additional details. This patch allows one to avoid these empty lines only when going to the special line 0, which is the default for keys g and <. Thus it should be sufficient in practice.

Related Question