Professional Documents
Culture Documents
Table of Contents
• Starting vi
• Cursor keys
• Word and Line Movement keys
• Number counts
• Cancelling commands
• Status line
• Scroll commands
• Delete commands
• Input/editing commands
• Search
• Copy, paste, undo, repeat
• Ex commands (file commands)
• Search and replace
When you start vi, you are in COMMAND mode. When you add, insert, replace, or
change text, you need to enter INPUT mode. Pressing the ESC (Escape) key at any time,
will return you to the command mode. If you are already in command mode, your
terminal will simply beep you, telling you so.
For the next several sections, we will be in command mode only. We will learn about the
input mode once we know how to navigate around in a file.
Starting vi
To invoke vi with a new (blank) file, simply type "vi" at the prompt:
>> prompt% vi
You should get a screen with the cursor blinking at the top left, and tildes "~" along the
left margin. The tildes indicate that those lines are completely void of any characters
(spaces, tabs, returns included).
You should see your file, along with a status line along the bottom stating the name of the
file, the number of lines, and the number of characters in that file:
To view a file only (you will not be making any changes, or want to prevent any
accidental changes) type "view" followed by the filename:
Cursor Keys
You can move the cursor around using the movement keys. "h" and "l" moves the cursor
left and right; "j" and "k" moves down and up.
(up)
k
(left) h l (right)
j
(down)
NOTE: The arrow keys work in vi as well, but not all terminals may have them. In any
case using hjkl is recommended, since it keeps your hands on the keyboard, and so you
don't have to reposition your hand everytime you move the cursor. Alternative keys:
(up)
-
(left) Backspace (spacebar) (right)
+
(down)
We're still in command mode. To move to the beginning of the next word, type "w". To
move to the beginning of the previous word, type "b". You can also move to the end of
the next word, type "e".
You can also jump to the start and end of lines if you're somewhere in the middle. 0
(zero) or ^ (caret) jumps to the start, "$" jumps to the end.
If you're a fan of run-on sentences, you might want to jump from sentence to sentence.
Use "(" to move to the start of the previous sentence and ")" to move to the start of the
next sentence.
We can jump in even larger units. Using "{" and "}", we can jump to previous and next
paragraphs.
Also, if you hit RETURN at any time, you will move to the beginning of the *next* line
down.
There are also several commands to jump to relative locations on the screen and in the
file:
One other useful command, is "G", which let's you jump to the end of your file:
In the next section we'll see how to jump to any line in a file.
Number Counts
Much of the time even using the word movement keys is too slow. If we could jump any
number of words/lines instead of having to press the keys each time, things would be
much easier. Vi, therefore, provides a way to repeat commands.
>> To repeat command X, n times: nX
Examples:
Number counts can be used with almost all movement and editing commands. As you
know, "G" by itself moves to the last line. To move to the first line of a file, type "1G".
To move to the nth line, type "nG".
Cancelling Commands
If at any time, you want to cancel a number count or any other command before you
finish typing it, you can always hit ESCAPE.
Status Line
Sometimes when you jump around in a file a lot, you lose track of where you are. CTRL-
G will give your current position and the file name you're editing at the bottom of the
screen. It should look something like:
Scroll Commands
When you're simply reading through a file, often times you just want to page through the
text and not make any changes. Having to type "40j" every page would be too tedious.
Fortunately, vi has a set of scroll commands. To scroll down a screen (forward), press
CTRL-f. To scroll back a screen (backwards), press CTRL- b. To scroll up half a screen,
CTRL-u. To scroll down half a screen, CTRL-d.
Delete Commands
To delete a single character, type "x". This deletes the character directly under the cursor
and moves the remaining text to the right one space to the left.
>> To delete a single character at cursor x
We can combine number counts with "x" as well:
Example:
delete 4 characters: 4x
Of course, if you want to delete large sections of text, typing "9999x" is a bit unwieldy.
Fortunately (as you may have guessed), vi has another delete command, "d".
By now, you know how to move an distance in any direction by combining a number
with a movement key. The delete commands work in a similar fashion.
"d" is always used with another command, never by itself. We need to tell it which
direction you want to delete and how far in that direction. We already know how to move
any distance in any direction, right? Now all we do is combine "d" with any movement
key and we've learned almost all the delete commands.
Vi does provide a short cut for d$. You can use "D" to the same effect.
To delete the current line, instead of having to type "^d$" (move to start of line and delete
to the end), vi has a shortcut command, "dd".
Two ways? Which one is right? Vi isn't picky. They're both right. Deleting three words
once (d3w) is the same as delete one word three times (3dw).
Now...what command would you use to delete 5 lines starting from the current line?
The expected way: 5dd Alternate way: 4dj
Why "4dj"? Remember, deletion always occurs relative to the cursor position when we
give it a direction. So "4dj" deletes the current line and the 4 lines below it. Similarly,
"4dk" deletes 5 lines, the current line and the 4 lines above it.
Remember that we always start in command mode. As soon as you type any of the input
commands (except "r" replace) you stay in input mode until you hit ESCAPE. So in order
for you to go back any make changes in what you've typed, just press ESC and move to
where you have to go and enter the appropriate input mode.
Often times you want to type over something instead of having to insert new text and
then delete old text. To do this we use change.
Similarly for "cw", vi will change the last character of the word you intend to change into
"$".
Replace and substitute are fairly similar to change, although with subtle differences in
usage.
However, if you use a number count with x, like "25x", paste *will* insert all 25
characters.
The ex commands are all invoked with the colon, ":". Once you type ":", vi will give you
the ex prompt ":" in the lower left corner.
If you want to overwrite the existing file, then you use :w! filename
Opening files
Often times you'll be editing a series of files. Having to type "vi file1" and exiting
with :wq each time can be a hassle. If you know the name of your other files, you can
open those files from inside vi, by typing ":e file2".