regexvimvimgrep

ag Silver Searcher: rules for lookahead/lookbehind patterns?


I'm using ag in Vim with good results for string/file search.

However, there seems to be not much documentation how patterns are constructed for ag.

I'm trying to use ag instead of vimgrep in an example from the Practical Vim book.

I want to find every occurrence of "Pragmatic Vim" in all files in a directory recursively, and substitute this string with "Practical Vim". There is also "Pragmatic Bookshelf" in some files, and that string must remain.

the book suggests this approach:

/Pragmatic\ze Vim
:vimgrep /<C-r>// **/*.txt

And after that step, populate quickfix list with :Qargs plugin command, and, finally :argdo %s//Practical/g

Now, how do I specify /Pragmatic\zepattern using ag?

Is ag at all designed for what I'm trying to do here?


Solution

  • The Silver Searcher tool uses PCRE (Perl-Compatible Regular Expression) syntax. So instead of Vim's \ze, you need to use the Perl syntax for positive lookahead: (?=pattern). (The corresponding lookbehind for \zs would be (?<=pattern).)

    I'm showing your example on the command-line, but it should be identical from within Vim:

    $ ag 'Pragmatic(?= Vim)'