Skip to main content

A Primer on Basic Editing Operations with VI Editor

The VI editor, integral to Unix and Unix-like operating systems, is a versatile and reliable text editor. This tutorial is a beginner’s guide, focusing on creating a new file and performing basic editing operations using VI, which will help new users to get acquainted with its functionalities and diverse editing capabilities.

Creating a New File

  1. Open the terminal and type the following command to create a new file or edit an existing one using VI:
    vi filename.txt

Understanding VI Modes

Before proceeding to editing, it's imperative to understand that VI operates primarily in two modes:

  • Command Mode: Utilized for navigation, deletion, copying, and pasting.
  • Insert Mode: Employed for inserting and modifying text.

Entering Text (Insert Mode)

To begin inserting text, press i in Command Mode to enter Insert Mode. You can now type in your content. Below are a few operations you can perform in Insert Mode:

  • Inserting Text: Simply start typing the desired text.
  • New Line: Press the Enter key.

After finishing, press the Esc key to return to Command Mode.

Appending Text

  1. In Command Mode, navigate to the point where you wish to append text.
  2. Press a to enter Insert Mode and append text after the cursor.
  3. Type in the desired text.
  4. Press the Esc key to return to Command Mode.

Opening a New Line

To open a new line below or above the current line, follow the steps below:

  1. In Command Mode, navigate to the desired line.
  2. Press o to open a new line below and automatically enter Insert Mode.
  3. Alternatively, press O to open a new line above the current line and enter Insert Mode.
  4. Input the text you want.
  5. Press the Esc key to revert to Command Mode.

Editing Text in Command Mode

  • Deleting Characters: Navigate to the character and press x.
  • Deleting Entire Line: Navigate to the line and press dd.
  • Copying a Line: Navigate to the line and press yy.
  • Pasting a Line: Navigate to the desired point and press p to paste below the cursor or P to paste above the cursor.

Saving and Exiting the VI Editor

Once you've concluded editing, it’s crucial to save your work and exit:

  • To Save: In Command Mode, type :w and press Enter.
  • To Save and Exit: Type :wq and press Enter.
  • To Exit Without Saving: Type :q! and press Enter.

Conclusion

This refined step-by-step tutorial provides beginners with concise guidance on creating a new file and performing basic editing operations in the VI editor, including appending text and opening new lines above or below a given line. Acquiring proficiency in these fundamental operations allows users to adeptly navigate and modify text, fostering enhanced productivity in various Unix-based environments, whether it be in software development or system administration.

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