How to delete every other line in Vim?

前端 未结 12 1401
天涯浪人
天涯浪人 2020-12-04 06:13

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:

a         


        
12条回答
  •  旧时难觅i
    2020-12-04 06:57

    As another approach you could also use python if your vim has support for it.

    :py import vim; cb = vim.current.buffer; b = cb[:]; cb[:] = b[::2]
    

    b = cb[:] temporarily copies all lines in the current buffer to b. b[::2] gets every second line from the buffer and assigns it to the whole current buffer cb[:]. The copy to b is necessary since buffer objects don't seem to support extended slice syntax.

    This is probably not the "vim way", but could be easier to remember if you know python.

提交回复
热议问题