ag: search exact string, not a pattern
Asked Answered
G

1

13

My Problem

I'm searching for Application(service) in my files using ag. Since ag treats strings as regular expression patterns, I have to escape the parens and use:

ag 'Application\(service\)'

This is cumbersome and easy to miss, especially when there are multiple regex symbols in my search.

What have I tried

  • Search the manual
  • Escape the regex chars (works, but error-prone)

My Question

How can I instruct ag to treat the search term as a literal string, instead of a regex pattern?

Gratin answered 26/5, 2019 at 15:2 Comment(4)
Have you tried --literal/-Q?Fabien
Try using quoted literal and place your string between \Q and \E like this \QApplication(service)\E and as I know ag tool uses PCRE based engine so it should work most likely, though not sure as never used that tool.Ordure
@Fabien Great, don't know how I missed it. Answer it and I'd be happy to upvote.Gratin
This is less of a programming question and more of a "how do I use ag?" question.Wolfy
F
16

The option you're looking for is

-Q --literal: Do not parse PATTERN as a regular expression. Try to match it literally.

@ https://github.com/ggreer/the_silver_searcher/blob/master/doc/ag.1.md:

Fabien answered 26/5, 2019 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.