home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 2.6 Review of Basic vi Commands Chapter 3 Next: 3.2 Movement by Text Blocks
 

3. Moving Around in a Hurry

You will not use vi just to create new files. You'll spend a lot of your time in vi editing existing files. You rarely want to simply open to the first line in the file and move through it line by line. You want to get to a specific place in a file and start work.

All edits begin by moving the cursor to where you want to begin the edit (or, with ex line editor commands, by identifying the line numbers to be edited). This chapter shows you how to think about movement in a variety of ways (by screens, by text, by patterns, or by line numbers). There are many ways to move in vi , since editing speed depends on getting to your destination with only a few keystrokes.

This chapter covers:

  • Movement by screens

  • Movement by text blocks

  • Movement by searches for patterns

  • Movement by line number

3.1 Movement by Screens

When you read a book, you think of "places" in the book by page: the page where you stopped reading or the page number in an index. You don't have this convenience when you're editing files. Some files take up only a few lines, and you can see the whole file at once. But many files have hundreds of lines.

You can think of a file as text on a long roll of paper. The screen is a window of (usually) 24 lines of text on that long roll.

In insert mode, as you fill up the screen with text, you will end up typing on the bottom line of the screen. When you reach the end and press [RETURN] , the top line rolls out of sight, and a blank line appears on the bottom of the screen for new text. This is called scrolling.

In command mode, you can move through a file to see any text in it by scrolling the screen ahead or back. And, since cursor movements can be multiplied by numeric prefixes, you can move quickly to anywhere in your file.

3.1.1 Scrolling the Screen

[ctrl-F] There are vi commands to scroll forward and backward through the file by full and half screens:

^F Scroll forward one screen.
^B Scroll backward one screen.
^D Scroll forward half screen (down).
^U Scroll backward half screen (up).

(In the list of commands above, the ^ symbol represents the [CTRL] key. ^F means to hold down the [CTRL] key and press the f key simultaneously.)

There are also commands to scroll the screen up one line (^E ) and down one line (^Y ). However, these two commands do not send the cursor to the beginning of the line. The cursor remains at the same point in the line as when the command was issued.

3.1.2 Repositioning the Screen with z

[z] If you want to scroll the screen up or down, but you want the cursor to remain on the line where you left it, use the z command.

z [RETURN]

Move current line to top of screen and scroll.

z.

Move current line to center of screen and scroll.

z-

Move current line to bottom of screen and scroll.

With the z command, using a numeric prefix as a multiplier makes no sense. (After all, you would need to reposition the cursor to the top of the screen only once. Repeating the same z command wouldn't move anything.) Instead, z understands a numeric prefix as a line number that it will use in place of the current line. For example, z [RETURN] moves the current line to the top of the screen, but 200z [RETURN] moves line 200 to the top of the screen.

3.1.3 Redrawing the Screen

[ctrl-L] Sometimes while you're editing, messages from your computer system will display on your screen. These messages don't become part of your editing buffer, but they do interfere with your work. When system messages appear on your screen, you need to redisplay, or redraw, the screen.

Whenever you scroll, you redraw part of (or all of) the screen, so you can always get rid of unwanted messages by scrolling them off the screen and then returning to your previous position. But you can also redraw the screen without scrolling, by typing [CTRL-L] .

3.1.4 Movement Within a Screen

[H] You can also keep your current screen, or view of the file, and move around within the screen using:

H

Move to home -- top line on screen.

M

Move to middle line on screen.

L

Move to last line on screen.

n H

Move to n lines below top line.

n L

Move to n lines above last line.

H moves the cursor from anywhere on the screen to the first, or "home," line. M moves to the middle line, L to the last. To move to the line below the first line, use 2H .

Keystrokes Results
L Graphic

Move to the last line of the screen with the L command.

2H Graphic

Move to the second line of the screen with the 2H command. (H alone moves to the top line of the screen.)

3.1.5 Movement by Line

[return] Within the current screen there are also commands to move by line. You've already seen j and k . You can also use:

[RETURN]

Move to first character of next line.

+

Move to first character of next line.

-

Move to first character of previous line.

The above three commands move down or up to the first character of the line, ignoring any spaces or tabs. j and k , by contrast, move the cursor down or up to the first position of a line, even if that position is blank (and assuming that the cursor started at the first position).

3.1.5.1 Movement on the current line

Don't forget that h and l move the cursor to the left and right and that 0 and $ move the cursor to the beginning or end of the line. You can also use:

^

Move to first non-blank character of current line.

n |

Move to column n of current line.

As with the line movement commands above, ^ moves to the first character of the line, ignoring any spaces or tabs. 0 , by contrast, moves to the first position of the line, even if that position is blank.


Previous: 2.6 Review of Basic vi Commands Learning the vi Editor Next: 3.2 Movement by Text Blocks
2.6 Review of Basic vi Commands Book Index 3.2 Movement by Text Blocks

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