PDF download Download Article
A step-by-step guide to delete and cut lines in your text file or code
PDF download Download Article

If you need to delete a line in Vim, there are a few ways to go about it. To delete just one line, just enter command mode, use the arrow keys to navigate to the line, then press "dd." You can also delete lines easily in visual mode, including multiple lines at once. This wikiHow article will teach you the simplest ways to delete one or more lines from a text file using the Vim text editor.

Things You Should Know

  • To delete a line, move the cursor to that line, press Esc, and then type dd.
  • To delete multiple lines, type a number before dd. For example, 8dd deletes the current line and the 7 that follow.
  • You can also delete lines in visual mode by highlighting them and typing d.
Section 1 of 5:

Delete in Command Mode

PDF download Download Article
  1. You can use the "dd" command in Vim's command mode to delete one or more lines easily.
    • This method will also work for the original Vi text editor .
    • If it's your first time using Vim, you can open a file for editing using the command vim <yourfilename> .
  2. Alternatively, you can press k to go up, or j to go down. [1]
    Advertisement
  3. Entering dd instantly deletes the current line from your text file.
    • If you change your mind, press u to undo the deleted line. [2]
  4. If you want to delete more than one line, just type the number of lines you want to delete before the dd . Here's how:
    • Move your cursor to the first line you want to delete.
    • If you want to delete 3 lines, beginning with the current line, type 3dd (replace "3" with the number of lines you want to delete). This deletes the current line and the following 2 lines.
    • If you wanted to delete multiple lines above the current line instead of below it, you'd use d3k (replace "3" with your preferred number of lines).
  5. Advertisement
Section 2 of 5:

Delete in Visual Mode

PDF download Download Article
  1. If you're not already in command (normal) mode, press Esc to enter it now. From command mode, you can easily switch to visual mode, which makes it easy to delete one or more lines.
  2. Before ending visual mode, use the arrow keys to move to the line you want to delete. If you need to delete multiple lines, just move the cursor to the first line in the group you want to remove.
    • If you're not already in command mode, press Esc to enter it first.
  3. Vim's visual mode makes it especially easy to delete multiple lines.
  4. Make sure to use a capital V, as this command highlights the entire line. [3]
  5. If you want to delete multiple lines, use the up and down arrows to select all lines that you want to delete. This expands the selection.
  6. This deletes all selected lines.
  7. Advertisement
Section 3 of 5:

Cut and Paste a Line

PDF download Download Article
  1. If you want to remove a line from one location and move it to another, you're in luck—the dd command also works like the Cut command you may recognize from your PC or Mac. Not only does it delete the current line, it also saves it to a virtual clipboard.
  2. The line will disappear, but not forever.
    • If you'd rather copy the line instead of cut it, type yy instead.
  3. This pastes the deleted line to its new location.
  4. Advertisement
Section 4 of 5:

Delete Lines that Contain a Pattern

PDF download Download Article
  1. If you want to delete all lines that contain a certain character or set of characters in order, you can do with a simple command.
  2. For example, if you want to delete all lines in an HTML file that contain the word "copyright," your command will look like this: :g/copyright/d .
    • Keep in mind that the command is case-sensitive.
  3. This deletes all lines in the text file that match the pattern.
    • If you'd rather delete all lines that don't match the pattern, use :v/<pattern>/d instead. [4]
  4. Advertisement
Section 5 of 5:

Delete All Blank Lines

PDF download Download Article
  1. If your text file has multiple blank lines that you don't need, you can delete them quickly using Vim's command mode.
  2. This instantly removes all blank lines from the text file . [5]
  3. If you still have blank lines in your file, there may be spaces and tabs in those lines that you can't see. To remove them all at once, use the command :g/^\s\+$/d .
  4. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 3,946 times.

      Is this article up to date?

      Advertisement