Is there a shortcut for word boundary in `less`?

后端 未结 4 772
情书的邮戳
情书的邮戳 2021-01-11 12:22

This is the less I am using:

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

In Vim it is \\< a

相关标签:
4条回答
  • 2021-01-11 12:55

    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.

    0 讨论(0)
  • 2021-01-11 13:12

    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.

    0 讨论(0)
  • 2021-01-11 13:14

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

    0 讨论(0)
  • 2021-01-11 13:17

    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 ?! )

    0 讨论(0)
提交回复
热议问题