tsz
tsz

Reputation: 307

How do I need to escape search string in vim?

I need to search and replace this:

ExecIf($["${debug}" = "1"]?NoOp

with this:

GoSub(chanlog,s,1(1,[${CHANNEL}] 

I can't seem to do it in vim, and I'm not sure what needs to be escaped, as nothing I've tried works.

Upvotes: 2

Views: 1029

Answers (3)

pb2q
pb2q

Reputation: 59617

If you're having problems with which characters to escape in a vim substitution (:s//), remember the nomagic concept, and in particular the nomagic version of a substitute: :snomagic// or :sno//. nomagic means: interpret each character literally.

So this should work without worrying about escaping characters in the substitution:

:sno/ExecIf($["${debug}" = "1"]?NoOp/GoSub(chanlog,s,1(1, [${CHANNEL}]/

Get to know magic vs. nomagic, :sno//, and \v, \V:

:help magic

The nomagic version of a search for your string uses \V:

/\VExecIf($["${debug}" = "1"]?NoOp

Upvotes: 2

epsilonhalbe
epsilonhalbe

Reputation: 15967

you have to escape the [] and the spaces:

:s/ExecIf($\["${debug}"\ =\ "1"\]?NoOp/GoSub(chanlog,s,1(1,\[${CHANNEL}\]/

just a bit trial and error

Upvotes: 1

Alan Curry
Alan Curry

Reputation: 14711

If you want to change a long string with lots of punctuation characters, and it's an exact match (you don't want any of them to be treated as regex syntax) you can use the nomagic option, to have the search pattern interpreted as a literal string.

:set nomagic
:%s/ExecIf($["${debug}" = "1"]?NoOp/GoSub(chanlog,s,1(1,[${CHANNEL}]/
:set magic

You still have to watch out for the delimiters (the slashes of the s/// command) but you can use any character for that, it doesn't have to be a slash, so when you have something like this and there are slashes in the search or replace string, just pick something else, like s@foo@bar@ or s:bar:baz:.

Upvotes: 4

Related Questions