How to Quit VIM: 10 Ways to Exit VI

Jun 12, 2014 - 12 Comments

How to quit out of VIM VIM is a very powerful command line text editor that is loved by experts and often hated by novices, mostly because it has a fairly steep learning curve that may seem unintuitive to those who aren’t familiar with it. Even something as simple as quitting out of VIM can seem like a challenge to those who aren’t familiar with VI, and often results in mashing Control+C Control+X !q !q enter escape Control+Z until eventually something happens and maybe the process suspends or maybe quits who knows for sure, so just follow that up with a “killall vim” and on you go, back to open the same text file again in nano, right?

OK, that’s not ideal, but we’ve all been there. That’s what we’re looking to avoid from now on, because even if you don’t want to use vi you can at least know how to exit it properly. This tutorial will show you exactly how to quit VIM / VI properly, actually it will show you multiple different ways to exit VI!

10 Ways How to Quit VIM / VI

This is a question that appears in our comments fairly often on command line articles… how on earth to actually quit out of VIM? It turns out there are literally 10+ ways to quit out of VIM, which is perhaps indicative of why VI baffles so many users. Let’s get to the easiest methods first:

Quit VIM without saving with ZQ

Hit the ESCAPE key, then hit SHIFT + ZQ

This will instantly quit out of VIM without saving, basically the same as the :q! command.

Quickly Quit VIM with Write & Save to File

Hit ESCAPE key, then SHIFT + ZZ

These are the two fastest ways to exit out of VIM for me personally, but everyone has their own opinions here and many prefer to type a command in the more traditional manner.

Quit VIM with :q

Hit the ESCAPE key, then type :q and hit RETURN

To be perfectly clear, hitting the “Escape” key enters into command mode. Then typing :q is literal, as in a colon not a semi-colon, so it would be Shift+; followed by q and hitting the Return key enters the command to quit.

This only works if no changes have been made to the document, so to quit if changes were made you make a slight adjustment and at a bang at the end:

Hit the ESCAPE key, then type :q! and hit RETURN

Quit VIM and write changes with :wq

Hit ESCAPE and type :wq and hit RETURN

This saves (writes) changes to the active file and quits. You can force this by adding a bang if necessary:

Hit ESCAPE and type :wq! followed by the RETURN key

This should help avoid this situation:

quitting VIM humor

We can be a bit more thorough though and cover every possible way to quit VIM (at least that I know of courtesy of the man page, chime in the comments with more ways if there are others that we missed here), which we’ll get to next:

Every possible way to quit out of VIM

Hit ESCAPE key to enter into command mode first, then use any of the following :

  • :q – quit
  • :q! – quit without saving, even if modified
  • :cq – quit always without writing
  • :wq – write / save the current file and exit
  • :wq! – write the current file and exit always
  • :wq (name) – write to file (name) and exit
  • :wq! (name) – write to the file (name) and exit always, even if modified
  • ZZ – save the current file if modified, then exit
  • ZQ – quit and exit without saving
  • Launch a new terminal and type ‘killall vim’ – this is a joke that many will understand, and while not a proper way to quit out of vim it does work

So that’s how to escape VIM, any less confused? Perhaps not, and that’s OK, we’ll generally stick to using nano around here for walkthroughs because it’s more user friendly. And there’s nothing wrong with that, even though I’ve grown more comfortable with VIM over time, I still prefer nano out of ease and perhaps old stubborn habits.

How to quit out of VIM

For those interested in learning VIM or at least becoming more comfortable with it, you can always try the vimtutor command, use this excellent online interactive VIM tutorial, and just get more practice by using it often on any terminal. You can even get vim on your iPad or iPhone if you’re really committed. And at least you know how to quit out of vi now, right?

.

Related articles:

Posted by: Paul Horowitz in Command Line, Tips & Tricks

12 Comments

» Comments RSS Feed

  1. Vim for Mac says:

    Hello, this is brilliant :)
    My 5 cents: if you have multiple split windows in Vim, you need to do “:qall” or “:qall!” to quit all windows (or repeat “:q” multiple times).

  2. harperville says:

    Typo: you specifically say “colon” not “semi-colon” in the first half of this sentence, “Then typing :q is literal, as in a colon not a semi-colon…” but in the latter half you have, “…so it would be Shift+; followed by q…”, and show a semi-colon.

  3. BT says:

    :x

    Save and exit this file.

    Rarely, as in Sasquatch-rarely, need anything else.

  4. HC says:

    When you have more than 1 file open and want to save all changes and exit quickly, just try this:

    Saving changes in all files

    :wa

    Quitting vim closing all files properly at the same time

    :xa

  5. Nando Vieira says:

    Damn… my shortcut was replaced by a emoticon. It’s colon-x

  6. Nando Vieira says:

    > Every possible way to quit out of VIM

    Not really… I use :x to save and quit.

  7. adam says:

    This and the how to install Yosemite on external Disk are just some joke…

    But just like your blogs,
    somes helps a good ;)

    • Paul says:

      We try to cover a wide range of topics for various user proficiencies, so while some are vi experts, others aren’t. But yes, there is some intentional humor here! =]

  8. Ryan says:

    I think it’s a right of passage for every dev to mash the keyboard a few dozen times in their early days learning vi ;)

    I do appreciate the article and it may help some newbs. Or yea, stick with nano or emacs. Easy.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site