Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

with grep I can do a grep -v "my search" to get all the lines with out "my search"

with sed I can sed '/baz/!s/foo/bar/g' to find replace text on lines with out baz

Is there a way to do the same thing vim. And is it possible but with out the "s///" syntax. Using just the "/" search syntax.

share|improve this question

2 Answers 2

up vote 16 down vote accepted
:g/pattern/

matches all the lines were pattern is found.

:v/pattern/

does the opposite. See :h global for more details.

You can use it like this:

:v/pattern/norm Ipattern not found - <CR>

to prepend "pattern not found - " to every line that doesn't have "pattern" or

:v/pattern/s/nrettap/pattern

to replace "nrettap" with "pattern" on every line that doesn't have "pattern".

Contrived examples, yes.

share|improve this answer
    
I lol'd on "nrettap". –  UncleZeiv Jan 30 '12 at 14:44

To search for the lines not containing foo, for example, do:

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

Source: http://vim.wikia.com/wiki/Search_for_lines_not_containing_pattern_and_other_helpful_searches

share|improve this answer
    
I did not know about the \@ directive that lets you reference the previous atom in search. Very useful. –  nelaar Jan 30 '12 at 14:19
    
Even if correct, this is really costly. –  Benoit Jan 30 '12 at 17:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.