EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Unix Tutorial VI Command in Unix
 

VI Command in Unix

Updated May 30, 2023

VI Command in Unix

 

 

Introduction to VI Command in Unix

In Unix Shell Scripting, the VI editor is one of the most used and very useful text editors. It is the default editor that is installed in Unix OS. The most usage of vi editors is to create a new file, edit an existing one, or just read one. In this topic, we will learn about VI Command in Unix.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Few reasons make VI editors mostly use the editor

  • This editor is default present in all Linux or Unix OS.
  • The most usage of vi editors is to create a new file, edit an existing one, or just read one.
  • The editor is user-friendly and flexible to use. So, users prefer using this editor for their needs.

Syntax:

Below is the syntax for the vi command:

vi filename

How does vi command work in Unix?

With the up-gradations of versions, there is also an advanced version of vi editors, which is VIM(Vi Improved).

Few reasons make VI editors mostly use the editor

  • This editor is default present in all Linux or Unix OS.
  • The most usage of vi editors is to create a new file, edit an existing one, or just read one.
  • The editor is user-friendly and flexible to use. So, users prefer using this editor for their needs.

Operating modes in vi editors

  • Command Mode: By default, command mode is on as soon as vi is started. This command mode helps users to copy, paste, delete, or move text. We should press the [Esc] key to go to command mode when in other modes.
  • Insert mode: Whenever we try to open vi editors, it will go to command mode by default. We must go to insert mode to write the contents in the file. Press ‘I’ to go to insert mode. If we want to go back to command mode, press the [Esc] key.
  • Last Line Mode or Escape Mode: when the vi editor is in command mode, press [Esc] and colon[:] to go to Escape mode. This mode helps users to save files, store the data in the file, and many more.

The syntax for vi commands:

  • The general syntax for the vi command is:
vi filename
  • To read an existing file, the syntax is as follows:
vi -R filename
  • To open an existing file, the syntax is as follows:
view filename

Steps to create a file and write contents in the file

Here are the following steps mentioned below

Step #1

Create a new file:

To create a new file, you can use the below syntax:

Syntax:

vi filename.txt

Command:

vi new_file.txt

vi command in unix output 1

Command Mode:

The below screenshot is taken when the vi editors were in command mode.

vi command in unix output 2

Step #2

Go to Insert Mode:

Press’ I’ from the keyboard to switch from command mode to insert mode. At the bottom of the editor, you can see’ INSERT’ written as shown below:

output 3

Step #3

Write the content:

Once the editor is in insert mode, you can start writing the content in the file.

output 4

Step #4

Save the file and exit from the editor:

Press the [Esc] key and the ‘:wq’ to save and exit the file.

Syntax:

[Esc] + :wq

output 5

Step #5

Check whether the data has been created successfully or not:

To view the content in the file, you can use the cat command in Unix.

Syntax:

cat filename.txt

Command:

cat new_file.txt

output 6

Vi Editing commands

Below are the editing commands used in vi editors to edit either the data or modify the content:

.i – Go to insert mode

  • a – To Write after the cursor
  • A – To Write the data at the end of the line
  • ESC – Closes insert mode and goes to command mode
  • u – Undo the changes
  • U – Undo all changes in the line
  • o – Start a new line
  • dd – Delete a complete line
  • 3dd – To Delete 3 lines consecutively.
  • D – To Delete the data of the line next to the cursor
  • dw – Delete a word in the file
  • 4dw – To Delete 4 words in the file
  • cw – To Change a word to the other
  • x – To Delete a character at the beginning of the cursor
  • r – To Replace a character in the file
  • R – To Overwrite characters present in the file
  • s – Substitute a char
  • ~ – To Change the case of an individual character

Process to save and close a file in vi editors

  • Shift + zz: To save a file and exit
  • Esc +:w – Save the file but do not exit.
  • Esc +:q – To quit from the file without saving
  • Esc +:wq – To save the file and exit from it.

Conclusion – VI Command in Unix

In Unix Shell Scripting, the VI editor is one of the most used and very useful text editors. It is the default editor that is installed in Unix OS. The most usage of vi editors is to create a new file, edit an existing one, or just read one. This editor is default present in all Linux or Unix OS. The most usage of vi editors is to create a new file, edit an existing one, or just read one. The editor is user-friendly and flexible to use. So, users prefer using this editor for their needs.

Recommended Articles

We hope that this EDUCBA information on “VI Command in Unix” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Array in Unix
  2. Crontab in Unix
  3. VI Editor in Unix
  4. Install UNIX
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW