22

This is the less I am using:

less 458 (POSIX regular expressions)
Copyright (C) 1984-2012 Mark Nudelman

In Vim it is \< and \>, in most other regex it is \b.

Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
Steven Lu
  • 41,389
  • 58
  • 210
  • 364

5 Answers5

25

The character classes [[:<:]] and [[:>:]] match beginning and end of word, respectively, in system less on OS X 10.11.5. I haven't found a way to make the documented short forms \<, \>, or \b work.

(Thanks to denis for the suggestion to check man 7 re_format.)

Community
  • 1
  • 1
Jack Foy
  • 493
  • 5
  • 8
  • Ridiculously convoluted solution. Yet again Mac OS behavior is different from everywhere else. – Spidey Oct 19 '21 at 12:57
9

Your version of less was built with posix regular expressions, as if:

wget http://ftp.gnu.org/gnu/less/less-451.tar.gz
tar zxf less-451.tar.gz
cd less-451
./configure --with-regex=posix
make

However, apparently the cause of whether \< works or not does NOT depend on this:

  • In Debian/Linux, \< will work fine even if you build with the above commands, with posix regex
  • In Mac OS X, I tried all possible values of --with-regex except pcre, and \< doesn't work with any of them. If I build with pcre, then \b works, instead of \<.

To conclude, I don't know how to make it work with \<. But you can build yourself with pcre and then it should work with \b. If you are not a sysadmin, you probably want to use a --prefix to install under your home directory, for example --prefix=$HOME/opt. After the make step, confirm it works with ./less /path/to/some/file. If looks good, then finish with make install.

janos
  • 120,954
  • 29
  • 226
  • 236
1

First see if man 7 re_format on your computer has an "Enhanced features" section which lists \< etc. If it does, change one line in less-451/pattern.h:

#define REGCOMP_FLAG  REG_ENHANCED  // not REG_EXTENDED

Then ./configure --with-regex=posix; make less will understand \< .

This works on Macosx 10.8; on other systems, try following /usr/include/regex.h .
(Gnu.org has a round dozen Regular-expression-syntaxes ?! )

denis
  • 21,378
  • 10
  • 65
  • 88
0

less generally uses vi syntax, i.e. \< and \> unless it has been compiled with the --with-regex=none configure option or if the regular expression library found at compilation time doesn't provide word boundary search. Your system might also provide a different syntax.

jlliagre
  • 29,783
  • 6
  • 61
  • 72
0

On my macOS v12.6.3 system, the following enables vim-like, \b word-boundary-searching:

brew install less
echo PAGER='less'
hash -r # in bash, to discover new, homebrew-based less(1) path

Also, fyi:

$ less --version | head -2
less 608 (PCRE2 regular expressions)
Copyright (C) 1984-2022  Mark Nudelman
$

More, related details about my test system: https://gist.githubusercontent.com/johnnyutahh/0ca86ec252f832cfc512eaf56acecc3c/raw

Johnny Utahh
  • 2,389
  • 3
  • 25
  • 41