Navigating Using vi


In vi you do all of your navigation within the Terminal window with the keyboard, and almost all of it from command mode.

Think of your file as having a grid of lines and columns , each one a character wide and high. Your basic cursor movements are up, down, left, and right.

Figure 6.8 shows the vi cursor on line 5, column 13.

Figure 6.8. The vi cursor is on line 5, column 13.


To see which line you are on:



  • The status line displays the line number where the cursor is and also indicates whether the file has unsaved changes ("modified") or not ("unmodified") ( Figure 6.9 ).

    Figure 6.9. Pressing displays the cursor position on the vi status line.
     shopping-list: unmodified: line 5 of 12 [41%]  col 13 

Basic cursor movement

Newer versions of vi (including the version included with Mac OS X) allow you to move the cursor by using the arrow keys while in either command or edit mode. But not every version of vi allows this. Some versions support the arrow keys only in command mode, and some do not support the arrow keys at all, regardless of which mode you are in.

vi Cheat Sheets

There are hundreds of vi cheat sheets on the Web. Here are a few in different styles.

vi Editor Cheat Sheet (K Computing; www.kcomputing.com/vi.html). Has a link to an excellent PDF cheat sheet that is very graphically oriented. Highly recommended.

vi Editor Cheat Sheet (University of the Virgin Islands, Center for Administrative Computing; http://cac.uvi.edu/miscfaq/vi-cheat.html). A text-based cheat sheet that covers many commands.

VI Cheat Sheet (Tufts University, Computational Mechanics Studio; http://ase.tufts.edu/mechanical/compstudio/help/vihelp.html). A short, text-based cheat sheet.


Table 6.2 shows commands for moving the cursor that will work in command mode with every version of vi . Also see the sidebar " vi Cheat Sheets."

Table 6.2. Basic vi Moves

M OVE THE C URSOR

C OMMAND

Left one column

h or

Right one column

l or

Up one line

k or

Down one line

j or

To the end of this line

$

To the start of this line

^

Down one screen

(similar to "page down")

Up one screen

(similar to "page up")

Go to line 23 of file

23G

Go to last line of file

G


You do not press after any of the commands shown in Table 6.2. In fact, pressing by itself in command mode is another way to move down one line.

Navigating by searching for text

Another very common way to navigate within a file when using vi is to search for a string of characters , which moves the cursor to the next occurrence of the string. You may search forward (toward the end of the file) or backward (toward the top of the file). In either case, the search will "wrap" around the top or end of the file.

Remember that these commands must be executed from command mode, and that you can make sure you are in command mode by pressing .

To move forward to the next occurrence of a string:

1.
/

The cursor drops to the status line in your Terminal window ( Figure 6.10) .

Figure 6.10. The cursor drops to the bottom line as soon as you type a slash to search from command mode.

2.
Type the string you want to search for.

The search string is case sensitive ( Figure 6.11 ). (The search string in Figure 6.11 is Ea .)

Figure 6.11. Enter a case-sensitive search string, and press .

3.
Press to execute the search.

The cursor moves to the next occurrence of the string, which was actually above where we started searching (note the "wrapped" comment in the status line) ( Figure 6.12 ).

Figure 6.12. Note the comment in the status line "search hit BOTTOM, continuing at TOP."

Tips

  • To repeat the last search and move to the next occurrence of the string, use the n command. (You do not press after typing the n .)

  • To repeat the search in the opposite direction, use N (uppercase N ). (Again, do not press .)


To move backward to the previous occurrence of a string:

  • This is exactly the same as searching forward, except you use ? instead of / .

    Once again, the cursor drops to the status line as soon as you press .



Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net