14

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?

timss
  • 9,982
  • 4
  • 34
  • 56
whytheq
  • 34,466
  • 65
  • 172
  • 267
  • @hammar - how do I turn my `C+v` in OP into pictures of little buttons - I see it on some posts? – whytheq May 23 '13 at 19:18
  • Use `...` tags, e.g. `Ctrl + V`. Don't overdo it, though. Long Vim commands are often easier to read with the normal code markup. – hammar May 23 '13 at 19:18
  • @hammar - ok - I do have a tendency to overdo new tricks (check timss answer out ...is that ok or overdoing it?) – whytheq May 23 '13 at 19:24
  • 2
    I'd say that's pushing it a little. But that's subjective, of course. Anyway, we're getting off topic here. Ask on [meta] if you want some more opinions. – hammar May 23 '13 at 19:52

7 Answers7

20

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.

Community
  • 1
  • 1
timss
  • 9,982
  • 4
  • 34
  • 56
  • +1 thanks for that reference - stared and saved to read later – whytheq May 23 '13 at 19:31
  • Props for the formatting, though I think it's often better to use the normal notation for Vim commands instead of `` markup. There is no unshifted `"` key or `(` key so it doesn't really make sense to use `` for them. – glts May 23 '13 at 20:19
  • @timss - some markup would have been nice - think you might have over-reacted! – whytheq May 24 '13 at 06:46
  • @whytheq I can't really tell what's best since `d` etc. is both a command but also a mapping. Anyway, I think it's all right now. – timss May 24 '13 at 09:39
  • @timss - for all the markup madness I'd better mark this as the answer – whytheq May 24 '13 at 13:17
  • @whytheq Thanks, I appriciate it. – timss May 24 '13 at 13:23
  • @timss If there exist another `{ }` block inside outer `{ }` block then `di{` doesn't work. – alhelal Feb 13 '18 at 09:32
12

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

FDinoff
  • 30,689
  • 5
  • 75
  • 96
  • 4dd is the obvious simple answer - thanks! seems like just `v` followed by selecting the lines and pressing `d` is ok aswell - my mistake was using `C+v` – whytheq May 23 '13 at 19:28
7

If the block is really large, and you can't be bothered to count the number of lines to delete, but you know the first and last line numbers (:set number helps), you can always go to ex mode and

 :3,1415d

to delete from line 3 through line 1415.

Jens
  • 69,818
  • 15
  • 125
  • 179
7

<C-v> puts you in "visual block mode". In that mode, you act on a rectangle (the "block" in "visual block mode") that may or may not cover the lines you want to delete. d only acts on the characters contained in that block, leaving the lines as is.

What you want is "visual line mode" where you specifically act on lines. From normal mode or from any other visual mode, hit V (Shift + v), move your cursor to define your selection then hit d.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • +1 nice summary - that was my mistake and that is my solution (4dd in normal mode is probably the simplest solution though ...although good for me to practice visual line mode) – whytheq May 23 '13 at 19:34
3

delete 4 lines of text, starting at the line the cursor is on:

4dd

or use the ex command d with line addressing, for example:

:3,24d 

would delete lines 3-24.

.

.

.

Here's some useful special characters when using line addressing with ex:

 .       : current line
 $       : last line
 /text/  : next occurrence of text
 ?text?  : previous occurrence of text
 *       : all text currently on screen
 %       : entire file
 +n      : next n lines
 -n      : previous n lines
Luke
  • 5,567
  • 4
  • 37
  • 66
1

or d with motion e.g.

d3j

also the :d command is flexible too. check the help for detail

Kent
  • 189,393
  • 32
  • 233
  • 301
0

You can use the book marks. Mark the first line with book mark, say, 'e' In command mode (press ESC if in INSERT mode) press

m e 

that marks the first line with bookmark 'e'.

Then go to the last line you want to remove and bookmark that, with say 'f'

m f

You can then goto ex mode (using colon) and delete the lines

:'e,'f d
user50619
  • 325
  • 5
  • 14