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

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Bao Niu
in Vim is enclosing your search pattern with "/" recommended? The documentation says it is optional. If it's optional then why not just forget it? Can someone explain this using newbie friendly language, preferably with examples. Thanks.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Tim Chase
On 2015-01-19 20:24, Bao Niu wrote:
> in Vim is enclosing your search pattern with "/" recommended? The
> documentation says it is optional. If it's optional then why not
> just forget it? Can someone explain this using newbie friendly
> language, preferably with examples. Thanks.

While this is only from my personal experience, I tend to wrap my
pattern in /.../ if it's anything beyond a simple single word.  So I
might do

  :vimgrep foobar *.c *.h

whereas I'd use

  :vimgrep /foo.*bar/ *.c  " uses a regexp
  :vimgrep /foo bar/ *.c   " has a space
  :vimgrep /@term@/ *.c    " has the same char at the beginning/end

I'm pretty sure (shooting from the hip here, so test to make sure)
that as long as it doesn't have a space in it and it doesn't have
the same non-word character at the start and end of the pattern, it
should be fine to omit them.  But rather than think, I just use the
heuristic of "if it's a single word, don't bother; otherwise, wrap
the pattern"

-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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

JohnBeckett
In reply to this post by Bao Niu
Bao Niu wrote:
> in Vim is enclosing your search pattern with "/" recommended?
> The documentation says it is optional. If it's optional then
> why not just forget it?

I would be too nervous to follow Tim's advice, but it seems to
work. Are you aware of the general rule that applies to commands
like :s (substitute)?

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

These tips are worth careful study:
http://vim.wikia.com/wiki/Searching
http://vim.wikia.com/wiki/Search_and_replace

John


--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Bao Niu
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?

On Tue, Jan 20, 2015 at 2:48 AM, John Beckett <[hidden email]> wrote:
Bao Niu wrote:
> in Vim is enclosing your search pattern with "/" recommended?
> The documentation says it is optional. If it's optional then
> why not just forget it?

I would be too nervous to follow Tim's advice, but it seems to
work. Are you aware of the general rule that applies to commands
like :s (substitute)?

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

These tips are worth careful study:
http://vim.wikia.com/wiki/Searching
http://vim.wikia.com/wiki/Search_and_replace

John


--
--
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 a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/RgAUMOyjGKM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Erik Falor
On Tue, Jan 20, 2015 at 01:33:46PM -0800, Bao Niu wrote:
> 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?

Don't let it intimidate you!  It's quite simple, really.

In fact, using a character other than the '/' to delimit the search
and replace parameters to the substitute command is not something
unique to Vim.  Perl and sed follow this convention as well.

For instance:

http://www.grymoire.com/Unix/Sed.html#uh-2

--
Erik Falor
Registered Linux User #445632                  http://linuxcounter.net

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

attachment0 (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

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

Bao Niu
Thanks for this!

On Tue, Jan 20, 2015 at 2:01 PM, Erik Falor <[hidden email]> wrote:
On Tue, Jan 20, 2015 at 01:33:46PM -0800, Bao Niu wrote:
> 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?

Don't let it intimidate you!  It's quite simple, really.

In fact, using a character other than the '/' to delimit the search
and replace parameters to the substitute command is not something
unique to Vim.  Perl and sed follow this convention as well.

For instance:

http://www.grymoire.com/Unix/Sed.html#uh-2

--
Erik Falor
Registered Linux User #445632                  http://linuxcounter.net

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Tim Chase
In reply to this post by Bao Niu
[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".

-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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Nikolay Aleksandrovich Pavlov


2015-01-21 1:50 GMT+03:00 Tim Chase <[hidden email]>:
[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 [hidden email].
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.