Javatpoint Logo



Javatpoint Logo

Vi editor

What is vi

The vi editor is elaborated as visual editor. It is installed in every Unix system. It is a very powerful application. An improved version of vi editor is vim but most Linux systems have vi editor installed.

vi editor has two modes:

  • Command Mode: In command mode, actions are taken on the file. The vi editor starts in command mode. Typed words will act as commands in vi editor. To pass a command you have to be in command mode.
  • Insert Mode: In insert mode, entered text will be inserted into the file. Esc key will take you to the command mode from insert mode.

By default, vi editor starts in command mode. To enter text, you have to be in insert mode, just type 'i' and you'll be in insert mode. Although, after typing i nothing will appear on the screen but you'll be in insert mode. Now you can type anything.

To exit from insert mode press Esc key, you'll be directed to command mode.

If you are not sure which mode you are in, press Esc key twice and you'll be in command mode.

Using vi

The vi editor tool is an interactive tool as it displays changes made in the file on the screen while you edit the file.

In vi editor you can insert, edit or remove a word as cursor moves throughout the file.

Commands are specified for each function like to delete it's x or dd.

The vi editor is case-sensitive. For example, p allows you to paste after the current line while P allows you to paste before the current line.

vi syntax:

In the terminal when you'll type vi command with a file name, the terminal will get clear and content of the file will be displayed. If there is no such file, then a new file will be created and once completed file will be saved with the mentioned file name.

Linux vi example

Let's understand vi through an example:

To start vi open your terminal and type vi command followed by file name. If your file is in some other directory, you can specify the file path. And if in case, your file doesn't exist, it will create a new file with the specified name at the given location.


Linux Vi Editor1

Look at the above snapshot, we are creating a new file file.txt (as this file doesn't exist) and have entered the full path for the directory Downloads.

Command mode

This is what you'll see when you'll press enter after the above command. If you'll start typing, nothing will appear as you are in command mode. By default vi opens in command mode.

Linux Vi Editor1

Look at the above snapshot, it is blank as it is a new file. To start typing, you have to move to the insert mode. At the end of the terminal window, directory name and file name are displayed.

Insert mode

To move to the insert mode press i. Although, there are other commands also to move to insert mode which we'll study in next page.

Linux Vi Editor3

Look at the above snapshot, after pressing i we have entered into insert mode. Now we can write anything. To move to the next line press enter.

Once you have done with your typing, press esc key to return to the command mode.

To save and quit

You can save and quit vi editor from command mode. Before writing save or quit command you have to press colon (:). Colon allows you to give instructions to vi.

exit vi table:

Commands Action
:wq Save and quit
:w Save
:q Quit
:w fname Save as fname
ZZ Save and quit
:q! Quit discarding changes made
:w! Save (and write to non-writable file)

To exit from vi, first ensure that you are in command mode. Now, type :wq and press enter. It will save and quit vi.

Type :wq to save and exit the file.

Linux Vi Editor4

Look at the above snapshot, command :wq will save and quit the vi editor. When you'll type it in command mode, it will automatically come at bottom left corner.

If you want to quit without saving the file, use :q. This command will only work when you have not made any changes in the file.

Linux Vi Editor5

Look at the above snapshot, this file is modified and hence on typing :q it displays this message at bottom left corner.

The above file can be saved with the command :!q. It discards the changes made in the file and save it.

Linux Vi Editor6

Look at the above snapshot, we have typed :!q, it will save our file by discarding the changes made.

Next TopicVi Commands

Latest 4 Tutorials on JavaTpoint