VI Tutorial - Lesson 5 - Positioning on the screen

As explained in the Entering VI section, when the VI editor is called, the opening screen will show the cursor located in the home position. If VI is opening an existing file, you will see the first 20 to 24 lines of the file on the screen. All additional lines copied from the disk will also be in the buffer, but not viewable on the screen.

If the home position is the location from which you want to do your editing, then you are in great shape; unfortunately, most editing is performed at other locations within the file. So, your first course of action is to issue the proper text positioning command to cause the necessary text to appear on the terminal screen. This can be done from command mode by scrolling the screen, using the goto command, or doing a pattern search.

5.1 Scrolling the Screen

To visualize scrolling, imagine that the file is one long, continuous page (like an ancient scroll) and that only a portion of the text is revealed on the screen at any one time. Thus, the only way to see another section of text is to realign the terminal window to the file.

Depending on your point of reference, the user may imagine that the terminal screen floats up and down over a stationary text file, or that the text is pulled up and down with only a portion of it exposed to the stationary terminal screen. Either point of view arrives at the same result.

However, the designers of the VI editor have decided that the screen floats up and down with respect to the file. As a result, the scrolling commands reflect the direction the screen moves past the text. These commands are mnemonic in nature. In other words, there exists a relationship between the command and its meaning. For example, when giving the command "control d", the terminal window moves "downward" toward the end of the file. The command "control u", moves the terminal window "upward" toward the first line of the file. Thus, the window is "pushed" up or down revealing new text that either precedes or follows the current screen.

control b
  scroll "backward" 1 screen
control u
  scroll "up" 1/2 screen
control d
  scroll "down" 1/2 screen
control f
  scroll "forward" 1 screen

 

5.2 Using the GOTO Command

Scrolling through one screen after another in order to reach the portion of the text you want to look at can, at times, be laborious as well as boring. Sometimes it is easier to move the screen window directly to the location you are interested in viewing. If you know the line number or the general area you want to access, repositioning can be accomplished by using the "G" (goto) command. The goto command, preceded by a line number, such as "250G", will position the cursor on the first character space of line number 250. If the requested line is not currently on the screen, the screen will be redrawn with the requested line situated in the window.

Typing "1G" will move the cursor to the first line of the file and redraw the screen if necessary. Not specifying a line number when typing "G", will move the cursor to the last line in the file. It is frequently handy to be able to pop to the beginning or end of a file; so "G" and "1G" are important commands to remember.

Discovering your current line number and overall file size information can be gotten by typing "control g". The editor will then print: the file name, the line number, the number of lines in the file, and the percentage of the way through the file all at the bottom of the screen. Such a line might look like:

 

     "filename" line 17 of 122 -- 13%

Additionally, you can use the :number approach. To go to line 5 type:

     :5

Generally, new users find the "G" command easier to use if all lines are numbered. For more information on how you can have line numbers added to your files, refer to the Miscellaneous Information section.

5.3 Moving by Searching

Another method to reposition yourself within a file is by identifying a word, phrase, or string of characters for the editor to locate. To institute a search, you type a forward slash mark ( / ) followed by a string of characters, terminated by a "RETURN". The cursor will hop to the bottom of the screen where the search command will be echoed. For example, if you wanted to find the word "catnip" in your file, you would type:

 

     /catnip  [RETURN]

The editor will hunt downward from your current position in the file for this pattern and will then place the cursor on the first letter of next occurrence of "catnip". If this is not the "catnip" you are seeking, type in "n" (next), and the editor will continue the search downward. The "n" may be repeated as often as necessary to find the character string sought.

Sometimes you will know the contents of your file well enough to know that the occurrence of "catnip" you want is above your current location. To search upward, type "?catnip" and the editor will search toward line one. The "n" command will repeat this search.

Searching with the " / " or " ? " will ultimately process through the complete file. If you are searching upward and the pattern is not found before reaching the top of the file, the search will wraparound and the seeking will continue from the last line of the file until the pattern is found. The reverse is true when searching downward.

After using the VI editor for a while, some users like to add the upper-case "N" command to their "bag of tricks". The "N" command reverses the direction of the search, i.e. if lower-case "n" is searching downward, upper-case "N" searches upward, and vice-versa.

 

Original Text

The olde smoking fence...
The Purdue "smoking fence" is the iron rail
fence stretching along Grant Street. It was
here that students would congregate for a smoke 
during the old days when smoking was forbidden
on campus. Here they would lean over the fence
and while "off campus" smoke without breaking
the rule.

After "?smok" Command

The olde smoking fence...
The Purdue "smoking fence" is the iron rail
fence stretching along Grant Street. It was
here that students would congregate for a smoke
during the old days when smoking was forbidden
on campus. Here they would lean over the fence
and while "off campus" smoke without breaking
the rule.

After Additional "?smok" Command

The olde smoking fence...
The Purdue "smoking fence" is the iron rail
fence stretching along Grant Street. It was
here that students would congregate for a smoke
during the old days when smoking was forbidden
on campus. Here they would lean over the fence
and while "off campus" smoke without breaking
the rule.

 

If the pattern you have instructed VI to search for is not found throughout the entire file, the editor will respond at the bottom of the screen with:

 

     Pattern not found

Remember, the editor will look per your instructions for an exact duplicate of the character string you have given it. If the editor does not locate your pattern, first check to see if you have mistyped the pattern you requested the editor to find. Note, spaces or absence of spaces can be important. If this is not the case, give thought to the possibility that the object in the file you are searching for is misspelled. In that case, try the pattern search again but this time try using only a portion of the word, such as "tnip" rather than the full word "catnip".


previous | MENU | next

Last modified: 2009/09/01 09:46:26.251000 GMT-4 by christopher.n.deckard.1
Created: 2007/11/06 14:24:30.993000 US/Eastern by brian.r.brinegar.1.

Categories

Search

Type in a few keywords describing what information you are looking for in the text box below.

Admin Options: Edit this Document