Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a block of text in Vim

Tags:

vim

So I can delete a text+line using dd (normal mode) and all the below text moves up a line.

I can go into visual mode using Ctrl+v

If I then say do 0 > C+v > jjj > $ > d the text of 4 rows is deleted but the lines are not deleted.

How do I delete a block of text and delete the lines at the same time so any preceding lines of text move up to the cursor?

like image 375
whytheq Avatar asked May 23 '13 19:05

whytheq


2 Answers

For something like this I usually use shift+v, jjj...d, but you could delete using text objects as well.
See :h text-object. A few examples:

di" - delete inside "
dap - delete around paragraph

And you could of course use other commands than d, such as c or v.
Something I use all the time is ci( and ci" for editing content inside () and "".

More cool examples using text-objects and visual mode can be found here:
What is your most productive shortcut with Vim?


You could use as well, i.e. 4dd as mentioned by FDinoff, or a range, mentioned by Jens. However in most scenarios I personally believe using visual line (shift+v) is more flexible, and you don't have to count lines or anything. It's easy to remember, you see the result instantly, you won't miss counting lines and it'll work even if you're at the top/bottom on the screen.

like image 155
timss Avatar answered Sep 28 '22 06:09

timss


Use either 4dd to delete 4 lines of text.

Or

Use linewise visual block. <S-v> then move to the last line you want to delete then press d

like image 26
FDinoff Avatar answered Sep 28 '22 05:09

FDinoff