Download Article
A step-by-step guide to delete and cut lines in your text file or code
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.
Steps
Section 1 of 5:
Delete in Command Mode
-
Press Esc to enter command (normal) mode. 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> .
-
Use the arrow keys to move to the line you want to delete. Alternatively, you can press k to go up, or j to go down. [1] X Research sourceAdvertisement
-
Type dd . Entering dd instantly deletes the current line from your text file.
- If you change your mind, press u to undo the deleted line. [2] X Research source
-
Delete multiple lines. 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).
Advertisement
Section 2 of 5:
Delete in Visual Mode
-
Enter command mode. 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.
-
Move the cursor to the line you want to delete. 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.
-
Press v to enter visual mode. Vim's visual mode makes it especially easy to delete multiple lines.
-
Type a capital V . Make sure to use a capital V, as this command highlights the entire line. [3] X Research source
-
Select additional lines (optional). 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.
-
Press d . This deletes all selected lines.
Advertisement
Section 3 of 5:
Cut and Paste a Line
-
Press Esc to enter command (normal) mode. 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.
-
Move the cursor to the line you want to cut.
-
Type dd . The line will disappear, but not forever.
- If you'd rather copy the line instead of cut it, type yy instead.
-
Move the cursor to another location and press p . This pastes the deleted line to its new location.
Advertisement
Section 4 of 5:
Delete Lines that Contain a Pattern
-
Press Esc to enter command (normal) mode. 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.
-
Type :g/<pattern>/d . 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.
-
Press ↵ Enter to run the command. 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] X Research source
Advertisement
Section 5 of 5:
Delete All Blank Lines
-
Press Esc to enter command mode. If your text file has multiple blank lines that you don't need, you can delete them quickly using Vim's command mode.
-
Delete blank lines that contain only whitespace. 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 .
Advertisement
Expert Q&A
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Tips
- To delete all trailing whitespace characters at the ends of each line in your file, use :%s/\s\+$// .Thanks
- To delete a single word in Vim, select it, then type dw in command mode.Thanks
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement
References
About This Article
Thanks to all authors for creating a page that has been read 5,716 times.
Advertisement