How to Vim / Vi Save and Quit the Editor

Vim is an improved adaptation of Vi - the standard Unix visual text editor. Vim is available on literally all Unix-like systems, including Linux and Mac OS.

Vim is quite fast and almost all functions have shortcut keys. Vim is a very useful text editor for programming/scripting and editing configuration files and more.

In this tutorial, we will show you how to save a file in Vim / Vi and quit the editor.

1Open FileType vim filename
2Edit FilePress i
3Save FilePress ESC and type :w
4To QuitPress ESC and type :q
5To Save and QuitPress ESC and type :wq
6To Quit Without SavingPress ESC and type :q!
Vim / Vi - Save and Quit

Save a file in Vim / Vi

Firstly, run the command below to open Vim.

$ vim

You should see a new blank text editor.

On most systems, when you run vi, it will also open Vim.

$ vi
Open Vim

To open an existing file with Vim, run the command below instead.

$ vim filename

Note: If the existing file is not in the current working directory, you should include the file path. For example:

$ vim /home/shola/filename

Vim has two modes which allow you to perform different sets of operations. They are; Normal mode and Insert mode.

Normal mode

When you open Vim, you are in normal mode. This mode allows you to enter Vim commands and move around the file.

Insert mode

The Vim insert mode allows you to insert text into the editor. To switch from normal mode to insert mode, press i on your keyboard. Notice the -- INSERT -- cue at the bottom left of Vim.

Vim Insert Mode
Vim Insert Mode

Now, go ahead and enter some text into the editor.

To save your file, you would need to first switch back to normal mode. Press the ESC key to leave the insert mode and return to normal mode.

Next, type :w and press enter to save your file. You can think of this as writing your changes to disk.

:w

Note: If you are working on a new file, you would be required to name your file. Below is an example.

:w testfile
Save a file in Vim
Save a file in Vim

You should see a message at the bottom of Vim indicating that your changes have been written.

Quit Vim / Vi

While in normal mode, enter the following command to quit Vim.

:q
Quit Vim

To quit Vim without saving your changes, the command you should run is;

:q!

Further, it is possible to save your file and quit Vim in one go as follows.

:wq

Alternatively, run the command below to save changes and quit Vim;

:x

Conclusion

In this tutorial, we showed you how to save a file in Vim, as well as how to quit the Vim text editor. If you have any questions, we are happy to clarify them for you.

Leave a Comment