Vi/Vim Basics

The other week Harry Roberts wrote Vim for people who think things like Vim are weird and hard (a really good read if you haven’t read it yet) and so I thought I’d share my own Vi/Vim cheat sheet.

I’ve been using Vi/Vim for many years (the two are pretty much interchangeable so I will refer to them as simply Vim from now). Admittedly Vim is my second text editor of choice. The rest of the time I use Sublime Text.

Sublime Text seems to have become really popular as an editor, but strangely one of the reasons many love Sublime Text appears to be the same reason they’re scared off of Vim. The many keyboard shortcuts of Sublime Text make it quick and easy to use to improve productivity. As Harry says in his piece on Vim: “If you spend 90% of your time typing, reaching for a mouse in order to complete certain tasks is a pretty big dent in concentration and focus.”

Vim is all about keyboard shortcuts and there are many of them, but there’s a lot you can do with just a handful of commands. I put together the cheat sheet below when I started out with Vim to get me going. It only covers the basics, but will get you quickly working with one of the most powerful text editors out there. I hope it helps.

Command and Insert Modes

Vim works in different modes. The two main modes you need to be aware of are command and insert. Command is the normal mode and the one that you are in when you first open a file. All of the commands listed below work in this mode. The only exception is Esc which you use to exit another mode and return to the command mode. If you want to type content into your file you need to enter the insert mode; there are a few ways of doing this, but the easiest is to use i (lower case i for insert).

Editing

i insert text at the cursor position (enters insert mode)
a insert/append text after the cursor position (enters insert mode)
Esc Escape the insert mode
u undo last action

Exit and Save Changes

Closing Vim is one of the things many seem to get frustrated with, but it’s not difficult when you know how. Just remember you need to escape the insert mode if you’re in it using Esc first (in general it doesn’t hurt to preceed any of the following with it first).

:w write changes to file (save)
:wq write changes to file and quit
:q! quit and lose changes (:q will work if the file is unchanged)

Cut, Copy and Paste

yy yank/copy the entire line
yw yank/copy the current word
p paste after the cursor (P pastes before the cursor)
dd delete/cut the entire line
dw delete/cut the current word

Preceded by a number these will repeat the task, so 2yy will yank/copy two lines and 3dw will delete the next three words.

di + char delete inside followed by a character

e.g. to delete inside brackets you would do di(

Search

Find
/pattern forward search for pattern
?pattern backwards search for pattern
n repeat search in the same direction (N repeats in the opposite direction)
Find and Replace
:%s/foo/bar/ finds and replaces all occurrences of ‘foo’ with ‘bar’
:s/foo/bar/ finds and replaces all occurrences of ‘foo’ with ‘bar’ on the current line
:%s/foo/bar/c finds and replaces all occurrences of ‘foo’ with ‘bar’ asking for confirmation first
:%s/foo/bar/ci finds and replaces all occurrences of ‘foo’ (case insensitive) with ‘bar’ asking for confirmation first
:%s/foo/bar/cI finds and replaces all occurrences of ‘foo’ (case sensitive) with ‘bar’ asking for confirmation first

Navigation

G goes to the last line of the file
gg goes to the first line
:10 goes to line 10
f + char forward to the first instance of a character

e.g. to jump forward to the first $ you would do f$

It’s also useful to know how to toggle line numbers on and off when navigating a file:-

:set nu — displays line numbers
:set nu! — removes the line numbers

Other Cheat Sheets

The above commands just cover the basics. If you’re looking for more the following may be worth a look:-

Related Content

Published on


Comments

No one’s commented yet.

Leave a Comment
  • You will need to preview your comment before you can submit it.