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

The text editors are used to create, edit and save text files. Different text editors can be used in Linux, like nano and Vim text editors. Unlike other text editors, Vim is advanced and a little bit complex to use. There are two different modes in a vim text editor that are insert mode and command mode. To close the vim editor by saving the file, different approaches can be used.

Without knowing how to save the data in the vim editor, all your efforts can be wasted as you could lose it all. This blog explains the methods of quitting the vim text editor with and without saving the file. This guide will cover the following sections:

Let’s start!

Saving and Quitting a File in Vim Editor

The Vim text editor allows you to save the changes and exit the editor using the command line utility. In this method, the changes will be saved in the local file, and the user will be redirected to the terminal. The following two commands can save the new changes and exit. Let’s discuss both!

Using Write and Quit Command (:wq)

How to install Kali Linux in Window...

To save the changes in the file and quit the Vim editor, you can follow the below-performed steps:

You need first to create a file using a vim command by using the following syntax:

$ vim [fileName]

Note: If the file is already created the <fileName> file will open in Vim editor, else a new file with the name of <fileName> will be created and opened in the Vim editor.

After creating the file, hit the “Esc” button for “Normal” mode and enter the command “:wq” to save changes in the file (write) and quit the editor.

Using Exit Command (:x)

Another way to quit the editor is by saving the changes in the file and executing the “:x” command in the Vim editor. To quit the Vim editor after saving the edited file using the exit command, perform the below steps:

  • Open the file and make changes.
  • Press the Escape button for Vim “Normal” mode.
  • Press the “:x” command to quit, and changes will be saved automatically.

How to Exit Vim Without Saving the File?

There are two different ways that you can use to exit vim without saving the file, which are explained below.

Exit Without Saving a File in Vim

If you have accidentally opened the wrong file in Vim and want to exit the file without changes, perform the below steps:

  • Open the file in the Vim editor.
  • Change the Vim editor mode to “Normal” by pressing the  “Esc” key.

Note: If you are not in normal mode after pressing the “Esc” key, press the “i” key for insert mode, then press the “Esc” key.

  • Enter the “:q” command to quit Vim without any changing in the original file:
:q

Exit After Editing & Without Saving the Text

To exit the Vim editor after editing the file, and if you don’t want to save those changes, follow the below steps:

  • Launch the Vim editor and open the specified file.
  • Press the “i” key for insert/append mode and edit the file.
  • Press the “Esc” key for Normal mode.
  • Without saving the editing of the file, press the “:q!” command to quit Vim.

The user will be directed to the terminal after exiting the Vim editor.

Note:  If you exit the file after editing and using the “:q” instead of the “:q!” command, it will give you an Error: No write since last change (add ! to override).

Bonus Tip: Saving & Quitting a File in Vim Using a Shortcut Key

If you are looking for a shortcut key to save & exit the Vim editor, the “CTRL+ESC +ZZ” shortcut key is used to save the file and exit the editor.

That’s the end!

Conclusion

To save a file and quit the Vim editor, the “:wq” or “:x” commands are used in Vim’s normal mode. We can quit the editor without saving the files executing the “:q” or “:q!” commands in Vim. This post has briefly demonstrated the possible methods to save the file in Vim and quit the editor.