vimvimgrep

My vimgrep search is not behaving as I would expect


I am performing the following vimgrep search (in vim(!))....

:vimgrep /^\s*bool\s\+\i\+\s*=\s*\(false\)\|\(true\);\s*$/      *[files....]*

in order to find bool variable initialisations in my code. It successfully returns all of the bool initialisations, e.g.

bool result1 = false;

bool result2=true;

but it also returns other lines where bool are assigned (not initialised), e.g.

result = true;

(i.e. it returns lines even when bool is not found at the start of the line).

I'd be grateful if anybody could tell me why it matches code where there is no "bool" type specifier at the start of the line.

Many thanks,

Steve.


Solution

  • :vimgrep /^\s*bool\s+\i+\s*=\s*(false)\|(true);\s*$/ [files....]
                                     ^     ^^^^   ^
    

    You have some problems, both are marked:

    1. Vim uses \(...\) to group atoms, not (...). Looks like that was the SO parser problem (\( not enclosed with backtics produces ().
    2. You should have \| inside parenthesis: \(false\|true\), or it will take it as «find either a lines where boolean variable is initialized as false (^\s*bool\s+\i+\s*=\s*\(false\) part) or a line which contains true followed by a semicolon at the end of line (\(true\);\s*$ part)».