The VI editor is renowned for its efficiency and versatility in text manipulation within Unix and Unix-like operating systems. One of the foundational operations in VI is deleting text, which, due to VI’s modality, can be done in numerous ways, each suiting different contexts and preferences. In this article, we will explore the various commands for text deletion in VI, along with practical examples, enabling readers to effectively remove text with precision.
Creating a Sample Text File
Before we delve into the deletion commands, let's create a sample text file that readers can use to practice the commands. Open your terminal and create a new file using VI:
Enter the Insert Mode by pressing
i and type in the following lines of text:
This is a sample text file.
We will use this file to demonstrate
the various text deletion commands
in the VI editor.
Esc to exit Insert Mode, and type
:wq to save and exit the file.
Deleting Text in VI Editor
Now that we have our sample file, let’s go through the different commands to delete text in VI.
1. Deleting a Character
To delete a character, simply move the cursor over the character in Command Mode
2. Deleting a Line
To delete the entire line where the cursor is positioned, press
dd in Command
3. Deleting Multiple Lines
To delete multiple lines, type the number of lines to be deleted followed
dd. For instance,
3dd will delete the current line and the two lines
4. Deleting to the End of the Line
To delete from the cursor position to the end of the line, press
5. Deleting to the Beginning of the Line
To delete from the cursor position to the beginning of the line, press
followed by zero).
Table: VI Deletion Commands and Their Descriptions
|Deletes the character under the cursor.
|Deletes the entire line where the cursor is located.
|Deletes the N number of lines starting from the cursor. For example,
5dd deletes the current line and the four lines following it.
|Deletes from the cursor position to the end of the line.
|Deletes from the cursor position to the beginning of the line.
|Deletes from the cursor to the end of the word.
|Deletes from the cursor to the beginning of the word.
D, deletes to the end of the line.
|Deletes from the cursor position to the end of the file.
|Deletes from the cursor position to the beginning of the file.
|Deletes from the cursor position to the specified line number N. For instance,
d20G deletes from the cursor position to line 20.
Let’s practice these commands on our sample text file.
Delete a Character: Open the file (
vi sample_text.txt), move the cursor over any character and press
Delete a Line: Place the cursor on any line and press
Delete to the End of Line: Place the cursor somewhere in the middle of a line and press
Delete Multiple Lines: Place the cursor at the beginning of a line and type the number of lines to be deleted followed by
dd, for example,
Delete to the Beginning of Line: Place the cursor somewhere in the middle of a line and press
Mastering deletion commands in the VI editor is essential for anyone working in Unix-like environments. The plethora of options provided by VI for deleting text allows users to choose the most efficient method for their needs, whether it's removing a single character, a whole line, or a block of text. Practicing these commands on a sample file will help in developing proficiency and in ensuring precise text manipulation within the VI editor, leading to enhanced productivity in text editing tasks.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.