20

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).

0

3 Answers 3

22

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 
1
  • 4
    after 37 years we have wrap-around search…… still can't believe it! Commented Aug 25, 2021 at 22:55
5

Probably not: the manual page for less does not mention wrapping, and the description of the search feature does not hint it is possible.

4
  • 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 and n and N or g and n? Commented Jan 17, 2016 at 11:50
  • 1
    That's all the manpage gives you. Commented Jan 17, 2016 at 12:01
  • 2
    ggn is easily typed, basically, return to top, next... Commented 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. Commented Jan 17, 2016 at 21:14
2

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.