codedmart
codedmart

Reputation: 107

vim :gsearch (greplace plugin) escaping characters

I am using the greplace plugin for vim and am not sure how to escape brackets in a search.

I want to search for cookies[:parent] and have tried:

:Gsearch cookies[:parent] # returns nothing
:Gsearch cookies\[:parent\] # returns nothing

How should I be doing this?

Thanks

Upvotes: 1

Views: 480

Answers (3)

Trevor Powell
Trevor Powell

Reputation: 1174

:Gsearch cookies\\\[:parent] works for me.

Remember that :Gsearch requires a file mask in addition to the pattern, so in reality, you'd want to type something like :Gsearch \\\[:parent] *.php or whatever, to specify which files you want to have searched.

Upvotes: 1

ZyX
ZyX

Reputation: 53644

Try

Gsearch cookies\\\[:parent\\\]

or

Gsearch 'cookies\[:parent\]'

. If I understood correctly, shell invoked by :grep! invoked by :Gsearch gets string grep -n cookies\[:parent\] /dev/null (assuming grepprg option has default value) and thus your escapes are interpreted by shell that thinks they are for escaping [ in order to prevent glob expansion. But after globbing done by shell grep takes argument as a pattern, so you need to escape it for grep also and it is why I have three backslashes here: two are to make grep get a backslash and third to prevent glob expansion.

Upvotes: 2

Tom Whittock
Tom Whittock

Reputation: 4230

:Gsearch cookies\[:parent]

[ is the start of a character class, so needs to be escaped. The ] isn't particularly special so doesn't need to be escaped.

Upvotes: 0

Related Questions