Skip to main content

Mastering Text Deletion in VI Editor: A Comprehensive Guide

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:

vi sample_text.txt

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.

Press 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 and press x.

2. Deleting a Line

To delete the entire line where the cursor is positioned, press dd in Command Mode.

3. Deleting Multiple Lines

To delete multiple lines, type the number of lines to be deleted followed by dd. For instance, 3dd will delete the current line and the two lines following it.

4. Deleting to the End of the Line

To delete from the cursor position to the end of the line, press D.

5. Deleting to the Beginning of the Line

To delete from the cursor position to the beginning of the line, press d0 (d followed by zero).

Table: VI Deletion Commands and Their Descriptions

CommandDescription
xDeletes the character under the cursor.
ddDeletes the entire line where the cursor is located.
Nd or NddDeletes the N number of lines starting from the cursor. For example, 5dd deletes the current line and the four lines following it.
DDeletes from the cursor position to the end of the line.
d0Deletes from the cursor position to the beginning of the line.
dwDeletes from the cursor to the end of the word.
dbDeletes from the cursor to the beginning of the word.
d$Same as D, deletes to the end of the line.
dGDeletes from the cursor position to the end of the file.
d1G or dggDeletes from the cursor position to the beginning of the file.
dN or dNGDeletes from the cursor position to the specified line number N. For instance, d20G deletes from the cursor position to line 20.

Practical Examples

Let’s practice these commands on our sample text file.

  1. Delete a Character: Open the file (vi sample_text.txt), move the cursor over any character and press x.

  2. Delete a Line: Place the cursor on any line and press dd.

  3. Delete to the End of Line: Place the cursor somewhere in the middle of a line and press D.

  4. 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, 3dd.

  5. Delete to the Beginning of Line: Place the cursor somewhere in the middle of a line and press d0.

Conclusion

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.

YouTube @cloudaffle