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:
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:
:character says "put vim in last-line mode".
gcharacters 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.
dat 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:
or this one:
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 "
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 with %s/<regex> and trying to erase matching lines, but this makes it real easy. thanks!
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.
Hi. How do you delete lines in vi with a NOT condition? For example, you site:
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.
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.
Glad you found grep. In vi, it looks like these two approaches both work to delete all lines not matching a pattern:
or this one:
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 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.
Thanks for the awesome tip!