1

I would like to use Regex in helpgrep. For example, searching for lines or sections that contain two words. Something like :helpg 'word1' & 'word4' to find this line as my result: word1 word2 word3 word4. Or another example for lines or sections containing word1 or word4.

1 Answer 1

1

You can use regexps in :helpgrep, you only need to provide a valid regular expression. Your 'word1' & 'word4' does not do what you describe. To match both "word1" and "word4" on the same line or either of them, try the respective

:helpgrep word1.*word4
:helpgrep word1\|word4

The regexps explained: The first line will match "word1" followed by any number of any character, followed by "word2".
The second line will match "word1" OR "word2", the \| being the OR operator in Vim's dialect of regular expressions (other dialects use a plain | so you may have seen this).

As I said, this works on lines and things get complicated quickly when trying to get matches within a section or paragraph. It's probably easier to search from there or actually reading the whole paragraph. I like doing so because I tend to learn new stuff this way.

A full description of regexps is beyond the scope of an answer on this site. To familiarize yourself with (Vim's dialect of) regular expressions, read :help regexp.

5
  • what is the meaning of `\` after word1? Commented Oct 4, 2023 at 13:06
  • It's not so much after word1 as it is in front of |. The \| is regexp's OR operator. Coincidentally, it's the first item in :help regexp. Please read it.
    – Friedrich
    Commented Oct 4, 2023 at 13:09
  • Thanks a lot, did you read the whole documents? Is it necessary in your opinion? I'm really disappointed to learn vim. It's huge.... Commented Oct 4, 2023 at 13:45
  • I agree there are more accessible editors out there and not necessarily worse. Most of the time I read the section and sometimes the adjoining paragraphs. I don't think I have read all of Vim's documentation yet.
    – Friedrich
    Commented Oct 4, 2023 at 13:50
  • You can also use \& to denote a branch like in :helpgrep .*word1\&.*word4 or :helpgrep .*concats\&.*one or more (Hint :-) ). Commented Oct 4, 2023 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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