all4register
all4register

Reputation: 93

What means is vim commnad ":v"or":g!" when it words on range

Vim manual says

:[range]v[global]/{pattern}/[cmd] Execute the Ex command [cmd] (default ":p") on thelines within [range] where {pattern} does NOT match."

but when use command like ":v/{pattern1}/,/{pattern2}/[cmd]", the result is not what I want, and I don't understand why.

For exapler, there is a text file

1
2
3
4
5

When i execute the command ":g/2/,/4/d",then the line from "2" to "4" will be deleted. It's fine.

But if i execute the command ":v/2/,/4/d", it's not work just like i know.

I thinkit should keep the three lines from "2" to "4", deleted others, but it not.

Or, I think the command ":v/2/,/4/d" may works like ":g/[^2]/, /[^4]/d", but it's not, too.

So, what's the exact mean about the command ":v/{pattern1}/,/{pattern2}/[cmd]"?

Upvotes: 0

Views: 109

Answers (1)

Kent
Kent

Reputation: 195029

the command does what it should do. I think you didn't understand the command correctly. I try to explain it.

first, the :g one. You have: :g/p1/,/p2/d we should read the command as:

:g/p1/   "for each line match p1
,/p2/d   "till line match p2, delete.

here you used a range, from line matching p1 till (, comma) line matching p2. with your 1-5 example, vim find the 1st matched line #2, then you have a 2,/4/d, so line 2-4 was removed. :g is not finished, it looks the rest line : the 5 line, it doesn't match /2/, next line, oops, hit EOF, so :g has done its job.

If you made an example from 1 to 20 , you would see some error msg Pattern not found, this happens because, the :g can find /2/, but the range ending /4/ could not be found any longer, it has been removed by last d command. Do a test by yourself, you will see what I meant.

If :g is clear, :v is easy to understand. :v/2/,/4/d

vim search first line doesn't match /2/, it would be the first line 1, then do 1,/4/d, that is, line 1-4 are deleted. :v command is not yet finished, it goes to the line with 5, which is line number 1 now, it doesn't match /2/ either, so vim take it, does a 1,/4/d, but there is no line matching /4/ in your buffer, so pattern not found error message will be displayed. And :v command finished its job.

You should keep in mind that, :g/{pattern}/cmd is not :g{range}cmd. pattern and range are different things. do a :h range to see detail

That's why you got the result. Hope it is clear.

Upvotes: 2

Related Questions