Weird behavior of less regular expression (regex) search in man

I was recently learning about Linux regular expressions, and I found that on my computer the less program’s regex searching behavior is quite weird when being called from man. For example, if I run man ls and use /-a for searching, I can find the “-a” and part of the “–all” option on the first page of the manual. However, by just adding a space in the search pattern (i.e., using / -a for searching), less shows to me “Pattern not found”, which should not happen. However, I then created a text file in which a line begins with spaces and then follows “-abc”, and use less to open the file. This time, / -a successfully matched the begining of “-abc”. I am running Fedora 35 with less 590 and man 2.9.4, which should be the latest version. It would be great if anyone can reproduce my problem.

I have checked my environment variables and both $PAGER and $MANOPT are not set, so man should be in default settings.

By the way, when I run less --version, I see that less in Fedora uses POSIX regular expressions, while the less program on my Debian WSL 2 virtual machine uses GNU regular expressions. Are these two different, and if so what are the differences? Also, man ls on my Debian virtual machine does not have this weird behavior.

2 Likes
LESS="-p\ -a" man ls
1 Like

Searching in less does not wrap at the end of the file. Can you go to beginning of the file by pressing g and try / -a again?

1 Like

Searching for -a works as expected here (I need to use n or N to go forward or backwards in the man page, depending on how far down in the man page I am since the search does not wrap: as noted by ersen above), so this is most likely an issue somewhere with your environment. Can you please create a new user and see what happens? That’ll tell us if it’s something in your user environment or system wide.

1 Like

Thanks for all the suggestions! After checking my environment I found that my LESS variable is set to -r, which leads to the problem unexpectedly. Changing it to -R works. I previously set LESS=-r to enable colored output but after looking throught the manual -R is the better way and -r will lead to many unexpected side effects like this.

2 Likes