How to exit vim in 5 simple steps.
OR: Help! I was using git to commit some code and now the window has changed and I don’t know what’s going on!
*This post is the first in my ‘Pre-Job Series’ — for more info, check it out here*
Sound familiar? For those of you, going ‘Huh?’, imagine the situation. You have written some code, diligently ran git status to check which files have changed, added the files and even ran git status again just to check everything has been staged properly like so:
So far so good. But then typegit commit and this happens:
The astute amongst you will have noticed that instead of writing git commit -m 'my commit message', I just wrote git commit.
Because you can’t make a commit on git without a message, what has happened is that git has opened an editor called vim in order to force you to do so. Now the problem here is that many folk, especially newbies just starting out¹ sometimes get a bit over-excited with their commits and forget to add a flag and their message. And then when this screen happens, it’s nothing they’ve ever seen before and they don’t even know how to google it because they don’t know what it is. And worse still, they can’t get out of it without simply shutting down the terminal window, which I think we all agree is a pain.²
Git also opens vim during merges and I’m pretty sure vim also seemingly randomly opens in other circumstances that I can’t bring to mind at this precise moment.
The effect that this has is more or less like wandering through a city you know well, opening a door and finding yourself in Narnia, complete with talking animals and scary witch. Oh, and the door you entered through has somehow disappeared.
But exiting the vim screen of what the effing crap is actually very simple. Once you know that what has essentially happened is that a vim editor has opened in your terminal window, you can just google from there. In fact, over one million developers have done just that! It is in fact the top searched question on Stack Overflow:
But to save you the hassle, here’s what you do:
- Type i. The editor opens in ‘normal’ mode, where everything you type is interpreted as a command and where you can do stuff like saving the file and exiting. Typing i means that you switch to ‘insert’ mode which allows you to edit the actual file you’re working on (and yes, that really is what you are doing here — your simple little commit message just turned into a file…).
- Write your commit message on the first line.
- Hit esc. This will return you to ‘normal’ mode.
- Type :wq. It’s important that you type the colon or it won’t work — apparently this is a throwback from earlier days. The w stands for ‘write’, i.e. save and the q for ‘quit’ — so basically, save and quit.
5. Finally, hit enter — and you will be taken back to your normal terminal window.
And that really is it!
 Myself included back in the day, which was, like, all of a year ago…
 And actually, it’s not just newbies who experience this trauma. When I told my colleague, a decent mid-level back end dev, compsci grad and everything, that I was writing this post, his comment was ‘Yeah, do it, I still don’t know how to get out of vim’. Though clearly in his case, he ends up in vim for other reasons than getting over-excited with his commits.
This post is the first in my ‘Pre-Job Series’ — for more info, check it out here*