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 - 4after 37 years we have wrap-around search…… still can't believe it!viuser– viuser2021-08-25 22:55:56 +00:00Commented Aug 25, 2021 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.
- 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
/patternandnandNorgandn?viuser– viuser2016-01-17 11:50:53 +00:00Commented Jan 17, 2016 at 11:50 - 1That's all the manpage gives you.Thomas Dickey– Thomas Dickey2016-01-17 12:01:37 +00:00Commented Jan 17, 2016 at 12:01
- 2
ggnis easily typed, basically, return to top, next...lornix– lornix2016-01-17 21:07:45 +00:00Commented Jan 17, 2016 at 21:07 - 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– Thomas Dickey2016-01-17 21:14:10 +00:00Commented Jan 17, 2016 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.