logo

Beginners' Guide to the vi Text Editor


vi is a rather powerful text editor, found on most UNIX-based systems. Because it's so widespread, it's really handy to know a little bit about it - especially if you are administering a lot of machines, or find yourself on an unfamiliar system.

Unfortunately, vi can be a bit daunting for the new user due to the vast number of commands it understands. Many complete vi references exist, but sometimes it can take a while leafing through pages of information just to find out how to correct a typing error!

The summary below gives a new user just enough information to start using vi successfuly, without getting swamped in the details.

Basic Concepts

The most important thing to know is this:

There are two main modes in vi - command mode, and edit mode.

Always try and remember which mode you're in. This isn't always easy, but it's a good mental exercise. It's a bit like knowing which gear you're in whilst driving a car.

Edit Mode (i)

By default, vi starts up in command mode. There are lots of ways to get into edit mode from command mode. Here are a few of them...

  i            Insert (when you just want to type something)
  r            Replace this character with the next key pressed
  R            As above, but stay in replace mode (for replacing multiple characters)
  o            Start inserting on the next line
  O            Start inserting on the previous line
  A            Start inserting at the end of this line
  I            Start inserting at the beginning of this line

...and there are a few more, but that's enough to be getting along with for now.

Then type away :-)

Command Mode (Esc)

The Esc key brings you to command mode. If you're already in command mode, you may hear a beep or see a "visual bell" - which is just a flash of the screen.

In command mode, you'll be wanting to do things like saving your file, quitting the program, searching for a text string. Stuff like that. Here's a helpful reference list. All commands are case-sensitive.

  :w            Write the file to disk.
  :w filename   Write the file out to disk with a new filename
  :w!           Write, disregarding any warnings
  :q            Quit
  :q!           Quit, disregarding any warnings
  :wq           Write and quit
  :x            Write and quit

Cursor movement (also in command mode):

Arrow keys move the cursor around, as do h, j, k and l.
  
  G             Moves to the last line of the file
  23G           Move to line 23. You'll not see the numbers appear as you
                type them (As you'd expect, this works for other numbers too.
		For instance, 1G is a very useful command)
  $             Move to the end of the current line
  ^             Move to the beginning of the current line
  w             Move forward by one word

And now for some copying and pasting:

  x             Delete the current character
  yy            Copy the current line
  dd            Delete the current line (also copies the line - a bit like
                'cut', but *anything* that's deleted gets copied)
  D             Delete from here to the end of the line
  p             Paste the last copied thing (after the cursor)
  P             Paste the last copied thing (before the cursor)

Some slightly more advanced stuff can be achieved by messing around with the above letters, and sticking some numbers in as well...

eg.

  ddp           Swaps the current line with the next  [cut then paste]
  xp            Swaps the current character with the next  [cut then paste]
  4x            Deletes the next 4 characters
  4dd           Deletes the next 4 lines. This can be v useful for cutting
                and pasting whole blocks of text.
  4yy           Copies the next 4 lines
  dG            Deletes everything from this line to the end of the file
  yG            Copies everything from this line to the end of the file
  p             Pastes that which you last copied or deleted 

As you can see, a lot of the commands are just combinations of other commands.

eg.

  y    copy
  $    move to the end of the line
  y$   copy everything from here to the end of the line

  d    delete
  $    move to the end of the line
  d$   delete everything from here to the end of the line

Confusingly, D also deletes everything to the end of the line (like d$), but Y copies the entire line (like yy). Don't worry too much about this. In vi there is often more than one way to do things.

One last useful command is J. It's much more useful than it looks :-)

  J    Append the next line onto this one.

And that should be everything you need in order to get started. Hope it helps.

Have fun!

S.


Valid XHTML 1.0!

Back to Tech index