How to Delete One or More Lines in Vi and Vim
Vi and Vim are popular text editors used in Linux and other Unix-based operating systems. These editors provide powerful editing capabilities, including the ability to delete one or more lines in a text file. In this article, we will discuss how to delete one or more lines in Vi and Vim.
Deleting a Single Line in Vi and Vim
To delete a single line in Vi and Vim, move the cursor to the line you want to delete and type the following command:
dd
This command will delete the line the cursor is on. The deleted line will be copied into the default buffer, and can be pasted with the p command.
Deleting Multiple Lines in Vi and Vim
To delete multiple consecutive lines in Vi and Vim, move the cursor to the first line you want to delete and type the following command:
ndd
where n is the number of lines you want to delete. For example, if you want to delete the next three lines, type:
3dd
This command will delete the next three lines, including the line the cursor is on.
Deleting Non-Consecutive Lines in Vi and Vim
To delete non-consecutive lines in Vi and Vim, use the following command:
:nd,nw
This command will delete all lines between line n and line w, inclusive.
Where n and w are the line numbers of the first and last line you want to delete. For example, to delete lines 5, 7, and 9, type:
:5,5d
:7,7d
:9,9d
This command will delete lines 5, 7, and 9, one at a time.