r/programming • • May 23 '17

Stack Overflow: Helping One Million Developers Exit Vim

https://stackoverflow.blog/2017/05/23/stack-overflow-helping-one-million-developers-exit-vim/
9.2k Upvotes

1.1k comments sorted by

View all comments

832

u/skztr May 23 '17 edited May 24 '17

My "how to use vim" guide in every wiki I've ever made for a company includes only the instructions:

  1. Press "escape"

  2. Type ":"

  3. Type "q"

  4. Press enter.

If you're in a position where you need more instruction than that, you probably already know how to use vim. If you don't know how to use vim, those are the only instructions you will ever need.

.... FFS after typing this comment I swear to god I just typed ESC :wq

edit: As several people have mentioned that the command should probably include an exclamation point, I logged in to an old wiki I currently have access to in order to copy the actual text verbatim:

--------8<---------

  • vi The default UNIX editor. Don't use it.

  • vim The real default UNIX editor: Running vi on many modern servers (including our own), actually runs vim in “compatibility mode”. If you don't already know how to use it, you should do this:

  1. Hit “Escape”

  2. Type :q! (that is: colon, q, exclamation mark)

  3. Hit “Enter”

This will exit the editor without saving changes.

If you really want to use it, see: http://www.vim.org/htmldoc/quickref.html

-------->8---------

260

u/AdvicePerson May 23 '17

.... FFS after typing this comment I swear to god I just typed ESC :wq

The worst is that ESC will close an email you're writing in Outlook.

93

u/skztr May 23 '17

ESC closes a lot of things. At least Hangouts remembers the message you were typing, in newer versions

27

u/diamondflaw May 23 '17

You'd think it could share some of its burden with my lonely Scroll Lock key.

24

u/chrunchy May 24 '17

The people who designed my last keyboard thought they would shave some dollars off of production costs and omitted the scroll lock key.

Too bad for me because i was heavy into Excel at the time and it's the only program I know of that actually had a use for the Scroll lock key. I had to use the onscreen keyboard to toggle it.

→ More replies (2)
→ More replies (9)

57

u/Trollygag May 24 '17

Ctrl+S = Save in Microsoft products

Ctrl+S = Suspend a terminal for some flavors of *nix terminal

Dat 'oh my gooooood' face on new devs.

41

u/f1u77y May 24 '17 edited May 24 '17

Microsoft

I'd say in almost all software which has GUI.

EDIT I've remembered those days when I used to use MS Office and was surprised by Shift+F12 shortcut to save document in MS Word.

→ More replies (4)
→ More replies (7)
→ More replies (8)

156

u/nitiger May 24 '17

:q(uit)

:w(rite)q(uit)

:q(uit)!(goddammit!)

201

u/kilot1k May 24 '17

I just flip the breakers and power down my house.

72

u/down1nit May 24 '17

Hey that worked! Thanks.

12

u/kilot1k May 24 '17

Sometimes the easiest solution is the best.

→ More replies (6)
→ More replies (2)
→ More replies (5)

101

u/dwhite21787 May 23 '17

:! sudo /bin/bash

24

u/[deleted] May 24 '17

I laughed harder at this than I should have

28

u/dwhite21787 May 24 '17

used to be that any vi :! subshell command would appear in "ps" as another vi instance, so you could get in vi, :! nethack , and nobody'd be the wiser. And it taught you vi key cursor commands.

→ More replies (5)
→ More replies (1)
→ More replies (3)

304

u/wilhelmtell May 24 '17

There is an art to exiting properly from Vim. :x is (I'd argue) a better way than :wq to exit Vim, because it only saves the buffer if the buffer is modified. ZZ is a synonym for :x so that works as well if you prefer that.

This, :x rather than :wq, makes a difference if, for example, you're on a header file that's transitively included in a gazillion translation units in a project that takes forever to build. Particularly if :wq would save no real changes but just a touch, it can be a frustrating waste of time and even a kick out of zone if you were in flow.

Related, there's also the contrast between :w and :up, AKA :write and :update. I personally have a mapping for :update and I avoid :w and :wq. This is what many IDEs and GUI editors do too, if that bears any motivation.

And, there's also :wa, for updating all modified buffers. A misnomer, since there's a w there even though it does an :update and not a :write. And of course :wqa, which again is an update all and quit rather than a write all and quit. Useful for quickly exiting from a successful merge resolution.

And finally, I love :cq, Vim's ejecting seat. It means abandon and quit with an error. Programs that fork $EDITOR generally listen for the return code, so this is the way to communicating to them a "cancel". I do that for example when a manual merge conflict resolution gets hairy and I want to start again. Exiting with an error immediately communicates to Git that the merge failed, so Git doesn't accidentally accept my mess.

So there you have it. I just spent 5 paragraphs on exiting from Vim. oO

Anyway, the wiki should say <ESC>:x<RET> and not <ESC>:wq<RET> :)

85

u/joeltrane May 24 '17

I want you to sit in my living room and explain things to me.

→ More replies (1)

14

u/Ahri May 24 '17

Thank you for this, I think it's the wisest reply here. I didn't know any of these tips despite using vim for years!

11

u/temp4509840984 May 24 '17

There is an art to exiting properly from Vim.

Vim in a nutshell.

(Seriously though, thanks for the writeup.)

→ More replies (14)

31

u/atomheartother May 23 '17

If you're in a position where you need more instruction than that, you probably already know how to use vim. If you don't know how to use vim, those are the only instructions you will ever need.

I know that I have to type ESC then :q but I still distinctly remember getting stuck in vim the few times I've tried it. So it can't be that easy.

47

u/LunarMadden May 23 '17 edited Jun 07 '17

If you edited the file, it will prompt you to either save (:wq or :x) or force quit (:q!).

21

u/AndrasKrigare May 24 '17

If you happen to miss the ":" before hitting "q" (even if you don't hit "enter") it'll go in to some recording mode which requires a couple more ":q"s to get out of, and can freak people out a bit.

→ More replies (3)
→ More replies (1)

24

u/reepha May 23 '17

'Ctrl+[' works as an alternative to escape if you don't want to move your fingers to esc. You know, to save yourself the strain and maybe a few milliseconds.

39

u/skztr May 23 '17

I have CapsLock remapped to be an extra Escape key, as any self-respecting* vim user does

23

u/malnourish May 23 '17

Caps to control! Useful in every program

→ More replies (23)
→ More replies (17)
→ More replies (3)
→ More replies (54)