Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App

vi Editor in Linux

Last Updated : 13 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The default editor that comes with the Linux/UNIX operating system is called vi (visual editor). Using vi editor, we can edit an existing file or create a new file from scratch. we can also use this editor to just read a text file. The advanced version of the vi editor is the vim editor. 

Table of Content

  • How to Open VI Editor
  • Modes of Operation in the vi editor
  • Linux vi Commands and Examples
  • Moving within a File (Navigation) in Vi Editor  
  • Control Command (Scrolling) in vi Editor
  • Inserting and Replacing text in Vi Editor
  • Deleting Characters and Lines in Vi Editor
  • Copy and Paste in Vi editor in Linux
  • Save and Exit in Vi Editor in Linux
  • Searching and replacing in (ex-Mode) in Vi Editor (Linux)
  • Block delete commands in (x mode) in Vi Editor  
  • Block copy command in (x mode) in Vi Editor  
  • Block moving commands in (x mode) in Vi Editor  

How to Open VI Editor

To open vi editors, we just need to type the command mentioned below.

vi [file_name]


Here, [file_name] = this is the file name we want to create or to open the pre-existing file.

Example 1: Creating a new file with `file_name` =  geeksforgeeks

vi geeksforgeeks 

to create file we used vi geeksforgeeks
to create file we used vi geeksforgeeks 

As we can see, we have created a new file geeksforgeeks in vi editor, which has no content in it.

Example 2: Opening a preexisted file with `file_name` =  jayesh

vi jayesh


Opened already existed file with vi jayesh
Opened already existed file with vi jayesh

As we can see, we have opened a file_name = Jayesh, that already existed in vi editor.

Modes of Operation in the vi editor

There are three modes of operation in vi: 

Here are three modes of operations on the vi editor
Here are three modes of operations on the vi editor 

Vi Command Mode :

When vi starts up, it is in Command Mode. This mode is where vi interprets any characters we type as commands and thus does not display them in the window. This mode allows us to move through a file, and delete, copy, or paste a piece of text. Enter into Command Mode from any other mode, requires pressing the [Esc] key. If we press [Esc] when we are already in Command Mode, then vi will beep or flash the screen.

Vi Insert mode: 

This mode enables you to insert text into the file. Everything that's typed in this mode is interpreted as input and finally, it is put in the file. The vi always starts in command mode. To enter text, you must be in insert mode. To come in insert mode, you simply type i. To get out of insert mode, press the Esc key, which will put you back into command mode.

Vi Last Line Mode (Escape Mode): 

Line Mode is invoked by typing a colon [:], while vi is in Command Mode. The cursor will jump to the last line of the screen and vi will wait for a command. This mode enables you to perform tasks such as saving files and executing commands.

Linux vi Commands and Examples

NOTE: vi editor in Linux is a case-sensitive.

How to insert in vi editor in Linux :

To enter in insert mode in vi editor in Linux we just need to press `i` on our keyboard and we will be in insert mode. we can just start entering our content. (Refer to screenshot mentioned below).

opened a file and pressed `i` to write content
opened a file and pressed `i` to write content

Moving within a File (Navigation) in Vi Editor : 

To move around within a file without affecting text must be in command mode (press Esc twice). Here are some of the commands that can be used to move around one character at a time.

CommandsDescription
`k` Moves the cursor up one line.
`j`Moves the cursor down one line.
`h`  Moves the cursor to the left one-character position.
`l`Moves the cursor to the right one-character position.
`0`Positions cursor at beginning of line.
`$`Positions cursor at end of line.
`W`Positions cursor to the next word.
`B` Positions cursor to previous work.
`(`Positions cursor to beginning of current sentence.
`)`Positions cursor to beginning of next sentence.
`H`Move to top of screen.
`nH`Moves to nth line from the top of the screen.
`M`Move to middle of screen.
`L`Move to bottom of screen.
`nL`Moves to nth line from the bottom of the screen.
Colon followed by a number positionThe cursor on the line number is represented by the number after the colon. For example, ":10" positions the cursor on line 10.

Control Command (Scrolling) in vi Editor :

 There are the following useful commands which can be used along with the Control Key. These commands are helpful in saving time by navigating quickly in a file without manually scrolling.

CommandDescription
CTRL+dmoves the screen down by half a page.
CTRL+fmoves the screen down by a full page.
CTRL+umoves the screen up by half a page.
CTRL+bmoves the screen up by a full page.
CTRL+emoves the screen up by one line.
CTRL+ymoves the screen down by one line.
CTRL+Iredraw the screen.

Inserting and Replacing text in Vi Editor :

To edit the file, we need to be in the insert mode. There are many ways to enter insert mode from the command mode.

Command Description
iInserts text before current cursor location
aInsert text after current cursor location 
AInsert text at the end of current line
oCreates a new line for text entry below cursor location and switches to insert mode.
OCreates a new line for text entry above cursor location and switches to insert mode.
sReplaces single character under the cursor with any number of characters and switches to insert mode.
ROverwrites text from the cursor to the right, without switching to insert mode.

Deleting Characters and Lines in Vi Editor :

Here is the list of important commands which can be used to delete characters and lines in an opened file.

CommandDescription
`X` (Uppercase)Deletes the character before the cursor location.
`x` (Lowercase)Deletes the character at the cursor location.
`Dw`Deletes from the current cursor location to the next word
`d^`Deletes from current cursor position to the beginning of the line.
`d$`Deletes from current cursor position to the end of the line.
`Dd`Deletes the line the cursor is on.

Copy and Paste in Vi editor in Linux:

Copy lines or words from one place and paste them in another place by using the following commands.

CommandsDescription
YyCopies the current line.
9yyYank current line and 9 lines below.
pPuts the copied text after the cursor.
P Put the yanked text before the cursor.

Save and Exit in Vi Editor in Linux:

Need to press [Esc] key followed by the colon (:) before typing the following commands:

Commands Description
qQuit
q!Quit without saving changes i.e. discard changes.
r [file_name]Read data from file called [file_name]
wqWrite and quit (save and exit).
wWrite to file called [file_name] (save as).
w!Overwrite to file called [file_name] (save as forcefully).
!cmdRuns shell commands and returns to Command mode.

Searching and replacing in (ex-Mode) in Vi Editor (Linux):

vi also has powerful search and replacement capabilities. The formal syntax for searching is:

:s/string 


For example, suppose we want to search some text for the string "geeksforgeeks" Type the following and press ENTER:

:s/geeksforgeeks


Input: 

:s/string
:s/string 

Output: finding the first match for "geeksforgeeks" in text will then be highlighted. 

"geeksforgeeks" in text will then be highlighted.
"geeksforgeeks" in text will then be highlighted. 

 The syntax for replacing one string with another string in the current line is:

:s/pattern/replace/ 


Here "pattern" represents the old string and "replace" represents the new string. For example, to replace each occurrence of the word "geeks" in a line with "geeksforgeeks" type:

:s/geeksforgeeks/gfg/ 


Input:

:s/geeksforgeeks/gfg/
:s/geeksforgeeks/gfg/ 

Output:

Output:
Output:

The syntax for replacing every occurrence of a string in the entire text is similar. The only difference is the addition of a "%" in front of the "s":

:%s/pattern/replace/ 


Thus repeating the previous example for the entire text instead of just for a single line would be:

:%s/gfg/geeksforgeeks/ 


Block delete commands in (x mode) in Vi Editor : 

need to press ESC and then commands will be followed by colon (:).

CommandDescription
:1ddelete the line 1.
 :1,5ddeletes the lines from 1 to 5.
:10,$d

deletes lines from 10th line to the last of the file. 

 ($ means last line of file).

 :.,$d deletes lines from present line to that last line. (. means the present line).
:.-3,.d

deletes the lines from present line and above 2 lines 

 (Deletes 3 lines including the cursor line).

:.,.+4ddeletes the lines from present cursor line followed 3 lines (total 3 lines).
:16 deletes the 16 line of the file.

Block copy command in (x mode) in Vi Editor : 

 need to press ESC and then commands will be followed by colon (:).

CommandDescription
:1,5 co 10copies the lines from 1 to 5 after the 10th line.
:1,$ co $copies the lines from 1 to last line after last line.
:.,.+5 co 8copies lines from present to 5 lines after 8th line.
 :-3,. co 10copies the lines from present cursor line above 3 lines after 10th line.

Block moving commands in (x mode) in Vi Editor : 

need to press ESC and then commands will be followed by colon (:).

CommandDescription
:1,5 mo 9moves line from 1 to 5 after 9th line.
:1,$ mo $moves lines from 1 to $ after last line.
:.,.+5 mo 10moves line from present line ans next 5 lines after 10th line onwards.
:.-3,. mo 10 moves present line and above 3 lines after 10th line.

Conclusion 

Vi editor is a powerful and widely used text editor in UNIX and Linux operating system. It allows us to create, edit and manage text files. Vim is the advanced version of vi editor. There are three modes in vi: Command mode, Last Line Mode and Insert Mode. We have also discussed many options in the above context. Overall, we can say that it is a powerful tool and it is useful for both beginners and experienced users.


S

saloni1297
Improve
Article Tags :
  • Misc
  • Linux-Unix
  • linux-command
  • Linux-text-processing-commands
Practice Tags :
  • Misc

Similar Reads

    unexpand command in Linux with Examples
    To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. The unexpand command by default converts each space into tabs writing the produced output to the standard output. Syntax $unexpand [OPTION]... [FILE]...where, OPTION: Represents the various
    3 min read
    uniq Command in Linux with Examples
    The uniq command in Linux is a command-line utility that reports or filters out the repeated lines in a file. In simple words, uniq is the tool that helps to detect the adjacent duplicate lines and also deletes the duplicate lines. uniq filters out the adjacent matching lines from the input file(tha
    7 min read
    until command in Linux with Examples
    until command in Linux is used to execute a set of commands as long as the final command in the 'until' Commands has an exit status that is not zero. It is mostly used where the user needs to execute a set of commands until a condition is true. Syntaxuntil COMMANDS; do COMMANDS; donewhere,CONDITION:
    2 min read
    Linux | Uptime command with examples
    It is used to find out how long the system is active (running). This command returns set of values that involve, the current time, and the amount of time system is in running state, number of users currently logged into, and the load time for the past 1, 5 and 15 minutes respectively. Linux uptime S
    5 min read
    How to add User in Linux | useradd Command
    useradd is a command in Linux that is used to add user accounts to your system. It is just a symbolic link to adduser command in Linux and the difference between both of them is that useradd is a native binary compiled with the system whereas adduser is a Perl script that uses useradd binary in the
    5 min read
    usermod command in Linux with Examples
    usermod command or modify user is a command in Linux that is used to change the properties of a user in Linux through the command line. After creating a user we have to sometimes change their attributes like password or login directory etc. so in order to do that we use the Usermod command. The info
    4 min read
    username Command in Linux With Examples
    Linux as an operating system holds the capabilities of handling multiple users each with a username and a display name (Full Name). So it is important to keep a check on the users and their related information in order to maintain the integrity and security of the system. Whenever a user is added it
    4 min read
    users command in Linux with Examples
    users command in Linux system is used to show the user names of users currently logged in to the current host. It will display who is currently logged in according to FILE. If the FILE is not specified, use "/var/run/utmp". "/var/log/wtmp" as FILE is common. Syntaxusers [OPTION]... [FILE]where,OPTIO
    2 min read
    How to Delete User in Linux | userdel Command
    Managing user accounts is an essential aspect of Linux system administration. Understanding how to delete a user in Linux is crucial, whether you need to remove an unused account, revoke access for a departing employee, or clean up your system for security reasons. Here, we will explore the 'userdel
    5 min read
    vi Editor in Linux
    The default editor that comes with the Linux/UNIX operating system is called vi (visual editor). Using vi editor, we can edit an existing file or create a new file from scratch. we can also use this editor to just read a text file. The advanced version of the vi editor is the vim editor.  Table of C
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences