What is Vi Editor?
Vi editor is also known as Visual Editor. It is widely installed in most of the linux systems. Since it is a Terminal based editor, it is most preferred by the linux users those who are working in command line mode. Vim Editor is the advanced version of Vi editor. Every Linux users must know, how to edit files using vi editor.
Using Vi
There are three modes Vi Editor has.
- Command Mode: Command mode is used to get commands to perform the desired actions.
- Insert Mode: Insert mode is used to perform the file modifications and edit process. Again to switch back to Command mode press Esc key
- Replace Mode: This is similar to Insert mode. But there is a difference. While perform file modification it will be delete the next characters and replace with newly typing text.
There are many commands available to use. We will use few major commands that are important to know. We will see those in the following example.
In this example we will explain how to open a file , do modifications and save a file.
1. Open the Terminal
If you are logged-in as GUI mode in linux system, open the Terminal or Konsole or any other Command line applications. If you already logged-in as Command Mode, then ignore this step.
2. Open the File or Create a new File
You can open the existing file or you can create the new file using the following command.
vi /tmp/new-file.txt
Or
vi /tmp/existing-file.txt
3. Switch to Insert Mode
Once you opened new file the editor will be in Command Mode.
Type the Insert key or i key to entering into insert command.
You will see this text in the last line of the editor.
— INSERT —
4. Complete the file modification
Do the necessary file modifications while you are in Insert Mode.
5. Exit the Insert Mode
Once you done with the file modifications you have to save/write the file. First you have to exit from the Insert Mode
Type Esc Key to exit from the Insert Mode
Now you will see the blank in the last line of the editor. Now the editor is ready to accept the new command.
5. Write the File
Type :w and press Enter key to save/write the file.
i.e. Press Colon + w and Enter key
Now the file has been saved to disk. You can exit from the Vi Editor
6. Quit the Vi Editor
Type :q and press Enter key to quit from the Vi Editor.
i.e. Press Colon + q and Enter key
Now file must be saved successfully.
7. Write and Quit the Vi
As a shortcut you can run the Write and Quit command as combined.
Type :wq and press Enter key to write and quit from the Vi Editor
That’s it. You are done with the file saving.
These are the basic commands need to learn by every linux users.
There are some other advanced Vi Editor Commands which I will share in another post.