How one can Delete One or Extra Strains in Vi and Vim

Editing text files directly from the Linux terminal can be tricky, especially if you're using a complicated text editor like Vi or Vim. Beginners find it difficult to dig into command line text editors and often find it difficult to remember the keyboard shortcuts used to perform basic operations.

Deleting lines in Vi and Vim can also be a nerve-wracking job for newbies. But using a rudimentary tool like a text editor doesn't have to be that difficult.

How to use Vi and Vim to remove one or more lines from a text file.

How to delete a single line in Vi / Vim

There is a better way to delete entire lines in Vi and Vim than by spamming Backspace on the keyboard.

To delete a line in Vi or Vim, switch to normal mode first. If you are in Command Mode or Insert Mode, you can press. return to normal mode Escape.

Highlight the line you want to delete, then press dd or D. on the keyboard. The editor automatically removes the entire line from the file. Note that the Vi and Vim commands are case-sensitive D. and D. are identified as two separate commands.

Beat dd or D. multiple deletes several lines one after the other. The next line is automatically highlighted as soon as the former is deleted.

Related: How to Use Vim: A Guide to the Basics

Deleting multiple lines in Vi and Vim

If you feel uncomfortable with repeatedly pressing keys on the keyboard, you can remove multiple lines at once by tweaking the above command. Instead of just pressing dd, you can specify the number of lines to be deleted.

For example typing 3dd deletes the next three lines from the file.

If you want to delete lines within a certain range, you must first switch to Command mode (press Escape in normal mode). Then use the following syntax to create your own delete command:

: (Beginning), (end) d

For example, to delete the lines between area one and five:

: 1.5d

You can also use wildcard characters in the above command.

  1. . (Point): Refers to the current line
  2. $ (Dollar): Indicates the end of the file
  3. % (Percent): Matches all lines in the file

For example:

:., 5d # deletes lines between the current and the fifth line.
:., $ d # removes all lines from the current line to the end.
:% d # will delete the entire file

Related: The Vim Linux Command-Line Editor Cheat Sheet

Spam backspace? I'm sorry, I am using Vi / Vim

If you want to become a Linux power user, you should become familiar with the ins and outs of command line utilities. On Linux, you can use various keyboard shortcuts and keyboard shortcuts to perform tasks that would otherwise take a lot of time and effort.

For those looking forward to mastering Vim, there is good news. The vim package comes with Vimtutor, a free command line tutor to help you get the most out of Vim.

Master the Vim text editor on Linux with Vimtutor

Learning how to use a terminal-based text editor like Vim can be difficult. But you don't have to do all this trouble, not with Vimtutor.

Continue reading

About the author

Deepesh Sharma
(93 published articles)

Deepesh is Junior Editor for Linux at MUO. He writes informational guides on Linux with the aim of providing a blissful experience for all newbies. I'm not sure about movies, but if you want to talk about technology, he's your man. In his spare time he can be found reading books, listening to different genres of music, or playing guitar.

More
From Deepesh Sharma

Subscribe to our newsletter

Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive offers!

Click here to subscribe

Leave a Reply

Your email address will not be published. Required fields are marked *