How to Delete One or More Lines in Vi and Vim
Editing text files directly from the Linux terminal can be tough, especially if you’re using a complicated text editor like Vi or Vim. Beginner users have a tough time wrapping their heads around command-line text editors and often find it hard to memorize the key bindings used to perform basic operations.
Even deleting lines in Vi and Vim can be nerve-wracking for newcomers. Using a rudimentary tool like a text editor doesn’t have to be so hard.
Here’s how you can remove one or more lines from a text file using Vi and Vim.
How to Delete a Single Line in Vi/Vim
There’s a better way of deleting complete lines in Vi and Vim than spamming Backspace on the keyboard.
To delete a line in Vi or Vim, switch to normal mode first. If you’re in command mode or insert mode, you can switch back to normal mode by pressing Escape. If you don’t know what modes are, it’s a good opportunity to learn the basics of Vim.
Highlight the line you want to delete, then hit dd or D on the keyboard. The editor will automatically remove the whole line from the file.
Vi and Vim commands are case-sensitive, which means d and D are identified as two separate commands.
Hitting dd or D multiple times will delete several lines one by one. The next line will be automatically highlighted once the former is deleted.
To delete a specific line in Vi/Vim, press Escape to enter into command mode, then type nd to delete a particular line, where n is the number of the line you want to remove.
For instance, if you want to delete the 150th line in the /var/log/messages file, you’ll open it with Vi/Vim and then type 150d to remove the line.
Deleting Multiple Lines in Vi and Vim
If you find repeatedly hitting keys on the keyboard inconvenient, you can remove multiple lines at once by tweaking the aforementioned command. Instead of simply pressing dd, you can specify the number of lines you want to delete.
For example, typing 3dd will delete the next three lines from the file.
If you want to delete multiple lines within a specific range, you’ll have to switch to command mode first (press Escape while in normal mode). Then, use the following syntax to create your own deletion command:
For example, to delete the lines between the range one and five:
You can also use wildcard characters in the command above.
- . (Dot): Refers to the current line
- $ (Dollar): Denotes the end of the file
- % (Percentage): Matches all the lines in the file
:.,5d # deletes lines between the current line and the fifth line
:.,$d # removes all lines starting from the current line till the end
:%d # clears the entire file
Remove Lines Based on Regular Expressions
Regular expressions are useful when you want to search for and delete lines containing a pattern. You can delete lines containing a specific word, lines starting with a particular letter, and more. Inside Vi/Vim, you can do this using the g command, which stands for global.
The basic syntax of the command is:
Since you want to delete stuff, you’ll replace command in the string above with d. Regarding the pattern, you can use any valid regular expression to match the lines you need to remove in a file.
For example, to delete lines containing the word “delete,” enter the following:
You can inverse this operation by either prefacing the first slash (/) with an exclamation mark (!) or by substituting g with v, which acts as the inverse of g.
To match and delete lines starting with the letter “T,” use:
It takes a lot of practice and repetition to master command-line tools like Vi and Vim. To make it easier for yourself, bookmark our Vim commands cheat sheet for later reference.
Spamming Backspace? No Thanks! I Use Vi/Vim
If you want to become a Linux power user, learning the ins and outs of command-line utilities is the direction you should be heading towards. On Linux, you can use various shortcuts and key bindings to perform tasks that would otherwise take a lot of time and effort.
For those who are looking forward to mastering Vim, there’s good news. The vim package comes with Vimtutor, a free command-line tutor that will help you use Vim to its true potential.