FANDOM


(Comments)
(Comments: Answer to the comment.)
Line 46: Line 46:
 
==Comments==
 
==Comments==
 
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)
 
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.
  +
In this way you can 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 I think the faster way to do that is to use :[range]d several times.
  +
Another solution is to use exe to build a pattern:
  +
<pre>
  +
exe "g/".join(map(filter(range(line("$")), 'v:val%2'), '"\\%".v:val."l"'), '\|')."/d"
  +
</pre>

Revision as of 10:10, September 24, 2012

Tip 213 Printable Monobook Previous Next

created 2002 · complexity basic · author tarjei · version 5.7


The ex command g is very useful for acting on lines that match a pattern. You can use it with the d 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" (the first command is optional; it shows the lines that the second command will delete):

:g/profile
:g/profile/d

More complex patterns can be used, such as deleting all lines that are empty or that contain only whitespace:

:g/^\s*$/d

To delete all lines that do not contain a pattern, use g!, like this command to delete all lines that are not comment lines in a Vim script:

:g!/^\s*"/d

Note that g! is equivalent to v, so you could also do the above with:

:v/^\s*"/d

The next example shows use of \| ("or") to delete all lines except those that contain "error" or "warn" or "fail" (:help pattern):

:v/error\|warn\|fail/d

See also

Comments

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:

:g/.*/if line('.')%2|call setline(line('.'), '===delete===')|endif
:g/^==delete==$/d

If you simply put delete inside the if statement all the lines will be deleted. In this way you can delete lines from several different ranges:

: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

But I think the faster way to do that is to use :[range]d several times. Another solution is to use exe to build a pattern:

exe "g/".join(map(filter(range(line("$")), 'v:val%2'), '"\\%".v:val."l"'), '\|')."/d"
Community content is available under CC-BY-SA unless otherwise noted.