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
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
.
-
-
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. Oct 4 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.... Oct 4 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. Oct 4 at 13:50
-
You can also use
\&
to denote a branch like in:helpgrep .*word1\&.*word4
or:helpgrep .*concats\&.*one or more
(Hint :-) ). Oct 4 at 13:52