Unit 3 VI Editor
Unit 3 VI Editor
Introduction:
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 vi editor
(short for "visual editor") is a text editor available on most Unix-based systems,
including Linux. It’s a modal editor, meaning it operates in different modes for
different types of tasks, such as inserting text or executing commands. It is
powerful yet lightweight, making it an essential tool for developers, system
administrators, and users working on a command-line interface (CLI). It is a very
powerful application. An improved version of vi editor is vim.
History of vi
vi was first released in 1976 as part of the Unix operating system by Bill Joy, one
of the co-founders of Sun Microsystems. Since its inception, it has become a
standard text editor in the Unix world due to its efficiency and the ubiquitous
presence in nearly all Unix-based systems, including Linux.
Use/Features of VI;
The vi editor is a versatile and powerful text editor widely used in Linux and Unix
systems for creating and modifying text files. It is especially useful for editing
configuration files, scripts, source code, and logs.
Editing System Configuration Files
Writing and Editing Scripts
Log File Viewing and Debugging
Remote File Editing on Servers
Coding and Development
Creating and Editing Documents
Quick Text Editing in Low-Resource Systems
Searching and Replacing Text
Version Control (Git) Integration
Collaborative Development on Unix/Linux Systems
1
Editing Large Files
Different modes and working with 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` = file1
Vi file1
Modes of Operation in the vi editor:
There are three modes of operation in vi:
2
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).
3
Basic Commands in vi
1. Switching to Insert Mode
To edit or insert text, press:
i – Enter insert mode at the current cursor position.
I – Insert at the beginning of the current line.
a – Insert after the cursor.
A – Insert at the end of the line.
o – Insert a new line below the current line.
O – Insert a new line above the current line.
4
3. Navigating in Normal Mode
h, j, k, l – Move left, down, up, and right.
0 – Move to the beginning of the line.
$ – Move to the end of the line.
G – Go to the end of the file.
gg – Go to the beginning of the file.
Ctrl + f – Scroll forward.
Ctrl + b – Scroll backward.
4. Deleting Text
x – Delete the character under the cursor.
dd – Delete the current line.
d$ – Delete from the cursor to the end of the line.
dw – Delete a word.
5
Exiting Modes
Press Esc to exit Insert Mode and go back to Normal Mode.
Searching
/word – Search for a word (press n to go to the next occurrence).
?word – Search backward for a word.
Example Workflow
Open a file: vi example.txt
Press i to enter Insert Mode and start typing.
Press Esc to return to Normal Mode when done.
Save with :w and quit with :q.
Searching for pattern ( / and ?) in vi editor:
In the vi editor, searching for patterns is a common task, and it can be done using
the forward search (/) and backward search (?) commands. These commands allow
you to quickly navigate through a file and find specific text patterns.
1. Forward Search (/)
To search for a pattern in the forward direction (from the current cursor position
towards the end of the file), use the / command.
Syntax:
/pattern
Steps:
In Normal Mode (press Esc to ensure you're in this mode), type /.
Enter the pattern (word or regular expression) you're searching for.
Press Enter.
The cursor will jump to the first occurrence of the pattern.
Example:
/hello
6
This will search for the first occurrence of the word "hello" from the current cursor
position towards the end of the file.
Navigating Through Search Results:
n: Move to the next occurrence of the pattern.
N: Move to the previous occurrence of the pattern.
2. Backward Search (?)
To search for a pattern in the backward direction (from the current cursor position
towards the beginning of the file), use the ? command.
Syntax:
?pattern
Steps:
In Normal Mode, type ?.
Enter the pattern you're searching for.
Press Enter.
The cursor will jump to the first occurrence of the pattern before the current cursor
position.
Example:
?error
This will search for the first occurrence of the word "error" from the current cursor
position towards the beginning of the file.
Navigating Through Search Results:
n: Move to the next occurrence of the pattern in the same direction as the search
(backward in this case).
N: Move to the previous occurrence of the pattern.