16

I am sick of having to escape things when I want to search for part of an html tag.

How can I ack search for exactly what I type without having to escape stuff?

e.g.

ack-grep 'console.log(foo'

I get:

Unmatched ( in regex; marked by <-- HERE in m/console.log( <-- HERE par/
Andy Lester
  • 740
  • 5
  • 16
tester
  • 565
  • 8
  • 18
  • Use a different shell? Put it in quotes? Place your search string in a file, I suspect ack-grep can get the string from a file. Perhaps you can even could even get the search via stdin. – Zoredache Mar 01 '12 at 22:08
  • @Zoredache I added a better example.. I don't want to have to escape `(`.. and what do you mean by better shell? – tester Mar 01 '12 at 22:10
  • I mean all the chracters you are complaining about are handled by your shell (bash/zsh/whatever). The problem has nothing to do with the tool. The problem is that the shell is not sending what you type. – Zoredache Mar 01 '12 at 22:12
  • Ah I think i found it.. -Q uses it literally.. – tester Mar 01 '12 at 22:14

1 Answers1

21

You have to escape the regex.

ack 'console\.log\(foo'

(You should escape the . so that you don't match "consoleflog", because . matches any single character)

And if you would rather not do that, do this to quote every metacharacter automatically.

ack -Q 'console.log(foo'
Andy Lester
  • 740
  • 5
  • 16
  • 2
    \Q does not work on my server. `ack '\Qconsole.log(foo' ack: Invalid regex '\Qconsole.log(foo': Unmatched ( in regex; marked by <-- HERE in m/\Qconsole.log( <-- HERE foo/` – Sébastien Dec 01 '14 at 12:37
  • I was mistaken about the `\Q` example and have removed it from the answer. Thanks, @Sébastien. – Andy Lester Jul 14 '17 at 15:07