EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Unix Tutorial VI Editor in Unix
Secondary Sidebar
Unix Tutorial
  • Advanced
    • Array in Unix
    • Functions in Unix
    • Unix File Permissions
    • Unix File System
    • Unix Shell Commands
    • Crontab in Unix
    • egrep command in Unix
    • For Loop in Unix
    • VI Editor in Unix
    • What is Unix Shell
    • Gnome Development
    • UNIX interview questions
    • Cheat sheet for UNIX
    • Unix Sort by Column
    • Find Command in Unix
    • Gnome Version
  • Basic
    • What is Unix
    • Uses of Unix
    • Career In Unix
    • Install UNIX
    • Unix Architecture
    • Unix Commands
    • VI Command in Unix
    • AWK Command in Unix
    • Paste Command in Unix
    • SED Command in Unix
    • Sort Command in Unix
    • WHO Command in Unix
    • Unix Operators
    • UNIX Administrator

VI Editor in Unix

By Priya PedamkarPriya Pedamkar

VI Editor in Unix

Introduction to VI Editor in Unix

VI Editor is a kind of text editor that is available in the UNIX operating system, and it is basically the default editor that comes with the UNIX operating system, which is used to create a new file or editing an existing file, and offering rich user-friendly features, which are available in almost all the UNIX distribution systems. The capabilities incorporated into the tool which opens the door to the endless possibilities used to edit a file, renders the tool a unique position among all the editors, including pico, nano, vile, in UNIX, is termed as VI Editor in the UNIX operating system.

Modes of VI Editor in Unix

To have an easy working experience with the VI editor we need to have some understanding about different modes of operations of the VI editor.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

They are divided into three main parts:

  1. Command Mode
  2. Insert Mode
  3. Escape Mode

1. Command Mode

Command Mode is the first screen of VI editor. It is case sensitive. Any character that is typed during this mode is treated as a command. These are character are not visible on the window. We can cut, copy, paste or delete a piece of text or even move through the file in this mode

[ESC] used to enter the Command Mode from another mode (Insert Mode)

2. Insert Mode

We can easily move from Command mode à Insert mode by pressing ‘i’ or ‘Insert’ key from the keyboard. Characters typed in this mode is treated as input and add text to your file

Pressing ESC will take you from Insert Mode -> Command Mode

3. Escape Mode

Press [:] to move to the escape mode. This mode is used to save the files & execution of the commands

 Escape Mode

Fig : Blue Box Represents the various modes on VI editor

Green Box Represents the keys/commands to move from one mode to another

Syntax of VI Editor in Unix

VI Editor has various features for easy editing in the Linux environment. The basic purpose of highlighting these commands and their syntax is just to make oneself familiar with the availability of various features. We do not need to mug up all the commands. You can refer to the main pages for the details of the commands and the options.

Now let us get going on the same:

1. Open/ Create a File

This will create a file with the name ‘filename’ or open the file with the name ‘filename’ if already exists.

Vi Filename

Output:

VI Editor

Note: all the line starts with a tilde (~) sign which represents the unused lines

2. Read-Only Mode

To open the file in read-only mode use:

 vi R

Output: At the bottom of the file you will see ‘Readonly’

 vi editor in unix

3. Moving out of a file

:q Quit out of a file
:q! Quit the file without saving the changes
:w Save the content of the editor
:wq Save the changes and quit the editor (*Combing the commands: q &: w)
ZZ In command mode, this works similar to wq

4. Rename a File

:w newFileName – This will rename the file that you are currently working into ‘new filename’. A command is used in Escape Mode.

5. Move within a file

To move around in a file without actually editing the content of a file we must be in the Command mode and keep the below commands handy.

h Moves the cursor left one character position
l Moves the cursor right one character position
k Moves the cursor one line up
j Moves the cursor one line down

**Arrows can help you remember the functionality of that key. It has no other significance.

Move within a file

Keyboard keys for movements within the editor.

Note: Number at the beginning is equal to the number of times you want the action to occur

Example: 2j will move the cursor two lines down from the current cursor location\

6. Inserting or Adding Text

Following is the command used to put the editor in the insert mode.

Once the ESC is pressed it will take the editor back to command mode.

i Insert text before the cursor
I Insert at the beginning of the current line
a Append after the cursor
A Append at the end of the current line
o Open & places the text in a new line below the current line
O Open & places the text in a new line above the current line

7. Searching the Text

Similar to the find & replace command in windows editor we have certain Search & replace command available in the VI editor as well.

/string Search the mentioned ‘String’ in the forward direction
?string Search the mentioned ‘String’ in the backward direction
n Move to the next available position of the searched string
N Move to the next available position of the searched string in the opposite direction

8. Determining the Line Number

Having the line number is very useful sometimes while editing the file. These commands are used in Escape Mode that is after pressing the [:] key

:.= Line Number of the current line
:= Gives the total number of lines
^g Gives line number along with the total number of lines in a file at the bottom of the screen

9. Cutting & Pasting Text

These commands allow you to copy and paste the text

yy Copy (yank, cut) the current line into the buffer
Nyy or yNy Copy ‘N’ lines along with the current line into the buffer
p Paste / Put the lines in the buffer into the text after the current line

Conclusion

Due to the availability of the VI editor in all the Linux environment, learning VI editor can be really useful. It can help us in creating and editing the scripts. We must be familiar with the commands along with the particular mode in which that command is to be used. This is not the end of the options available in VI editor keep exploring as the challenge comes to your way.

Recommended Articles

This is a guide to the VI Editor in Unix. Here we discuss the basic concept, Modes of VI Editor in Unix and the Syntax of VI Editor in Unix. You can also go through our other suggested articles to learn more–

  1. Uses of Unix
  2. Functions in Unix
  3. Array in Unix
  4. Crontab in Unix
Popular Course in this category
Kali Linux Training (3 Courses, 3+ Projects)
  3 Online Courses |  3 Hands-on Projects |  21+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Linux Training Program (16 Courses, 3+ Projects)4.9
Red Hat Linux Training Program (4 Courses, 1+ Projects)4.8
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

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

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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 Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more