Vim Basics

How to exit vim

You're probably stuck in vim right now so we'll get right into it. Here's a few different ways to exit vim.

Force vim to exit without saving :q!

To exit vim without saving your changes, press esc to enter normal mode and then type:q! and press enter. This will exit vim without saving your changes. Since this is using the force modifier, it will exit even if you have unsaved changes.


How it works:

  • esc – Enters normal mode
  • : – Enters command-line mode. This is where vim will only parse the command once you press enter. Normally vim figures out what to do on a letter by letter basis.The colon lets you enter a command.
  • q – q is shorthand for the quit command. You can also use quit
  • ! – this is the force modifier. It tells vim to complete a command even if it fails some check. in this case, it tells vim to quit even if there are unsaved changes.

Exit vim without saving :q

To exit vim without saving your changes, press esc to enter normal mode and then type:q! and press enter. This will exit vim without saving your changes. If you have changes that you haven't saved, vim will display a warning. E37: No write since last change (add ! to override)


How it works:

  • esc – Enters normal mode
  • : – Enters command-line mode. This is where vim will only parse the command once you press enter. Normally vim figures out what to do on a letter by letter basis.The colon lets you enter a command.
  • q – q is shorthand for the quit command. You can also use quit

Save and exit vim :wq

To exit vim while saving your changes, press esc to enter normal mode and then type:wq and press enter. This will exit vim and save your changes.


How it works:

  • esc – Enters normal mode
  • : – Enters command-line mode. This is where vim will only parse the command once you press enter. Normally vim figures out what to do on a letter by letter basis.The colon lets you enter a command.
  • w – w is shorthand for write. This saves your current changes to the file.
  • q – q is shorthand for the quit command. You can also use quit

Save and exit vim (alternative):xit

This is another way to exit vim while saving your changes, press esc to enter normal mode and then type:xit and press enter. This will exit vim and your changes will be saved.


How it works:

  • esc – Enters normal mode
  • : – Enters command-line mode. This is where vim will only parse the command once you press enter. Normally vim figures out what to do on a letter by letter basis.The colon lets you enter a command.
  • xit – xit is a combination of the write and quit commands. This saves your current changes to the file and exits vim.

Save and exit vim (alternative, no colon)ZZ

A quick way to exit vim is to enter normal mod eby pressing esc then typeZZ This will exit vim and and your changes will be saved.


How it works:

  • esc – Enters normal mode
  • ZZ – This command saves your current changes and exits vim. If there are no changes, it will just exit vim.

This wasn't an exhaustive list but it should cover you for 99% of vim exiting cases.

Learning vim can be challenging but if you stick with it, you'll be flying through your code in no time. Learning enough to get comfortable and productive is the most important part. Once you're comfortable, you can start learning more advanced features.

If you're looking to get comfortable with vim quickly, check out our concise interactive course! We've helped over 2000 engineers learn to love vim!

Check it out