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
- 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
- In Command Mode, navigate to the point where you wish to append text.
- Press
a
to enter Insert Mode and append text after the cursor. - Type in the desired text.
- 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:
- In Command Mode, navigate to the desired line.
- Press
o
to open a new line below and automatically enter Insert Mode. - Alternatively, press
O
to open a new line above the current line and enter Insert Mode. - Input the text you want.
- 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 orP
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 pressEnter
. - To Save and Exit: Type
:wq
and pressEnter
. - To Exit Without Saving: Type
:q!
and pressEnter
.
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.