Is it possible to enable "wrap around" in less for the search? This means: If the last occurrence of a pattern has been found, pressing n
will start the search again at the beginning of the file (so you do not have to press g
and then n
).
3 Answers
Yes, it's possible since release v568.
Since this patch (full disclosure: I wrote it), the search modifier ^W
enables wrap-around search within the current file. Use it by pressing CTRL-W while on the search prompt. From the manpage:
/pattern
(...)
^W WRAP around the current file. That is, if the search
reaches the end of the current file without finding a
match, the search continues from the first line of the
current file up to the line where it started.
One way to make this the default behavior is via the command section in your config file (possibly located at ~/.lesskey
, see man lesskey
for details):
#command
/ forw-search ^W
? back-search ^W

- 726
-
2after 37 years we have wrap-around search…… still can't believe it! – viuser Aug 25 '21 at 22:55
Probably not: the manual page for less
does not mention wrapping, and the description of the search feature does not hint it is possible.

- 76,765
-
So if I am in the middle of a file, there is no way to be sure that pattern doesn't occur anywhere other than typing
/pattern
andn
*and*N
org
*and*n
? – viuser Jan 17 '16 at 11:50 -
1
-
2
-
It might be, but does nothing more than go to the top and search from the top, which was what OP wished to avoid. – Thomas Dickey Jan 17 '16 at 21:14
As a workaround, you can redefine the forward/backward search key mappings to jump to the begin/end of the file and then search.
Add these to the $HOME/.lesskey
file:
#command
\eB back-search
\eF forw-search
/ noaction gg\eF
? noaction G\eB
Compile it with lesskey
.
See man lesskey
for default key mappings and actions.
Tested in version 530.

- 148