does vi has a funtion like "grep -v"?

1,635 views
Skip to first unread message

skyworld

unread,
Jan 25, 2013, 4:21:37 AM1/25/13
to vim_use
Hi,

does vi has a fuction like "grep -v"? I always found I need to seach
within the file with those lines which exclue specific strings.
Thanks.


regards

Aaron Webster

unread,
Jan 25, 2013, 5:24:15 AM1/25/13
to vim...@googlegroups.com
Searching in vim is done with regular expressions, so I believe this is possible.  For example, to search for lines with do not include "string"

    /^\(\(.*string.*\)\@!.\)*$

See here:



--
Aaron Webster

Shlomi Fish

unread,
Jan 25, 2013, 5:31:14 AM1/25/13
to vim...@googlegroups.com, chenyo...@gmail.com
Hi skyworld,
First of all, you can use :v/re/p (Replace p with whatever). Otherwise look at
negated regular expressions in http://shlomif-tech.livejournal.com/40557.html .

Regards,

Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Beginners Site for the Vim text editor - http://vim.begin-site.org/

The bad thing about hardware is that it sometimes works and it sometimes
doesn’t. The good thing about software is that it’s consistent: it always
does not work, and it always does not work in exactly the same way.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

Charles Campbell

unread,
Jan 25, 2013, 10:00:20 AM1/25/13
to vim...@googlegroups.com
Here are two methods:

:v/pattern/p

/^\%(\%(pattern\)\@!.\)*$

You may find LogiPat of interest; it allows you to construct patterns
using Boolean logic (not, and, or):
http://www.drchip.org/astronaut/vim/index.html#LOGIPAT

Regards,
C Campbell

Jiantao Wu

unread,
Jan 25, 2013, 4:26:52 AM1/25/13
to vim...@googlegroups.com
No, it does not. You can use '?' or '/' to search.


2013/1/25 skyworld <chenyo...@gmail.com>

--
--
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




skyworld

unread,
Jan 26, 2013, 5:54:31 AM1/26/13
to vim_use
Thanks for all of your kind reply. I will check these ways.

On Jan 25, 5:26 pm, Jiantao Wu <4576211...@gmail.com> wrote:
> No, it does not. You can use '?' or '/' to search.
>
> 2013/1/25 skyworld <chenyong20...@gmail.com>
>
>
>
> > Hi,
>
> > does vi has a fuction like "grep -v"? I always found I need to seach
> > within the file with those lines which exclue specific strings.
> > Thanks.
>
> > regards
>
> > --
> > --
> > 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, visithttp://www.vim.org/maillist.php- Hide quoted text -
>
> - Show quoted text -

Ping

unread,
Jan 26, 2013, 11:34:32 AM1/26/13
to vim...@googlegroups.com, vim...@googlegroups.com
Why not just use external grep in vim if that's what you want?
:[range]!grep -v



On Jan 25, 2013, at 7:20 AM, Catalin Ciurea <catalin...@gmail.com> wrote:
> See the :grep command (:he :grep) in Vim.
> This command uses the 'grepprg' option to check what program to call. By default is 'grep -n'(See :he 'grepprg')
>
> You can always do something like:
> set grepprg+=-v
> :grep foo *
>
> Check :vimgrep command also. It uses Vim regular expressions and an internal search engine. (Slower but if you are familiar with Vim re, it's a good option)

Ben Fritz

unread,
Jan 26, 2013, 11:38:59 AM1/26/13
to vim...@googlegroups.com
On Saturday, January 26, 2013 10:34:32 AM UTC-6, ping wrote:
> Why not just use external grep in vim if that's what you want?
>
> :[range]!grep -v
>
>

This will replace all the lines in [range] with the output of !grep. I don't think the OP wanted to DELETE the lines, just find them.

The better way to invoke external grep for searching from Vim is with Vim's :grep command, which invokes 'grepprg' and then populates the quickfix list. 'grepprg' by default just uses grep.

ping

unread,
Jan 26, 2013, 12:56:44 PM1/26/13
to vim...@googlegroups.com, Ben Fritz
that's a good point, but from what my experiences (test, errors), in an
attempt to find the solution for this desire , I really think the
external grep way is the most efficient.
reason:

1) most of time , I just want to quicky find out the wanted or
not-wanted (-v) lines per keyword
2) :[range]!grep [-v] will re-use whatever your skills of standard unix
tool : grep
3) if you don't want to "replace" but to "display", you can use
:[range]w !grep [-v]
4) but the :[range]w ! way doesn't know "scale", as the new found texts
won't but put in a buffer by default, (unless redirected), so you can't
re-fine your research based on the result.
5) even if you don't want to "replace" the original texts, you can
easily go back via "u"
6) and if you like what you get, but want to even re-define a new
search based on it, just repeat the whole process recursively.
7) and once done, always just recover back to original text via some
'u'ndos.

I also like the vimgrep coming with the fancy quickfix list, but that
looks a better usage scenario when I need to search from a bunch of
files (under a common dir ).

just my 2 cents.

regards
ping
Reply all
Reply to author
Forward
0 new messages