vi intro - the cheat sheet method
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Table of Contents
If you're viewing this document online, you can click any of the topics below to link directly to that section.
1. Tutorial tips 2
2. First steps 3
3. Starting out 5
4. Miscellaneous vi 8
5. Insert mode 11
6. Productivity features 14
7. Summary 17
vi intro - the cheat sheet method Page 1
Section 1. Tutorial tips
Should I take this tutorial?
This tutorial shows how to use vi, a powerful visual editor. Using an accelerated "cheat
sheet" method, this tutorial aims to make you a proficient vi user without requiring a
huge time commitment. You'll quickly learn how to move around, edit text, use insert
mode, copy and paste text, and use important vim extensions like visual mode and
multi-window editing.
If you don't know vi or aren't comfortable using it, then you owe it to yourself to take this
tutorial and get up to speed with one of the most popular and powerful Linux/UNIX
visual editing programs.
Navigation
Navigating through the tutorial is easy:
* Use the Next and Previous buttons to move forward and backward through the
tutorial.
* Use the Main menu button to return to the tutorial menu.
* If you'd like to tell us what you think, use the Feedback button.
Getting help
For technical questions about the content of this tutorial, contact the author, Daniel
Robbins, at drobbins@gentoo.org .
Residing in Albuquerque, New Mexico, Daniel Robbins is the President/CEO of Gentoo
Technologies, Inc. , the creator of Gentoo Linux, an advanced Linux for the PC, and
the Portage system, a next-generation ports system for Linux. He has also served as
a contributing author for the Macmillan books Caldera OpenLinux Unleashed, SuSE
Linux Unleashed, and Samba Unleashed. Daniel has been involved with computers in
some fashion since the second grade, when he was first exposed to the Logo
programming language as well as a potentially dangerous dose of Pac Man. This
probably explains why he has since served as a Lead Graphic Artist at SONY
Electronic Publishing/Psygnosis. Daniel enjoys spending time with his wife, Mary,
and his new baby daughter, Hadassah.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
vi intro - the cheat sheet method Page 2
Section 2. First steps
Learning vi -- accelerated
One thing makes it difficult to learn vi -- vi has lots of commands. To use vi effectively,
you need to memorize quite a few. This can take a long time, and one of the goals of
this tutorial is not to take up a lot of your time. So, initially, I have a challenge -- how
exactly can I help you to memorize lots of commands in a short time?
To tackle this challenge, as we proceed through this tutorial, we're going to gradually
put together a vi "cheat sheet". This sheet will contain all the important vi commands.
After you've completed this tutorial, you'll be able to refer to this cheat sheet if you
forget a particular command. Over time, as you memorize commands, you'll gradually
become less dependent on the cheat sheet.
The learning process
In this tutorial, I'm going to use several techniques to help you learn. First, I'm going to
describe how a particular command works, as you'd expect. Then, I'm going to ask you
to try to use the command in vi (for practice), and then I'm going to ask you to
transcribe the command to the cheat sheet (for later reference.) If you want to learn vi
quickly, it's important that you perform all these steps. Trying out a command in vi and
transcribing the command onto your cheat sheet will help you to memorize the
command.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
vi intro - the cheat sheet method Page 3
Introducing vim
There are many versions of vi, and I'm going to be showing you how to use a version of
vi called "vim". vim is very popular and has a number of extensions that make vi a lot
nicer (whenever I demonstrate a vim-specific command, I'll make a note of it.) If you
need to install vim, you can get it from http://www.vim.org . In addition to an enhanced
command-line vi, vim also comes with gvim, a nice graphical editor that you can
configure to use the excellent GTK+ GUI library. Here's a gvim screenshot from my
system:
If you're a vi newbie, try to get gvim running on your system. Using vi from a GUI can
make things a bit easier for beginners.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
vi intro - the cheat sheet method Page 4
Section 3. Starting out
Pick a file
Before using vi to edit files, you need to know how to use vi to move around in a file. vi
has a lot of movement commands, and we're going to take a look at many of them. For
this part of the tutorial, find an unimportant text file and load it into vi by typing:
$ vi myfile.txt
If you have vim installed, type "vim myfile.txt". If you'd prefer to use gvim, type "gvim
myfile.txt". myfile.txt should be the name of a text file on your system.
Inside vi
After vi loads, you should see a part of the text file you loaded on your screen.
Congratulations -- you're in vi! Unlike many editors, when vi starts up, it is in a special
mode called "command mode". This means that if you press "l" on the keyboard,
instead of inserting an "l" into the file at the current cursor position, the cursor will move
one character to the right instead. In command mode, the characters on your keyboard
are used to send commands to vi rather than insert literal characters into the text. One
of the most essential types of commands are movement commands; let's take a look at
some.
Moving in vi, part 1
When in command mode, you can use the h,j,k and l keys to move the cursor left,
down, up and right respectively. If you're using a modern version of vi, you can also
use the arrow keys for this purpose. The h,j,k and l keys are handy because once
you're comfortable with them, you'll be able to move around in the file without moving
your fingers from the home keyboard row. Try using h,j,k and l (and the arrow keys) to
move around in the text file. Try using h until you get to the beginning of a line. Notice
that vi doesn't allow you to "wrap around" to the previous line by hitting h while you're
on the first character. Likewise, you can't "wrap around" to the next line by hitting l at
the end of a line.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
vi intro - the cheat sheet method Page 5