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

33 views
Skip to first unread message

Bao Niu

unread,
Jan 19, 2015, 11:24:58 PM1/19/15
to vim...@googlegroups.com
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.

Tim Chase

unread,
Jan 19, 2015, 11:55:25 PM1/19/15
to vim...@googlegroups.com
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


John Beckett

unread,
Jan 20, 2015, 5:48:37 AM1/20/15
to vim...@googlegroups.com
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


Bao Niu

unread,
Jan 20, 2015, 4:33:54 PM1/20/15
to vim...@googlegroups.com
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?



--
--
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 vim_use+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Erik Falor

unread,
Jan 20, 2015, 5:01:35 PM1/20/15
to vim...@googlegroups.com, Bao Niu
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

Bao Niu

unread,
Jan 20, 2015, 5:04:47 PM1/20/15
to vim...@googlegroups.com, Bao Niu
Thanks for this!

Tim Chase

unread,
Jan 20, 2015, 5:49:01 PM1/20/15
to vim...@googlegroups.com, niub...@gmail.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".

-tim




Nikolay Pavlov

unread,
Jan 20, 2015, 11:08:07 PM1/20/15
to vim...@googlegroups.com, niub...@gmail.com
`@` 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+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages