5 totally incorrect ways to exit Vim

The Vim text editor can seem confusing to beginners, but it's worth learning more than just how to exit.
Register or Login to like
System statistics with sar and the /proc filesystem

ajmexico. Modified by Jason Baker. CC BY-SA 2.0.

Vim can be a challenge to learn. But for many first-time users, just exiting the program can be a problem.

Last month, the Stack Overflow blog published an article on "helping one million developers exit Vim," a testament to the then-recent milestone of a million views on a popular question on the developer help site: how to exit the Vim editor? Based on the traffic, Stack Overflow says that during peak hours on weekdays, 80 people an hour were reading about how to get out.

In fairness, if you're not familiar with Vim, getting out can be a little unintuitive. Mashing Escape, hitting Ctrl+C, typing "quit" or "exit" alone won't yield you anything. Vim does try to give you some helpful hints along the way, even showing you how to get out right when you load the program for the first time.

vim-exit.png

Unfortunately, by the time you're using a search engine to find the answer, you've probably mashed a handful of other keys on the keyboard, entered insert mode, and have no idea that you need to hit Escape to be able to enter the commands to exit. What's worse, you may have made some changes to the buffer, meaning now Vim wants you to confirm that you wish to exit without saving your changes, requiring a whole different keystroke pattern than the one you just figured out.

Further, the way many people are introduced to Vim for the first time is from the Git command-line client, when they make a commit but don't specify the commit message along with the Git command. They have no idea where they landed, what they're doing there, or how to get back out.

Vim isn't hard to learn if you're willing to commit a little time to it, and its ubiquity makes it worth the time. If you want some help, we've got 5 tips to help you get started learning Vim.

But today, in celebration of Vim's rather well-known propensity to stump casual users just trying to exit, here are five more ways to exit Vim. I recommend none of them.

Hit the x

That's right, just use your GUI to kill the whole terminal window and give up. Surely there wasn't anything else you needed that window for, anyway, and you didn't want to save any potential changes you may or may not have made to that document you had open. Technically, with a graphical version like GVim, it's not the end of the world, but if they point of Vim is to keep your finger on the keyboard, if you're using the GUI to exit you're still missing the point.

Kill it automatically

Why worry about whether you'll remember how to exit Vim after you launch it? Instead, just start it up with a built-in kill switch using timeout.

$ timeout 60s vim

Your Vim instance can be gone in sixty seconds. Hope you remembered to save!

Kill all the Vims

What do you do when any other program runs amok? You throw open another terminal and kill it. Use your preferred method: ps + kill, find it visually in top or htop, or my favorite, because it makes me sound like a renegade, killall.

$ killall -9 vim

Goodbye, Vim(s).

Close it remotely

I haven't tried this personally, but apparently, Vim's client-server capabilities mean you can access a remotely-running instance of Vim by ssh'ing in, and politely ask Vim to save your file before you exit. The Vim Wikia has full instructions. Your mileage may vary, and this may actually not be a terrible idea, if you're the sort of person who leaves unsaved changes sitting in Vim after you leave a machine—but that, itself, is probably not the best idea.

Smash your computer

No, really, please don't do this. There are better ways. But if your level of frustration is nearing that level, just head over to your favorite video or gif site and let off some steam by watching others play a real-life edition of Super Smash Brothers with their machines.

Can't wait to get out of Vim? Let us know in the comments. Of course, Emacs users don't have this problem. I assume the solution to exiting there involves switching to exit-mode and writing a little bit of Lisp to initiate exiting.

Jason Baker
Former Red Hatter. Now a consultant and aspiring entrepreneur. Map nerd, maker, and enthusiastic installer of open source desktop and self-hosted software.

14 Comments

A mildly inappropriate way to exit vim is with :q!, which sure enough closes it, but also doesn't save anything you've done.
I mostly use emacs, the big problem there being all these various Ctrl-, Alt- sequences for various things. The one I may use most often is Ctrl-G, to kill any command sequence I accidentally started.

After years of using vim, I only recently learned that you can save and quit vim with ZZ. Much easier than :wq

Thanks for a great article. I love that video too. It's been awhile since I've seen it.

Every once in a while X crashes and it becomes impossible to close and save VIM. Thanks for the remote close idea. Usually I have some other device around I can ssh in and shutdown -r, but being able to save VIM is nice.

There's no need to enter an ex command at all - ZQ will quit the editor in the same manner as :q!.

nano

Problem solved.

Solution:
Run emacs in vile mode, but rebel and use c-x c-c to exit.

Ways to exit Vim and save the work done first:

° If you are entering text, first press the Esc key, then type ZZ, that is, (shift) zz

° Press Esc then :x

° Press Esc then :wq

° Using the graphical interface menu, press File, then Save or Exit

If you want to exit without saving your work, then Esc followed by :q! will safeIy exit without saving or leaving buffer content and no dangling Vim process

These may not cover every possible way to get out of Vim, but they are safe, effective ways to exit, either saving or discarding your work, depending on whether your work is what you want to do or if it is something that you want to back out.

All your not-very-experienced Vim users really ought to be made aware that all the shoot-it-in-the-head methods of killing Vim will result in it leaving a .swp file, which will cause even more confusion next time they open the file they were editing.

Being a short-tempered IT fellow, my boss would make me interview candidates that I didn't want to. I would sit them down and make them navigate VIM. If they couldn't, I would demand they remove Unix admin from their resume and ask them to leave.
My interviews typically lasted about three minutes.
That is all.

An interesting note to "ZZ" I just learned is it won't save if your disk copy has been deleted, while :wq will. I was (foolishly) working on a test script in /tmp and the next day I'd left it open. /tmp had been cleaned but I assumed ZZ would write it out - nope. I'd not made a change since the last save (now deleted) and viola! the file was gone.

perfectly normal.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.