1. Overview
Vi is a popular text editor in Linux systems and is preloaded on the majority of Linux environments. Thus, a Linux user or administrator should know the fundamentals of the vi editor.
We often need to delete single or multiple lines while using vi. In this tutorial, we’ll learn how to delete selected text in the vi editor.
2. Sample vi Editor Session
Let’s open the vi editor with filename sample and then enter some texts in it as shown below:
3. Delete a Single Line
We can delete only one line within the sample vi editor using the dd command.
First, we’ll need to open a vi session and then press Esc to be in the normal mode. Next, we’ll position the cursor on top of the line that we like to remove. We can move to the line that we want to delete using the arrow keys.
In our example, we’ve placed the cursor on the first line, Welcome to Linux! – Line 1:
After placing the cursor at the correct place, we’ll enter dd and then we’ll get the desired output:
We can see from the output that the text Welcome to Linux! – Line 1 has been deleted.
4. Delete Multiple Lines
We can also delete multiple lines within the vi editor using a variation on the dd command. To delete more than one line at once, we need to type the number of deleted lines before the dd command.
Let’s delete the first two lines.
We need to first open a vi session and then press Esc to be in the normal mode.
Then, we’ll position the cursor on top of the line from where the deletion should begin. We can move to the starting line that we want to delete using the arrow keys.
In our example, we have placed the cursor on the first line, Welcome to Linux! – Line 1:
After positioning the cursor, we’ll enter 2dd and observe the output:
As a result, we find that the lines Welcome to Linux! – Line 1 and Welcome to Linux! – Line 2 has been deleted.
5. Select and Delete Texts
Let’s discuss some standard solutions for selecting and deleting texts.
5.1. Using the Shift + V Keys
We can select and delete specific texts using the Shift + V keys and then using the dd command.
Let’s delete the line Welcome to Linux! – Line 4.
First, we’ll need to open a vi session and press v to be in the VISUAL LINE mode.
Next, we’ll select the line that we want to delete using the Shift + V keys.
In this case, we have selected Welcome to Linux! – Line 4:
After selecting the correct line, we’ll type dd and check the output:
So, in the above example, we see that the text Welcome to Linux! – Line 4 has been deleted.
We must note that this approach does not work if the VISUAL LINE mode is not available, especially in the older vi implementations.
5.2. Using the {d} Command
We can select and delete specific paragraphs using the {d} command.
We’ll modify our vi sample editor and then split its contents into two paragraphs:
We have taken the lines Welcome to Linux! – Line 1, Welcome to Linux! – Line 2, and Welcome to Linux! – Line 3 in paragraph 1 and Welcome to Linux! – Line 4 and Welcome to Linux! – Line 5 in paragraph 2.
Let’s delete paragraph 1.
We need to first open a vi session and then move to the start of the paragraph that we want to delete using the arrow keys.
Next, we’ll input {d} to get the desired output:
As a result, in the above example, we see that paragraph 1 has been deleted.
5.3. Using Text Line Number
We can delete a line of text within the vi editor using its line number. We can display the line number of texts within the vi editor using the set number command:
:set number
On running the command above, we should see the line numbers of all the lines within our editor:
Let’s delete the lines having numbers three to five using a command in the format:
: starting line number, ending line number d
Here, the starting line number is 3. The ending line number is 5. So, our command should be:
: 3, 5 d
After running the above command, we’ll get the output as:
So, in the above example, we see that lines Welcome to Linux! – Line 2, Welcome to Linux! – Line 3, and Welcome to Linux! – Line 4, having line numbers 3, 4, and 5, respectively, have been deleted. We’ve also gotten the message 3 fewer lines due to the deletion of these three lines.
6. Conclusion
In this article, we learned how to delete selected text from a vi editor in Linux using various techniques.