Vi Editor In UNIX - GeeksforGeeks

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.

Commands Description
`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 position The 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.

Command Description
CTRL+d moves the screen down by half a page.
CTRL+f moves the screen down by a full page.
CTRL+u moves the screen up by half a page.
CTRL+b moves the screen up by a full page.
CTRL+e moves the screen up by one line.
CTRL+y moves the screen down by one line.
CTRL+I redraw 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
i Inserts text before current cursor location
a Insert text after current cursor location 
A Insert text at the end of current line
o Creates a new line for text entry below cursor location and switches to insert mode.
O Creates a new line for text entry above cursor location and switches to insert mode.
s Replaces single character under the cursor with any number of characters and switches to insert mode.
R Overwrites 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.

Command Description
`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.

Commands Description
Yy Copies the current line.
9yy Yank current line and 9 lines below.
p Puts 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
q Quit
q! Quit without saving changes i.e. discard changes.
r [file_name] Read data from file called [file_name]
wq Write and quit (save and exit).
w Write to file called [file_name] (save as).
w! Overwrite to file called [file_name] (save as forcefully).
!cmd Runs 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 (:).

Command Description
:1d delete the line 1.
 :1,5d deletes 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).

:.,.+4d deletes 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 (:).

Command Description
:1,5 co 10 copies 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 8 copies lines from present to 5 lines after 8th line.
 :-3,. co 10 copies 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 (:).

Command Description
:1,5 mo 9 moves line from 1 to 5 after 9th line.
:1,$ mo $ moves lines from 1 to $ after last line.
:.,.+5 mo 10 moves 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.

Basic vi Command and Working – FAQs

What is the difference between vi and vim editor?

Vi is the original text editor that was created in 1976, while Vim (Vi Improved) is an improved version of Vi that was released in 1991. Vim offers additional features and functionality compared to Vi, such as syntax highlighting and mouse support.

How do you search for a word in vi editor?

To search for a word in vi editor, you need to first ensure that you are in command mode. Then

  • type the command ‘/searchterm’ and press enter.
  •  This will search for the first occurrence of ‘searchterm’ in the document. 
  • To find the next occurrence of the word, type the command ‘n’. 

How can you delete a line in vi editor?

Go to command mode (press ESC) then press `dd`. This will delete the complete line in which our cursor is there.

What is the purpose of the command mode in vi editor?

The command mode in vi editor performs commands and navigate through our document or content in file.  In this mode, you can move the cursor, delete text, search for text, and save the document.

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 Follow Improve Previous Article w command in Linux with Examples Next Article watch command in Linux with Examples

Từ khóa » Vi In Linux Meaning