Tuesday, January 20, 2015

Re: Should vimgrep always use "/" to enclose your search patterns?



2015-01-21 1:50 GMT+03:00 Tim Chase <vim@tim.thechases.com>:
[reordering as inline replies are preferred over top-posting on this
mailing list]

On 2015-01-20 13:33, Bao Niu wrote:
>> An example to replace "hello" with "goodbye":
>>
>>   :%s/hello/goodbye/g
>>
>> Suppose you need to replace something with slashes in it.
>> The following attempt to replace "a/b" will NOT WORK:
>>
>>   :%s/a/b/goodbye/g
>>
>> However, the following will work:
>>
>>   :%s.a/b.goodbye.g
>
> Thank you John, but your advice makes me nervous. Does this dot
> ('.') rule also apply to vimgrep? Or it's only for substitution?
> This must be something that only pros use?

It's not something only for pros, and it works fine with vimgrep as
well as with the ":s" and ":g" commands:

  :g@foo@p   " print every line containing "foo", :g/foo/p
  :vimgrep #\<for\># *.py
  :%s!/foo/bar!/this/that!g

However, using a regexp meta-character such as "." does give me pause
I tend to fall back to one of "!", "@", or "#" depending on whether
any of them will appear in my regexp because none of them have
inherent meaning in a regexp, unlike the "." which means "any one
character".

`@` does have special meaning. By default you need to escape it to have this meaning, but with `\v` you don't. It is used e.g. for zero-width look-aheads/behinds: `%s/a\@<=b/c/g` will replace all b's preceded by `a` with `c`. If you use it as a separator you lose either this or verbatim meaning of `@` (depending on regex mode: with `\v` you lose look-aheads/behinds, with other modes you lose verbatim meaning so you can still have both by switching modes).
 

-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/d/optout.

--
--
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/d/optout.

No comments:

Post a Comment