Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

How to Save a File in Vim / Vi and Quit the Editor?

Vim is an editor commonly used in Linux for editing. It is one of the most popular editors that can be used for text editing and coding as well.

Vim is also known as Vi and preinstalled in all Linux distributions and macOS. In this article, we will learn to save and file in Vim editor with some examples.

The key combination to save and quit a file in vim editor. First press Esc and type the following:

:wq

After that press Enter. This is a single-line solution for your questions. Let's get dive into understanding the whole process.

To begin with, first, open the terminal by pressing Ctrl+Alt+t and a terminal will open in your system. Now, see the step-by-step process.

1. Open File with Vim Editor

To open a file in Vim editor, write vim fileName command in the terminal and press enter. It will open a file that you have specified in the command. If the file does not exist then it will first create a file with the same name and then open the file. See the command.

$ vim fileName

The filename can be any, text, an HTML, or a doc file. So, we must specify the file with extension like file.txt, file.doc, etc.

2. Write into Vim Editor

After opening the file, we can modify the file by inserting text(writing).

To write into the file, press i that convert the vim editor to writing mode, and here you can write to the file. You can see the below screenshot where we insert a line and the editor mode can be seen at the bottom left, it is insert now.

3. Save into Vim Editor

After writing/inserting text, we can save the file by pressing the following keys:

  1. Esc

  2. :(colon)

  3. w

  4. Enter

First press Esc, then :(colon) and w, and last press Enter. It will save your file data into the vi editor. See the screenshot for help.

save vim

4. Save and Quit Vim Editor

If you want to quit the vim editor after saving the file, you can use this key combination:

  1. Esc

  2. :(colon)

  3. w

  4. q

  5. Enter

First press Esc, then : (colon), w and q, and last press Enter. It will save your file and quit the vim editor. See the below screenshot for help.

save

5. Quit Without Save Vim Editor

If we want to quit vim without saving the file then you can use the following key combination:

  1. Esc

  2. :(colon)

  3. q

  4. !

  5. Enter

First press Esc, then : (colon), q, and ! and last press Enter. It will quit the vim editor without saving your file. See the below screenshot for help.

Apart from saving and quitting the vim editor, you can do a lot more with this powerful editor. You can read our exiting article,

Conclusion

In this article, we focused to learn how to save and file and quite the vim editor after saving. We learned from scratch, to open vim editor, write text, save text, and quit after saving or without saving as well.