Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: find empty lines and print them with 1,2,3 lines after and before

Tags:

vim

I can find empty lines doing this command :g/^$. How to extend this command to print 1 line (2 lines, etc.) before exact empty line and after?

like image 290
Hello World Avatar asked Sep 12 '25 18:09

Hello World


1 Answers

:g/^$/.,+2p

will print every empty line and the two lines below. Use :# instead of :p if you also want the line numbers.

Note that this won't work if the matched line is the last.

See :help :p, :help :#, and :help :range.

like image 194
romainl Avatar answered Sep 15 '25 13:09

romainl