Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a :g style command with a range in Vim

Tags:

vim

With Vim, if I want to delete every line which has the text pending I can do:

:g/pending/d

How do I do this with a range? Say I want to delete every line with the text pending but only between lines 0 and 60 (or, between 0 and here) something like:

:0,./pending/d

I have searched, but I'm failing to see how to do this. I know you can do :0,. normal <command> but I'm not sure what the normal mode command to conditionally delete a line is.

p.s. and where do you go in help to learn these things ;)

like image 269
patrickdavey Avatar asked Oct 21 '25 05:10

patrickdavey


1 Answers

Like most if not all Ex commands, :global takes a range. So if :g/pending/d does what you want on the whole buffer it will also do what you want on the given range:

:1,.g/pending/d

As for where to find out about this, well… look no further than Vim's documentation.

:help :global says:

                                                *:g* *:global* *E147* *E148*
:[range]g[lobal]/{pattern}/[cmd]
                        Execute the Ex command [cmd] (default ":p") on the
                        lines within [range] where {pattern} matches.

Which is pretty clear.

NOTE: lines start at 1.

like image 184
romainl Avatar answered Oct 24 '25 05:10

romainl