DEV Community

Cover image for Emergency Vim Commands
G.L Solaria
G.L Solaria

Posted on

Emergency Vim Commands

Now I love vim but I have seen fear in the eyes of people who somehow find themselves faced with vim for the first time. If you haven't experienced that fear then don't feel all safe and snugly because it will happen to you one day. But perhaps you don't fear it and since you were little you have always dreamt of being able to save other people from vim. In that case the following article will equip you with the necessary skills to become a vim first responder.

First and foremost the stop-drop-and-roll of the vim world - exiting without saving:

ESC

:

q

!

ENTER

So the ESC key puts you into normal mode (sometimes called command mode). Keys do different things depending on which mode you are in. In normal mode the above key combination will quit without complaining and without saving. Now hitting ESC when already in normal mode does nothing (other than perhaps beep at you) so it's safe to do it just in case you have accidentally fallen into the depths of insert mode and not realised it.

If you are feeling brave and you actually want to modify the file, you need change to insert mode by simply pressing the i key (if you are disoriented and don't know which mode you are in escape to normal mode first just to be safe):

ESC

i

Now go for it - enter the characters. But be wary of using the arrow keys in insert mode - hidden dangers may lurk there!

Alright keep low, now you are going to have to manoeuvre. First, escape to normal mode. Now I won't be all self-righteous and say you have to use the hjkl keys because using the arrow keys is usually fine in both modes (unless you find yourself in an escape room with a VT100 terminal).

ESC

h

j

k

l

But be warned: back in the old days if you tried to use the arrow keys especially in insert mode, scary escape sequences would start to dance across the terminal. If this fate befalls you, then it is time to stop-drop-and-roll. To stay safe, a good first responder will attach the hjkl keys to their Swiss army knife just in case.

Deleting characters can also get a bit hairy. Backspace can usually be used in insert mode. I repeat backspace can usually be used:

BACKSPACE

But ... it can also cause scary escape sequences to dance across the terminal. If you are lucky it may simply be ineffectual. To safely delete a single character, escape to normal mode and hit x:

ESC

x

Finally lets seal off the area by saving and quitting:

ESC

:

w

q

!

ENTER

You could use other key combinations to save and quit but I think this one is the most memorable because it is : (just because), w for write, q for quit, ! for just-do-it-already-without-bothing-me-further.

So I think that's all you probably need to know about how to be a vim first responder. If you want further training for that fateful day you encounter vim, try the Interactive Vim Tutorial.

Top comments (9)

Collapse
 
glsolaria profile image
G.L Solaria

I do know about ZZ. I thought that maybe I could say when it is time to go to sleep and seal in the memories but it didn't fit with my theme. I also thought that making it to similar to :q! would be easier for people to remember if they don't use vim regularly. Can you tell I think too much sometimes? But you are right - ZZ is the way to go when time is of the essence!

Collapse
 
kostassar profile image
Kostas Sar

I think this post will save many people trapped in vim!

Personally plain ":wq" has never bothered me except in case I was in a file I should not have edited in the first place (Or without the correct rights).

What issues does '!' solve?

Collapse
 
glsolaria profile image
G.L Solaria

It just forces the command without asking any more questions. I figured emergency responders don't want to be asked "are you sure?" after they have already issued a command :)

Collapse
 
peteruren profile image
Pete

A great tongue-in-cheek but informative article, not only painting 'Dont Panic' on Vim but reminding those of us (and by that I mean me) working in VT100 terminals 30+ years ago and encountering these common issues on any terminal input.

Collapse
 
roramigator profile image
Morado

I no longer have to be afraid...

Collapse
 
daviddalbusco profile image
David Dal Busco

I use "Esc : q ! enter" way too often, not ashamed 😄

Cool article, thx you 👍

Collapse
 
maxdevjs profile image
maxdevjs • Edited

Good article, nice tips. I also learned a new word, "bothing". And I use ":q" to exit from my terminal now

Collapse
 
glsolaria profile image
G.L Solaria

You showed me a new word today! I didn't realise I was being so obscure. Good pick up.

Collapse
 
voyeg3r profile image
Sérgio Araújo

Sometimes we ask ourselves, after messing up with a file for a while, how in the hell can I discard all changes I made in this file? The answer to this:

:e!

Now our file got back to its old stat