Vim - How to Save and Exit

illustration of a woman on a computer

Want all the basic vim commands in one handy sheet? Check out our vi/vim cheatsheet!

Vim Cheatsheet
Complete list of commands and shortcuts to navigate around the world of Vim / Vi.

Three Ways To Save and Exit

Method 1: Using ZZ

The keyboard shortcut ZZ makes it easy to both save and quit VI or VIM. The command to save and quit. If you are in insert mode, here are the steps you need to follow:

  • Press the esc key
  • Hold down the Shift key, and pressing Z twice

That's all! That's the quickest and easiest way that you can exit out of vim after saving.

Method 2: :x

:x does exactly the same thing as using ZZ and is a quick and easy way to save your vi/vim file and quit it simultaneously.

  • Press the esc key to enter command mode
  • Type colon followed by: :x then press enter

Method 3: :wq

:wq can also be used to save and quit vim - the only difference is that where :x and ZZ only save the file if it has been modified, :wq writes the file regardless.

  • Press the esc key to enter command mode
  • Type colon followed by: :wq then press enter

Related: How to Select All in Vim

How To ‘Select All’ in Vim
In this guide, we cover the main methods with which you can select all in the vim/vi editor.

According to the Vim documentation, :x and ZZ are similar and only save a file when the corresponding buffer has changed. However, even if the buffer is untouched, :wq saves it to file.

The contents of the buffer will be saved to disc in both scenarios. Since the result would remain the same, why bother, right? There is a slight distinction that is important for certain use cases and situations.

If you quit vim with the command :x, the modification time of the file won't change if the buffer hasn't changed. However, since the file is technically overwritten if you exit with :wq, the modification time will change as the file will be overwritten.

Related: How to quit vim without saving

Vim: How to Quit Without Saving
How to exit the vim/vi command without saving