How to never get stuck in Vim again

Zac Braddy
RightIndem Team
Published in
5 min readApr 18, 2018
git commit

….wait

I FORGOT THE -m !!!

Oh…..for crying out loud…

….LET ME OUT!!!!!

As git’s default editor; Vim is spun up, to the chagrin of any non-Vim user, if you run the git commit command without the -m option to supply a message. What happens next for most non-Vim users is a series of failed attempts to do what would seem to be the simplest way to correct their mistake, close Vim. After a few minutes of attempting this a newly humbled developer will simply close the console window they were in and hope that no one saw what they just did.

I’m actually a full blooded Vim user now but to be able to call myself a Vim user I had to fight a gruelling knowledge battle over an extended period of time and it was complicated further by my tech stack of choice. As both a .NET developer and a Windows user my concerted effort to become a Vim user was an endeavour only undertaken after having spent many years in my career spitting a series of increasingly creative insults in Vim’s direction. Most of the time this string of expletives would begin with the scenario above.

In future articles I’d like to teach you more about Vim and show you what’s great about it and why it’s worth learning but the lesson for right now is simply to help you save some of your pride. In this article I will teach you how to exit Vim.

The Deer in headlights scenario (a.k.a What have I done?!)

Scenario

You’ve simply left the -m off your git commit and you’re now in Vim (again) and you’ve f̶r̶o̶z̶e̶n̶ ̶u̶p̶ ̶i̶n̶ ̶c̶o̶m̶p̶l̶e̶t̶e̶ ̶d̶i̶s̶b̶e̶l̶i̶e̶f̶ ̶t̶h̶a̶t̶ ̶y̶o̶u̶’̶v̶e̶ ̶d̶o̶n̶e̶ ̶t̶h̶i̶s̶ ̶a̶g̶a̶i̶n maintained your composure. You haven’t pressed any buttons yet but you’re not sure which one will make the pain go away.

Answer

:q <Enter>

That’s it, just type full colon, then q, then press the Enter key and you’re free. No need to read any further. What you’ve done here is provide Vim with the quit command.

The Hungry Hippos scenario (a.k.a hold my beer, a.k.a the 500th time this has happened and it’s not beating you this time)

Scenario

It’s happened again! This god damned thing that keeps you stuck in the console! What even is it anyway?! This one time you managed to get it to go away but you can’t remember for the life of you what you typed to get it to go away. You’ve got just the medicine for this…

*Furious keyboard clacking*

*Furious keyboard clacking intensifies*

You’re stuck…..you’re hopelessly stuck. You try to follow the advice above but it doesn’t work.

Answer

Well, the truth of the matter is that you could be in any number of modes or positions at this point. Here are a few ideas that might work but my best advice is that next time try and be more like our deer friend in the scenario before this one as the solution is a lot simpler when you do. That said, why don’t you start with this:

<Esc>:q<Enter>

There’s a chance you’ve found yourself in an editing mode in Vim and when this is the case if you try using the :q trick from above it will just type :q into the buffer you’re editing. To get back into normal mode so you can enter the quit command you need to press <Esc> first.

If that doesn’t work then maybe you’ve opened up some new windows within Vim and not noticed it. If you’ve tried the above and you’re still in Vim, we can try closing all the open windows in Vim. To do that we use the quitall (notice the q and a?) command like this:

:qa<Enter>

If you’ve done all this and you’ve been finding that you’ve been getting presented with a message like this:

Then at some stage during your keyboard mashing you’ve edited the buffer that was open and now Vim doesn’t want to let you out without saving your changes. In this case you just want to thank Vim for being so thoughtful, but seriously, right now we just want to exit Vim and spend a little time in the coffee break room trying not to cry. The “no seriously” character in Vim is the bang, so if you’re in this scenario you can get out like this:

:q!<Enter>

If you’re still in Vim then, well, mashing got you here and mashing may get you out. Why not try mashing all these commands together, see if this works:

<Esc>:qa!<Enter>

One of these might have worked for you, if not then enjoy closing your console again. Again, by far the best solution is to not mash the keyboard if you find yourself in Vim and want to get out with your pride in tact.

Be a badass (a.k.a. Actually use Vim)

As I said at the beginning of the article I’m actually a pretty big fan of Vim and it would be shame if I finished without actually telling you how to use it. To actually use Vim to enter your commit message in git follow these steps.

  1. Use the git commit command in your command line to open Vim.
  2. Press the i key to enter insert mode.
  3. Type your commit message.
  4. Press <Esc> to leave insert mode and get back into normal mode.
  5. Press :wq<Enter> to tell vim to write the buffer and quit.

After doing this git will recognise that you’ve written a message to the buffer and use this as your commit message.

I’m hoping to write more articles about being a Vim user on Windows but if you aren’t interested in them then I hope that, at least, I’ve been able to teach you something useful.

--

--

Zac Braddy
RightIndem Team

Dad — Podcast host of @tabsnspacesHQ — Proglot polygrammer — So you think you can meme? — Are you still reading? He/him