Want to Learn How to Quit VIM? Here Are 10 Ways to Exit VI

Jun 12, 2014 - 11 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 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?

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

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

11 Comments

» Comments RSS Feed

  1. 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.

  2. 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! =]

  3. Nando Vieira says:

    > Every possible way to quit out of VIM

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

  4. Nando Vieira says:

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

  5. 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

  6. BT says:

    :x

    Save and exit this file.

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

  7. 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.

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

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