Introduction

Perhaps you are happily configuring a virtual private server or following along in an online tutorial, and you end up, by accident or by necessity (I’m looking at you, Minimal Install), having to use a text editor like Vi or Vim. Do you break out into a cold sweat? Do you get the shakes? Do you feel the uncontrollable urge to flip the desk?

Frustrated non-Vim user

Photo: Peter Hess / licensed under CC BY 2.0

It happens. Vim (I’ll be using “Vim” from here on out, since these tips can be used in either Vi or Vim) can be a confusing landscape to navigate. Devotees may swear by it, but for most of you, it is the text editor of the Beast and should be avoided at all costs. Except when it can’t. For those situations, this quick reference will try to help you navigate your way back to what must seem like much saner pastures.

Prerequisites

– New to Vim
– Fear/hatred of Vim
– Stuck in Vim
– I don’t want to learn it, I just want to get on with life!

Quick Links

Typing
Exiting
Navigation
Find/Replace
Delete
Undo
.

Why Can’t I Just Type in Vim?

Vim works in different modes in which the keyboard keys can perform different actions based on the mode you’re in (kind of the way a game controller’s controls might behave differently in a game based on whether you are flying a plane or running through ancient ruins).

Vim generally starts in “Normal” mode, where many of the keys are mapped to different actions (some of you may consider the name of this mode an oxymoron).

To get to a mode where you can just start typing–called “Insert” mode in Vim world–type:

i

You should see “– INSERT –” show up in the lower left of your screen.

Vim insert mode

Vim insert mode

Most instances of Vim will provide this context clue to help you identify what mode you are in. Some older instances of Vi, though, do not present this clue. Even Vim users hate it when we find ourselves in this situation.

For all you Vim grognards, yes, there are other ways to enter “Insert” mode. This is arguably the most straightfoward. And how did you get here?

 

To get back to “Normal” mode (which is important if you want to save, quit, or executate any of the other actions below), press ‘Esc’.

Note: The remaining tips will be executable in “Normal” mode, so remember to ‘Esc’ out of “Insert” mode before trying to complete these actions!

.

Exit Vim

“I accidentally ended up in Vim and want to quit!”

:q   [then press 'Enter]

“But I’ve made some changes I want to keep before I quit!”

:wq   [then press 'Enter']

“I just want to get out and I don’t care to save any changes I might have accidentally made while mashing keys!”

:q!   [then press 'Enter']

.

Navigation in Vim

Go to the top of the document:

gg

 

Go to the bottom of the document:

G

 

Navigation, measured by page:

Ctrl+u   [Up by half a page]
Ctrl+b   [Back by a full page (equivalent to 'Page Up')]

Ctrl+d   [Down half a page]
Ctrl+f   [Forward by a full page (equivalent to 'Page Down')]

.

Find and Replace in Vim

“I need to find a particular string” (e.g., waldo):

/waldo   [then press 'Enter']

To find the next occurence of the string you are searching, press ‘n’ (as in “next”). If you’ve gone too far, you can cycle backwards through matches with a capital ‘N’ (as in…, “NO! Too far! Go back!”)

 

To replace all instances of one string with another (e.g., find all references to “waldo” and replace with “carmen sandiego”):

:%s/waldo/carmen sandiego/gc   [then press 'Enter']

The ‘c’ at the end of this command will ask you for confirmation for each replacement before continuing (use the standard ‘y’ and ‘n’ to confirm or deny each substitution). If you leave the ‘c’ off, it will replace all instances without confirming.

.

Delete in Vim

To delete a line of text, make sure your cursor is somewhere in the line you want to delete:

dd

You can use this command to delete multiple adjacent lines, as well. Just place your cursor in the first line you’d like to delete, then prepend the number of lines to delete before the dd (e.g., to delete 10 lines, 10dd).

.

Undo in Vim

To undo the last thing you did, type:

u

You can keep pressing ‘u’ to undo each previous action, at least as far back as your session’s buffer goes (so, for example, you could undo the changes you made, but not the changes you or another user made before last editing the file).

 

Care To Learn More?

If you’ve made it this far, I hope you have found some of these tips useful. If you’re interested in perhaps dipping your toe a little further into the Vim pool, you can, on the command line of a Linux device with Vim installed, type vimtutor to start some guided lessons. Or, if you prefer something a little less intimidating, you could always try the game Vim Adventures!