16

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

viuser
  • 2,614

3 Answers3

15

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
Arminius
  • 726
6

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

Thomas Dickey
  • 76,765
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.

fzbd
  • 148