How to Close Vi – Vim / Vi Save and Exit Command?

Vim” is the most popular text editor, also known as “Vi Improved”. It refers to the user-friendly editor beneficial for creating and editing configuration files, text files, scripts, and much more in Linux. Its usage is simple and easy to use, with many functionalities. Although most people are not familiar with it.

This post enlists commands to close, save and exit the “Vi-Vim/Vi” text editor through command with the following highlights:

Prerequisites: Open a File in Vi – Vim / Vi Editor

Before moving on to the practical implementation, have a look into the “Sample.txt” file opened in the Vi-Vim/Vi text editor having the following content:

$ vim Sample.txt

The vim text editor is in the “INSERT” mode shown in the left bottom. This mode allows the user to edit the text files in the vim editor and can be accessed by pressing the  “i” tab.

Only Save a File in the Vi/Vim Editor

To simply save the file in the  Vi – Vim / Vi text editor, move back from “INSERT” to “normal” mode by just pressing the “Esc” key. Type the “:(colon)” with “w” from the keyboard:

:w

Press the “Enter Key”, and the above “Sample.txt” file will be saved into the current working Linux System.

The message shown in the left bottom corner verifies that the “Sample.txt[New] file with “5L(Lines)” having size “144B(bytes)” has been written, i.e., saved.

Tip: Save a File with a Different File Name 

If the user wants to save the existing file with a different filename, then type the new file name along with the “:w” command:

:w New_File

After hitting the “Enter” key following message will be shown:

The output confirms that the “File1.txt” file has been saved and renamed “New_File”.

Save and Exit Vi/Vim

To save the file and exit the text editor simultaneously, switch vim from “INSERT” to “normal” mode. Type the “:w” with the “q(quit)” keyword combined in this way:

:wq

Hit the “Enter” key from the keyboard:

The vim editor has been exited, and the output of the “ls -l” list command shows that the “Sample.txt” file has been saved in the current working directory.

Exit the Vi/Vim Editor Without Saving

If you want to save the file without exiting/closing the vim editor, switch from “INSERT” to “normal” mode. Write the “:q!” command in the editor and press the “Enter” key:

:q!

Once the “Enter” key is pressed, the “vim” editor will exit. Execute the “cat” command to see the new changes in the “Sample.txt” file:

$ cat Sample.txt

The output shows that vim has been exited, and it did not save the new changes in the “Sample.txt” file.

Close the Vi/Vim Editor

Close the vim editor simply by typing the “:q” without an “!(exclamation mark)”  and pressing the “Enter” key:

:q

It will definitely close the vim editor without saving any changes made in the file.

Conclusion

To close the Vi – Vim / Vi editor in Linux, enter the “:q”; to save the file, type the “:w” and exit the editor without saving using the “:q!” commands. Before doing this, first, switch the editor into the “normal” mode using the “Esc” key via the keyboard. This post has illustrated all possible commands to close, save, and exit the Vi – Vim / Vi editor in Linux.