Next: Text files and editors:
Up: Introduction to the Linux
Previous: Text files
vi (short for ``visual'') is a very powerful text editor, but
takes a lot of getting used to. It works in two modes: command mode
and edit mode. In command mode you can delete lines,
search (and replace), move to different parts of the file and so
on. In edit mode you can type and delete text. vi has a steep
learning curve because it is not like many other text editors, but
once you get the hang of it you can edit files very quickly.
When you start vi it starts in command mode and will fill the
whole terminal window. If the file you are editting doesn't have
enough lines to fill the window, non-existent lines are indicated by a
tilde ` '.
Probably the most important things you need to know to use vi
are:
- Moving the cursor one space
Originally the cursor was moved by j (down), k (up),
h left and l right. On modern vi variants
you can also move the cursor with the arrow keys.
- Moving the cursor one word
w moves the cursor one word forwards (i.e. right), and b
moves it one word back.
- Deleting a character
x deletes the character under the cursor.
- Replacing a character
r replaces the character under the cursor with whatever
character you type next.
- Deleting a line
dd deletes the entire line the cursor is on.
- Deleting several lines
d3, followed by the enter key, deletes the current line and
the following 3 lines. Of course you can change 3 to any number you
like.
- Switching to edit mode
i allows you to insert text at the current cursor location,
a allows you to append text just after the current cursor
location.
- Switching back to command mode
The escape key will take you back to command mode.
- Searching for a string
Type / followed by the string of characters you want to search
for. When you are finished press the enter key. The cursor will move
to the first occurence of that string. Pressing n will move
the cursor to the next occurence,
- Saving a file
This is a command, and so you must type : first. The colon
will appear on the bottom line. Now type w to write the file
to disk.
- Quitting vi
This is a command, and so you must type : first. The colon
will appear on the bottom line. Now type q to quit
vi. If you haven't written the file to disk since your last
changes it will warn you instead of quitting. Use :wq to
write-and-quit, or q! to quit without saving.
Many computer officers and other experienced Linux users like vi
because it can do a lot with very few key presses, enabling an
experienced vi user to rapidly edit files.
Next: Text files and editors:
Up: Introduction to the Linux
Previous: Text files
Phil Hasnip
2007-08-23