Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
6.  Using the vi Editor Basic vi Commands  Previous   Contents   Next 
   
 

Moving Around in a File

In the previous sections you learned how to create, save, print, and exit a vi file. Now that you have created a file, you'll need to understand the concepts that are required to navigate within it. Open your practice file now, and try each of the commands that are discussed in this section.

Moving the Cursor

When you start vi, the cursor is in the upper left corner of the vi screen. In command mode, you can move the cursor with a number of keyboard commands. Certain letter keys, the arrow keys, and the Return key, Back Space (or Delete) key, and the Space Bar can all be used to move the cursor when you're in command mode.


Note - Most vi commands are case sensitive. The same command typed in lowercase and uppercase characters might have different effects.


Moving With Arrow Keys

If your machine is equipped with arrow keys, try these now. You should be able to move the cursor freely about the screen by using combinations of the up, down, right, and left arrow keys. Notice that you can only move the cursor across already existing text or input spaces.

If you're using vi from a remote terminal, the arrow keys might not work correctly. The arrow key behavior depends on your terminal emulator. If the arrow keys don't work for you, you can use the following substitutes:

  • To move left, press h.

  • To move right, press l.

  • To move down, press j.

  • To move up, press k.

Moving One Word

Press w ("word") to move the cursor to the right one word at a time.

Press b ("back") to move the cursor to the left one word at a time.

Press W or B to move the cursor past the adjacent punctuation to the next or previous blank space.

Press e ("end") to move the cursor to the last character of the current word.

Moving to Start or End of Line

Press ^ to move the cursor to the start of the current line.

Press $ to move the cursor to the end of the current line.

Moving Down One Line

Press the Return key to move the cursor to the beginning of the next line down.

Moving Left

Press the Back Space key to move the cursor one character to the left.

Moving Right

Press the Space Bar to move the cursor one character to the right.

Moving to the Top

Press H ("high") to move the cursor to the top of the screen.

Moving to the Middle

Press M ("middle") to move the cursor to the middle of the screen.

Moving to the Bottom

Press L ("low") to move the cursor to the bottom of the screen.

Paging and Scrolling

If you move down when the cursor is at the bottom of the screen, or move up when the cursor is at the top of the screen, you will see the text scroll up or down. This scrolling can be an effective way to display more text in a short file, but it can be tedious to move this way through a long file.

You can page or scroll backward or forward through a file, a screen or a half-screen at a time. To try out these commands on paint, you might want to add text so you have a longer file to work with.

Note that paging and scrolling are fundamentally different. Scrolling actually scrolls the cursor up or down through the text a line at a time, as though it were on a paper scroll. Paging moves the cursor up or down through the text a screenful at a time. On a fast system, you might not notice the difference. However, if you're working from a remote terminal or in any other situation where your system is running slower than usual, this difference can become painfully apparent.

Page Forward One Screen

To scroll forward (move down) one screenful, press Ctrl-F. (Hold down the Control key and press the F key.) The cursor moves to the upper left corner of the new screen.

Scroll Forward One-Half Screen

To scroll forward one half of a screen, press Ctrl-D.

Page Backward One Screen

To scroll backward (that is., move up) one screenful, press Ctrl-B.

Scroll Backward One-Half Screen

To scroll backward one half of a screen, press Ctrl-U.

Inserting Text

vi provides many commands for inserting text. This section introduces you to the most useful of these commands. Note that each of these commands places vi in entry mode. To use any of these commands, you must first be in command mode. Remember to press Esc to make sure you are in command mode.

Append

Type a (append) to insert text to the right of the cursor. Experiment by moving the cursor anywhere on a line and typing a, followed by the text you want to add. Press Esc when you're finished.

Type A to add text to the end of a line. To see how this command works, position the cursor anywhere on a text line and type A. The cursor moves to the end of the line, where you can type your additions. Press Esc when you are finished.

Insert

Insert text to the left of the cursor by typing i from command mode.

Type I to insert text at the beginning of a line. The command moves the cursor from any position on that line. Press Esc to return to command mode after you type the desired text.

Open Line

Use these commands to open new lines, either above or below the current cursor position.

Type o to open a line below the current cursor position. To experiment, type o followed by a bit of text. You can type several lines of text if you like. Press Esc when you are finished.

Type O to open a line above the current cursor position.

Changing Text

Changing text involves the substitution of one section of text for another. vi has several ways to do this, depending on circumstances.

Changing a Word

To replace a word, position the cursor at the beginning of the word to be replaced. Type cw, followed by the new word. To finish, press Esc.

To change part of a word, place the cursor on the word, to the right of the portion to be saved. Type cw, type the correction, and press Esc.

 
 
 
  Previous   Contents   Next