Posts Tagged ‘ed’

How to Exit Vim (or vi)

Thursday, April 26th, 2018

I never thought I would write a post about this, but evidently it is a problem, so here we go.

The Procedure

  1. Realize you’re in a parallel universe where “bogomips” is a word and computrons and thunks are the competing currencies.
  2. Freak out, probably pressing something awkward in the process, and then in a paralyzed daze watch as help screens, text versions of your favorite recipe website, prayers to Cthulu, and descriptions of long missing persons flash by.
  3. Press <ESC> about a half dozen times in a rush of maniacal panic.
  4. Enter the sequence: “:q!” and hit <ENTER>.

Note that step #3 is actually necessary.

An alternative to #4 is to press the capital letters “ZQ”. (Note: capital letters. Not lower-case. That would mean something totally different to vim. Duh.)

But WATCH OUT! If you have accidentally opened multiple buffers in your moment of weakness (step #2) then your victory stroke would have slain naught but one of the portals to Oblivion and, just like that game where you play a DIY avatar that you’ll never manage to edit into looking anything like anyone who has ever lived, the only way to save Tamriel once and for all (yeah, right) is to continue typing “:q!<ENTER>” over and over until each and every gate to madness has been closed, you are proclaimed hero and the Oblivion Crisis is finally over.

Er, I mean, until you’ve exited vim all the way.

Anyway, “successfully exiting vim” is evidently a tooth-cutting experience and lends tremendous street cred among node.js and Elixir programmers, so hopefully this explanation will help you out when/if the time comes and Fate dares to test your mettle.

Explanation

The first thing to understand is that vim (a.k.a. “The One True Text Editor”) has modes.

When you start vim it will be in “command mode”. In command mode anything you enter is a command: opening files, yanking lines of text, constructing match sequences or whatever else. Command mode is often called “beep mode” because you can always return to it by pressing <ESC> a bunch of times, pretty much no matter what else is going on, and if you’re already in command mode when you press <ESC> it will beep at you — hence the habit of doing something, it not working out, and automatically diddling <ESC> about four or five times until it starts beeping at you.

Other modes include “edit mode” where you can do things like type (the way you expect to do in a text editor), visual mode, and a few others that you either already know or definitely don’t if you are here reading this article. From any other mode, all of them being something other than beep mode, you can press <ESC> a bunch and get back to beep mode, which is where you want to be if your goal is to exit vim.

Once you’re in beep mode, you can exit abruptly (without saving anything) by typing a colon (to enter a command), the letter “q” (to “q”uit) and an exclamation mark to indicate “do it now no matter what!”.