32

If I want to select particular text of line and delete lines, how can I do that in vi (give the simplest means)?

32

You can also use V in view mode to launch "VISUAL LINE" mode. Select your lines and type d to delete them.

2
  • 7
    That is, Shift + V.
    – Zaz
    Aug 2 '10 at 12:40
  • 1
    What if V mode is not implemented (as in old Vi implementations?)
    – Campa
    Mar 15 '18 at 9:25
16

Move to the line you want to delete, either using the arrow keys or the j/k keys, and type dd. You can then save and exit by typing :x (or ZZ). A number can also be prefixed to dd to delete multiple lines, e.g. 3dd deletes 3 lines.

For more Vi commands, take a look at this handy Vi cheat sheet.

1
  • 1
    dd is the quickest way to delete a line.
    – Raptor
    Aug 2 '10 at 11:57
3

go to the first line you wish to delete, and type (in view mode) d[x-1] to delete x lines.

3

Another tip: if you want to delete a text paragraph, move to beginning of that paragraph with { and then type d}

Or, in other words, {d}

5
  • 3
    Or dap, delete a paragraph
    – user1686
    Aug 2 '10 at 9:54
  • @grawity: dap doesn't work for me in Vi.
    – Zaz
    Aug 2 '10 at 11:19
  • 1
    @Josh: 97% of the time, when someone says "vi", he either means "vim" or "vim running in vi-compatibility mode". So I suggested a vim-specific command (see also :help text-objects).
    – user1686
    Aug 2 '10 at 20:20
  • @grawity: Sorry, I am running Vim, but dap still has no effect.
    – Zaz
    Aug 2 '10 at 20:52
  • @Zaz: Perhaps your .vimrc file had other settings, or perhaps @user1686's had different settings.
    – jvriesem
    Feb 16 '20 at 2:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy