vim devops linux devBelieve it or not, “how to exit VIM” is one of the most googled sentence, for a reason.

You stepped into the command line world, now you want to take control over it; you touched your first awesome file, and now you want to put something into it!

Reading on the web, you found out that VI or its ++ version VIM (Vi IMproved – website) is one of the powerful command line editors, so you decide to open your console and you start typing

YAY! Here is your brand new awesome file, and you are ready to write whatever you want, I can feel your happiness going through your fingers as you write down your top notch script, but you are just a few steps away from desperation.

The content is ready, you feel satisfied, what now? Maybe try to save and exit, but how?

Here we are, there are several different ways to exit the editor, according to your needs (save and exit, quit without saving, etc).

Yes, but, how to exit VIM?!

Well, first of all, the most important thing to understand is that VI has 3 different operative modes, that is to say:

  • Command mode → VI accepts commands, you see no cursor on the screen
  • Input mode → VI allows you to write, the cursor position itself on a line
  • Directive mode → accessible via : while in command mode, is where you can input commands to manage the files you are editing; similarly to command mode, no cursor can be seen on the screen.

Since you are smart, you already got that what we need is to enter directive mode, so we are going to press ESC key, this will bring you into command mode.
Once in command mode every keystroke has a different ‘role’, many combinations can be used to edit lines, replace words and so on, but we just need to get into directive mode, so we can finally exit this maze!

These are some combinations and their relative functions:

:w – Saves the file without exiting
:q – EXITS VI!
:wq – Saves the file and EXITS VI!
:x – Shorcut for :wq, but still EXITS VI!

Shift+zz – (ZZ) Shorcut for :x and :wq , but still EXITS VI!

If you append a ! to the previous commands, you are telling the editor to override any warning and force the command.

CONGRATULATIONS! You just escaped the VIM maze and learned how to exit vim :)