This tutorial is about How To Remove All Lines in VIM. Recently I updated this tutorial and will try my best so that you understand this guide. I hope you guys like this blog, How To Remove All Lines in VIM. If your answer is yes after reading the article, please share this article with your friends and family to support us.
Check How To Remove All Lines in VIM
Most Linux and macOS distributions come with Vim or its predecessor Vi pre-installed. If you are a system administrator or just a normal Linux user, knowing the basics of Vim is essential. When working with text files, you will almost always need to delete one or more lines. This article shows how to remove lines in Vim / Vi.
Delete a line
The command to remove a line in Vim is dd.
Here are step-by-step instructions for removing a line:
- Press the Esc key to go to normal mode.
- Place the cursor on the line you want to delete.
- Type dd and hit Enter to delete the line.
Pressing dd multiple times will delete multiple lines.
Delete multiple lines
To delete multiple lines at once, prepend the dd command with the number of lines you want to delete. For example, to remove five lines, you would do the following:
- Press the Esc key to go to normal mode.
- Place the cursor on the first line that you want to delete.
- Type 5dd and hit Enter to delete the next five lines.
Delete a range of lines
The syntax for removing a range of lines is as follows:
For example, to remove lines starting from 3 to 5, you would do the following:
- Press the Esc key to go to normal mode.
- Type: 3,5d and press Enter to erase the lines.
You can also use the following characters to specify the range:
- . (period): the current line.
- $ – The last line.
- %: All lines.
Here are some examples:
- :., $ d: from the current line to the end of the file.
- :., 1d: from the current line to the beginning of the file.
- 10, $ d: from the 10th line to the end of the file.
Delete all lines
To remove all lines, you can use the% symbol that represents all lines or the range 1, $:
- Press the Esc key to go to normal mode.
- Type% d and hit Enter to erase all lines.
Delete lines that contain a pattern
The syntax for removing multiple lines based on a specific pattern is as follows:
The global command (g) tells the delete command (d) to delete all lines that contain the
To match lines that don’t match the pattern, add an exclamation point (!) Before the pattern:
The pattern can be a literal match or a regular expression. Here are some examples:
- : g / foo / d – Remove all lines containing the string “foo”. It also removes the line where “foo” is embedded in larger words, like “soccer”.
- : g! / foo / d – Remove all lines that do not contain the string “foo”.
- : g / ^ # / d – Remove all comments from a bash script. The pattern ^ # means that each line begins with #.
- : g / ^ $ / d – Remove all blank lines. Pattern ^ $ matches all empty lines.
- : g / ^ s * $ / d – Remove all blank lines. Unlike the previous command, this also removes blank lines that have zero or more whitespace characters ( s *).
Final remarks: How To Remove All Lines in VIM
I hope you understand this article, How To Remove All Lines in VIM. If your answer is no, you can ask anything via the contact forum section related to this article. And if your answer is yes, please share this article with your friends and family to give us your support.