vi Editor in Linux - GeeksforGeeks (2024)

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

vi Editor in Linux - GeeksforGeeks (1)

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

vi Editor in Linux - GeeksforGeeks (2)

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:

vi Editor in Linux - GeeksforGeeks (3)

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).

vi Editor in Linux - GeeksforGeeks (4)

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.

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.

CommandDescription
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.
PPut 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:

CommandsDescription
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:

vi Editor in Linux - GeeksforGeeks (5)

:s/string

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

vi Editor in Linux - GeeksforGeeks (6)

“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:

vi Editor in Linux - GeeksforGeeks (7)

:s/geeksforgeeks/gfg/

Output:

vi Editor in Linux - GeeksforGeeks (8)

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).

:.,$ddeletes 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).
:16deletes 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 10moves 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.



Like Article

Suggest improvement

Previous

w command in Linux with Examples

Next

watch command in Linux with Examples

Share your thoughts in the comments

Please Login to comment...

vi Editor in Linux - GeeksforGeeks (2024)

FAQs

Is vi editor useful? ›

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.

How do you go to the 100th line in vi editor? ›

If you're already in vi, you can use the goto command. To do this, press Esc , type the line number, and then press Shift-g . If you press Esc and then Shift-g without specifying a line number, it will take you to the last line in the file.

How do I select all content in vi editor Linux? ›

To pick all of a file's content from beginning to end, you should use the select all command.
  1. To select all in Vim, use ggVG.
  2. Use 99999yy to select and copy everything.
  3. To select and copy all, use $yy.
Dec 4, 2023

How do I get the number of lines in vi editor? ›

Displaying Line Numbers in Vi

Enter Command Mode: By default, Vi starts in “Normal” mode. To enter “Command” mode, press Esc . Enable Line Numbers: In “Command” mode, type :set number and press Enter . This will display line numbers along the left-hand side of the editor.

Why vi is better than Vim? ›

For simple text editing tasks, both vi and vim will behave similarly. However, vi is preferred because we can expect it to be on POSIX compliant (or at least mostly compliant) systems. Moreover, regarding the performance, vim requires a little more resources than vi due to its more extensive features.

Is Vim better than vi? ›

Vim, short for “Vi IMproved,” is an enhanced, improved, and extended version of the Vi text editor. Developed by Bram Moolenaar in the early 1990s, Vim builds upon the foundation of Vi while adding numerous features and improvements. It is a more feature-rich and upgraded version of the Vi editor.

How do you finish editing in vi? ›

Press Esc to enter Command mode, and then type :wq to write and quit the file. The other, quicker option is to use the keyboard shortcut ZZ to write and quit. In Vi, write means save, and quit means exit.

How do you delete 1000 lines in vi? ›

To delete multiple lines Press Esc to leave the insert/editing mode, enter the number of lines you want to delete followed by 'dd' i.e. ndd and the editor will delete the mentioned number of lines from the current line.

What is the difference between vi and Vim? ›

In casual conversation, Vi and Vim are interchangeable and usually refer to Vim (Vi Improved). On some POSIX systems, the vi command is a pointer to Vim (or else Vim is just called Vi). However, some systems ship just with Vi and you have to install Vim separately.

How many modes are available in vi editor? ›

Two modes of operation in vi are entry mode and command mode. You use entry mode to type text into a file, while command mode is used to type commands that perform specific vi functions. Command mode is the default mode for vi .

How many modes are there in vi editor in Linux? ›

Two modes of operation in vi are entry mode and command mode. You use entry mode to type text into a file, while command mode is used to type commands that perform specific vi functions. Command mode is the default mode for vi .

How to copy entire text in vi editor? ›

Press y to copy. Press d to cut. Move the cursor to where you want to paste your selection. Press P (uppercase) to paste before your cursor.

How do I edit multiple lines in vi? ›

Edit multiple lines in vi editor

Press Ctrl+v and select the lines you want to edit you can use arrow keys or k,j to move upward and downward respectively.

How do I find patterns in vi editor? ›

To find a character string, type / followed by the string you want to search for, and then press Return. vi positions the cursor at the next occurrence of the string. For example, to find the string “meta,” type /meta followed by Return.

What is the command to select all lines in vi editor? ›

Use Ctrl + A to perform a selection across all editable areas. Pressing Ctrl+A will select everything in the current view, including menus, toolbars, files, and other content. This is very helpful if you want to pick just about anything! You can also use this keystroke to make sure that nothing is selected.

Why is Vim editor so popular? ›

We have learned that Vim is a powerful text editor popular among developers. It's based on shortcuts, called the Vim language, which can make coding and writing faster and more efficient. With Vim, you can jump to any specific text position and rapidly make precise edits.

What is the use of vi text editor? ›

To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.

Top Articles
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 6348

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.