I need a macro/function that searches the current document, starting at the top, for a specific pattern. Stopping at the first line that isn’t commented out.
For example in this vim script:
"This is the first line
"This is the third line
if exists("did_something")
It would stop at if line, return true if it found the pattern and false otherwise.
All help appreciated!
Let’s say your pattern is the letter ‘e’.
To search for a line that is not commented out, but contains an e:
This uses a negative lookahead (
\@!) to confirm that the start of the line (^) isn’t followed by a the beginning of a comment (whitespace and then a double quote\s*") but is followed by an ‘e’ (.*e). The\%(and\)makes the enclosed pattern an atom that other operators (like negative lookahead) can operate on as a unit.To run a command on matching lines, use
:gTo see if the current line matches, use
match()The regex is pretty much always the same, the question is what do you want to do with matching lines?