Vi is text editor originally created for the Unix operating system. . This blog post explains how to save a file in vi and quit the editor under Linux or Unix-like systems.
How to open a file in Vi
You can start vi by typing the following command:
$ vi <filename>
VI modes: insert mode and normal mode
When we start vi text editor, we are in normal mode. This mode allows us to use vi commands and navigate around the file as per our needs.
In order to edit text, you need to enter the insert mode by pressing i key. This way, we can edit our program or configuration files such as /etc/nginx/nginx.conf.
Switching between normal mode and insertmode can be achived by pressing Esc and i keys.
How to save and quit the vi text editor
- If you are currently in insert or append mode, press Esc key.
- Press : (Colon)The cursor should reappear at the lower left corner of the screen beside a colon prompt.
- Enter the following command (type : x or :x! or :wq or :wq! and press Enter key):
- Press Enter key
- This will quit the editor, and all changes you have made to the document will be saved to the file.
How to trash all changes in vi
If you do not want to save any changes, first press Esc key. To exit Vi without saving changes press :q! followed by ENTER key.
:w! is save without exiting
:q! is exit without saving
:wq is “save and exit”
Stop using :wq! to save and exit! :wq is save and quit, the ! tells it to ignore errors and exit. This could potentially leave you with exiting a file unsaved due to an error.