Wednesday, October 23, 2013

Re: Literal Regexes

On 2013-10-23 10:40, Paul wrote:
> Sometimes I will want to do a simple search and replace on a string
> that contains characters that I have to escape, eg. for
> '$foo->{bar}[0]', I would have to use '\$foo->{bar}\[0\]'. I try
> not to use the various magic types, because it's difficult to
> remember them, and it's recommended to not change the default magic
> setting. Is there a way I can do 's/$foo->{bar}[0]/foobar/' and
> have it not treat any characters as special, ie. treat the search
> string as all literal?

Mostly. If you include

\V

in your pattern (I usually just put it at the beginning, but I think
it can go anywhere), it will activate "very no magic" processing.
This means that only the backslash needs to be escaped, treating
everything else literally.

:help /\V

Likewise, if used in a ":s" command, you'd have to escape whatever
character you use as a delimiter or choose a delimiter that you don't
have in your pattern. E.g.:

:%s@/path/to/foo@/home/path/to/bar@g

as detailed at

:help E146

-tim



--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment