In a file, I want to be able to delete context around a search pattern.
By context I mean: a) 'n' lines before the pattern b) 'n' lines after the pattern c) 'n' lines after and before the pattern d) do a,b,c by deleting the pattern line as well e) do a,b,c without deleting the pattern line
Is there some way to do it using :g/
or :%s
or some other way?
I can do this with macros, but that's not what I am looking for.
Here is sample text:
search_pattern random text 1
line below search pattern(delete me)
abc def
pqr stu
...
line above search pattern(delete me)
search_pattern random text 2
line below search pattern(delete me)
...
In each case, it's possible to utilize either a relative range or an offset and an argument to d
. The more logically straightforward option depends on the particular case; I tend to use explicit ranges in the inclusive cases (since you can usually omit half of the range), and an argument to d
otherwise.
Before the pattern, inclusive:
:g/regex/-3,d
:g/regex/-3d4
Before the pattern, exclusive:
:g/regex/-3,-1d
:g/regex/-3d3
After the pattern, inclusive:
:g/regex/,+3d
:g/regex/d4
After the pattern, exclusive:
:g/regex/+1,+3d
:g/regex/+1d3
Before and after, inclusive:
:g/regex/-3,+3d
:g/regex/-3d7
Before and after, exclusive:
:g/regex/-3,-1d|+1,+3d
:g/regex/-3d3|+1d3
Note that these commands will fail with E16: Invalid range
if the range goes past the beginning or end of the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With