How to Delete Multiple Lines of Text in Vim

Pavol Kutaj
1 min readOct 14, 2022

--

The aim of this pagešŸ“ is to learn quick deletions in the vim editor. Why? Learning an editor that is OOTB on a docker image, used by the team on multiple platforms (ā€œUbuntu 18.04.6 LTS (Bionic Beaver)ā€). Using for log parsing, in markdown format (funnily working for terraform logs). Tried Folding sections of Markdown in Vim | bitcrowd, but no luck there.

In any case, to activate line numbers, write

:set number
  • The essential command is dd which deletes a single line

DELETE ABSOLUTE RANGE

  • The syntax for deleting a range of lines is as follows:
  • For example, to delete lines starting from 3 to 5 you would do the following:
  1. Press the Esc key to go to normal mode.
  2. Type :3,5d and hit Enter to delete the lines.

DELETE RELATIVE RANGE: FROM START UNTIL CURSOR

You can also use the following characters to specify the range:

  • . (dot) - The current line.
  • $ - The last line.
  • % - All lines.
  • To delete until the current line use
:1,.d

DELETE RELATIVE RANGE: FROM CURSOR TO N LINES BELOW

  • To delete multiple lines at once, prepend the dd command with the number of lines to be deleted.
  • For example, to delete five lines you would do the following:
  1. Press the Esc key to go to normal mode.
  2. Place the cursor on the first line you want to delete.
  3. Type 5dd and hit Enter to delete the next five lines.

LINKS

--

--

No responses yet