Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 2.1 vi Commands Chapter 2
Simple Editing
Next: 2.3 Simple Edits
 

2.2 Moving the Cursor

You may spend only a small amount of time in an editing session adding new text in insert mode; much of the time you will be making edits to existing text.

In command mode you can position the cursor anywhere in the file. Since you begin all basic edits (changing, deleting, and copying text) by placing the cursor at the text that you want to change, you want to be able to move the cursor to that place as quickly as possible.

There are vi commands to move the cursor:

In Figure 2.1, an underscore marks the present cursor position. Circles show movement of the cursor from its current position to the position that would result from various vi commands.

Figure 2.1: Sample movement commands

Figure 2.1

2.2.1 Single Movements

The keys h, j, k, and l, right under your fingertips, will move the cursor:

h

left, one space

j

down, one line

k

up, one line

l

right, one space

You can also use the cursor arrow keys (<-, -v, -^, ->), + and - to go up and down, or the [RETURN] and [BACKSPACE] keys, but they are out of the way, and the arrow keys are not supported by all terminals. At first, it may seem awkward to use letter keys instead of arrows for cursor movement. After a short while, though, you'll find it is one of the things you'll like best about vi -- you can move around without ever taking your fingers off the center of the keyboard.

Before you move the cursor, press [ESC] to make sure that you are in command mode. Use h, j, k, and l to move forward or backward in the file from the current cursor position. When you have gone as far as possible in one direction, you hear a beep and the cursor stops. For example, once you're at the beginning or end of a line, you cannot use h or l to wrap around to the previous or next line; you have to use j or k.[1] Similarly, you cannot move the cursor past a tilde (~) representing a line without text, nor can you move the cursor above the first line of text.

[1] vim version 4.x, and vim version 5.x with nocompatible set, allow you to "space past" the end of the line to the next one with l or the spacebar.

2.2.2 Numeric Arguments

You can precede movement commands with numbers. Figure 2.2 shows how the command 4l moves the cursor four spaces to the right, just as if you had typed l four times (llll).

Figure 2.2: Multiplying commands by numbers

Figure 2.2

The ability to multiply commands gives you more options and power for each command you learn. Keep it in mind as you are introduced to additional commands.

2.2.3 Movement Within a Line

When you saved the file practice, vi displayed a message telling you how many lines are in that file. A line is not necessarily the same length as the visible line (often limited to 80 characters) that appears on the screen. A line is any text entered between newlines. (A newline character is inserted into the file when you press the [RETURN] key in insert mode.) If you type 200 characters before pressing [RETURN], vi regards all 200 characters as a single line (even though those 200 characters visibly take up several lines on the screen).

As we mentioned, vi has an option that allows you to set a distance from the right margin at which vi will automatically insert a newline character. This option is wrapmargin (its abbreviation is wm). You can set a wrapmargin at 10 characters:

:set wm=10

This command doesn't affect lines that you've already typed. We'll talk more about setting options in Chapter 7, Advanced Editing. (This one really couldn't wait!)

If you do not use vi's automatic wrapmargin option, you should break lines with carriage returns to keep the lines of manageable length.

[0] Two useful commands that involve movement within a line are:

0

Move to beginning of line.

$

Move to end of line.

In the example below, line numbers are displayed. (Line numbers can be displayed in vi by using the number option, which is enabled by typing :set nu in command mode. This operation is described in Chapter 7.)

Graphic

[$] The number of logical lines (3) does not correspond to the number of visible lines (6) that you see on the screen. If the cursor were positioned on the d in the word delete, and you entered $, the cursor would move to the period following the word them. If you entered 0, the cursor would move back to the letter m in the word move, at the beginning of line two.

2.2.4 Movement by Text Blocks

[w] You can also move the cursor by blocks of text: words, sentences, paragraphs, etc. The w command moves the cursor forward one word at a time, counting symbols and punctuation as equivalent to words. The line below shows cursor movement by w:

cursor, delete lines, insert characters,

You can also move by word, not counting symbols and punctuation, using the W command. (You can think of this as a "large" or "capital" Word.)

Cursor movement using W looks like this:

cursor, delete lines, insert characters,

To move backward by word, use the b command. Capital B allows you to move backward by word, not counting punctuation.

As mentioned previously, movement commands take numeric arguments; so, with either the w or b commands you can multiply the movement with numbers. 2w moves forward two words; 5B moves back five words, not counting punctuation.

We'll discuss movement by sentences and by paragraphs in Chapter 3, Moving Around in a Hurry. For now, practice using the cursor movement commands that you know, combining them with numeric multipliers.


Previous: 2.1 vi Commands Learning the vi EditorNext: 2.3 Simple Edits
2.1 vi Commands Book Index2.3 Simple Edits

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System