Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Delete all lines containing a pattern
Back to page
Edit
Edit source
View history
Talk (0)
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
Anti-spam check. Do
not
fill this in!
{{TipImported |id=213 |previous=212 |next=214 |created=2002 |complexity=basic |author=tarjei |version=5.7 |rating=30/33 |category1=Usage |category2= }} The ex command <code>g</code> is very useful for acting on lines that match a pattern. You can use it with the <code>d</code> command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern. For example, to delete all lines containing "profile" (remove the <code>/d</code> to show the lines that the command will delete): <pre> :g/profile/d </pre> More complex patterns can be used, such as deleting all lines that are empty or that contain only whitespace: <pre> :g/^\s*$/d </pre> To delete all lines that do ''not'' contain a pattern, use <code>g!</code>, like this command to delete all lines that are not comment lines in a Vim script: <pre> :g!/^\s*"/d </pre> Note that <code>g!</code> is equivalent to <code>v</code>, so you could also do the above with: <pre> :v/^\s*"/d </pre> The next example shows use of <code>\|</code> ("or") to delete all lines ''except'' those that contain "<code>error</code>" or "<code>warn</code>" or "<code>fail</code>" ({{help|pattern}}): <pre> :v/error\|warn\|fail/d </pre> ==See also== *[[VimTip72|Remove unwanted empty lines]] *[[VimTip1557|Filter buffer on a search result]] *[[VimTip282|Folding with Regular Expression]] *[[VimTip227|Power of g]] ==Comments== Can we delete/not delete the line that precedes the search string? like, in the below example i want the command to search for "keyword" and then ratain that and the line before that and deletes the test? Thank you! << file starts>> this is the line that preceeds the search string keyword asdfgf asdfgf lkjhj lkjhj << file ends>> :Easy, supply a [[Range]] to the <code>d</code> command: <code>:g/keyword/-1d</code>. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 22:09, May 20, 2015 (UTC) ---- Can we remove all even numbered lines in a file using this feature. can we do some kind of math in the pattern. (ex: \=line(".") % 2) Not really, but you can do that in two steps: <pre> :g/.*/if line('.')%2|call setline(line('.'), '===delete===')|endif :g/^==delete==$/d </pre> If you simply put delete inside the if statement all the lines will be deleted. Much faster solution is to record a macro "ddj" and play it over the file. You could delete lines from several different ranges: <pre> :let range = range(10,15)+range(20,25)+range(30,35) :g/.*/if index(range, line('.')) != -1|call setline(line('.'), '===delete===')|endif :g/^===delete===$/d </pre> But again I think the faster way to do that is to use :[range]d several times. How would you instead of deleting, replace matched lines with a ''single'' newline between remaining lines? Use s/// instead. Make sure your pattern matches the whole line (by using wildcards and/or anchors), and replace it with nothing: :s/.*pattern.*//
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Help
(
view source
)
Template:Navigation
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab