Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete every other line in Vim?

Tags:

vim

I would like to delete every other line from a Vim buffer, starting with the second one, i.e., lines 2, 4, 6, etc. For example, if the buffer’s contents is:

aaa bbb ccc ddd eee fff 

then, after the desired line removal, it should become:

aaa ccc eee 

Which Vim commands can be used to automate this operation?

like image 721
brazh Avatar asked Dec 22 '09 14:12

brazh


2 Answers

An elegant (and efficient) way to accomplish the task is to invoke the :delete command (see :help :d) for the + line range (same as .+1) addressing the line following the current one (see :help {address}), on every line (see :help /^) using the :global command (see :help :g):

:g/^/+d 
like image 63
ib. Avatar answered Sep 20 '22 17:09

ib.


You can use a macro for this. Do the following.

  • Start in command mode.
  • Go to the beginning of the file by pressing gg.
  • Press qq.
  • Click arrow down and press dd after.
  • Press q.
  • Press 10000@q

PS: To go to command mode just press Escape a couple of times.

like image 35
rui Avatar answered Sep 19 '22 17:09

rui