A vim pattern search and delete example

vim FAQ: How do I perform a vim search and delete using a vim regular expression pattern (regex)?

Every once in a while when I'm using the vi editor (or vim) I find myself in a situation where I need to delete a bunch of lines in the file that match a particular pattern. In my younger days I used to get out of vi and then use sed or grep to get rid of those lines, but it turns out there's a real easy way to do this in vi.

How to search and delete in vi/vim

Seeing that this is an election year ... assuming you have a file open in vim, and you want to delete all lines containing the string "George Bush", you'd just enter this vim search and delete command:

:g/George Bush/d

That's all you need to do. This vim search/delete command will find and then delete all lines containing the pattern "George Bush".

How this vi search/delete command works

Here's a brief explanation of how this vi/vim search and delete command works:

  • The : character says "put vim in last-line mode".
  • The g characters says "perform the following operation globally in this file".
  • The forward slash characters enclose the pattern I'm trying to match. In this case it's a very simple sequence of characters, but it can also be a more complicated regular expression.
  • The d at the end of the command says "When you find this pattern, delete the line".

vim - delete all lines not matching a pattern

To delete all lines in a vim not matching a given pattern you can use one of these two commands:

:g!/foo/d

or this one:

:v/foo/d

Both of these commands delete all lines not contain the simple pattern 'foo'.

Hopefully the first command is fairly easy to remember, as the "!" operator is often used to negate the meaning of a command in many Unix tools and languages. I can probably remember the "v" command by thinking of the "grep -v" command, which reverses the meaning of a normal grep search.

vim search and delete with regex patterns

Now that you've seen that example, all you need to do to delete with a regular expression (regex) is to replace the vim search pattern shown above with your own regex pattern. For instance, a simple regex pattern for this same example is  "G.* Bush".

If you'd like more vim search/delete regex examples, just leave a note in the Comments section below and I'll be glad to work some up.

very cool! I was fumbling

very cool! I was fumbling with %s/<regex> and trying to erase matching lines, but this makes it real easy. thanks!

hey! I need to search and

hey!

I need to search and delete, edit some keys, for which i want to invoke vi in shell script would you plz provide me with some solutin how to invoke vi in a script and then search and delete.

Use sed

If you're trying to do something from a shell script, you'll want to use the sed command instead of the vi editor. "sed" stands for "streamline editor", and it lets you edit files without the need to open an editor. Here's a link to a few sed examples on our website.

Deleting lines in vi editor

Hi. How do you delete lines in vi with a NOT condition? For example, you site:
:g/George Bush/d
to get rid of all of the lines with "George Bush" in them.

But here's the one that I can't figure out: How would you delete all lines in the file that DO NOT mention "George Bush"? Is that even possible?

In my case, I have done a big "grep" search and fed it into a temp file. Now I want to further refine that search by getting rid of all of the lines in the temp file that do not have a "PRINT" statement in them.

If you know the answer, it would be much appreciated.
Thanks,
- Marc

I think that I answered my

I think that I answered my own question, on the last question that I sent you. An easy solution is to just do another "grep" on the temp file to pull what I want. That can be done repeatedly.

thanks

Delete lines not matching a pattern

Glad you found grep. In vi, it looks like these two approaches both work to delete all lines not matching a pattern:

:g!/foo/d

or this one:

:v/foo/d

Both of these commands delete all lines not contain the simple pattern 'foo'.

This seems like a good tip, so I'll add it to the original article. Thanks.

Great! They both work

Great! They both work perfectly. I've looked around for these NOT parameters a few times but never found them. It's a hard thing to Google on. Now I'll have to write the commands down in a few places so that I remember them the next time I need them.

- Marc

Thanks for the awesome tip!

Thanks for the awesome tip!

Post new comment

The content of this field is kept private and will not be shown publicly.