10

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

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? – viuser Jan 17 '16 at 11:50
  • That's all the manpage gives you. – Thomas Dickey Jan 17 '16 at 12:01
  • 2
    ggn is easily typed, basically, return to top, next... – lornix Jan 17 '16 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 Jan 17 '16 at 21:14
4

Yes, it is possible since release v568.

There is a patch (full disclosure: I wrote it) for this exact feature included since v568. But note that your OS/package manager likely still provides an earlier stable version, not the latest release. So, you may have to build less from source to be able to use the feature right now.

The patch adds the ^W search modifier which allows wrap-around search within the current file. From the manual:

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

If you want this to be default behavior, you could add something like the following to you config file (probably located at ~/.lesskey, see man lesskey for details):

#command
/ forw-search ^W
? back-search ^W
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.