Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
6.  Using the vi Editor Using ex Commands Turning Line Numbers On and Off  Previous   Contents   Next 
   
 

Copying Lines

The basic form of the ex copy command is:

:line#,line# co line#

The first two numbers (separated by a comma) specify the range of lines to be copied. The third number is the line before the insertion point.

For example, to copy lines 1 through 5 of paint and place the copy after line 12, you would type the following:

:1,5 co 12

Then press Return.

When you specify line ranges, use the abbreviations:

  • Period (.) to denote "from the current line."

  • Dollar sign ($) to denote "to end of file."

Thus, to copy the range "from the current line through line 5" and insert this block after line 12, you would type:

:.,5 co 12

To copy the range "from line 6 through the end of the file" and insert this block after line 2, you would type:

:6,$ co 2

Moving Lines

The basic form of the ex move command is similar to the copy command that is discussed in the previous section.

:line#,line# m line#

Line ranges and insertion points are specified in the same ways, including use of the abbreviations . and $. The difference in function is simply that "move" removes a block from one location and reinserts it elsewhere.

For example, to move lines 1 through 5 to the line following 12, you would type:

:1,5 m 12

Then press Return.

Deleting Lines

To delete a range of lines, use the command form:

:line#,line# d

For example, to delete lines 1 through 5, you would type:

:1,5 d

Searching and Replacing With vi

vi provides several ways to find your place in a file by locating a specified string of characters. vi also has a powerful global replacement function.

Finding a Character String

A character string is one or more characters in succession. A string might include letters, numbers, punctuation, special characters, blank spaces, tabs, or carriage returns. A string can be a grammatical word or it can be part of a word.

To find a character string, type / followed by the string you want to search for, and then press Return. vi positions the cursor at the next occurrence of the string. For example, to find the string "meta," type /meta followed by Return.

Type n to go to the next occurrence of the string. Type N to go to the previous occurrence.

To search backward in a file, you can use ? instead of /. In this situation, the directions of n and N are reversed.

Searches normally are case sensitive: a search for "china" will not find "China." If you want vi to ignore case during a search, type :set ic. To change it back to the default, case-sensitive mode, type :set noic.

If vi finds the requested string, the cursor stops at its first occurrence. If the string is not found, vi displays Pattern not found on the last line of the screen.

Certain special characters ( / & ! . ^ * $ \ ?) have special significance to the search process and must be "escaped" when they are used in a search. To escape a special character, precede it with a backslash (\). For example, to search for the string "anything?" type /anything\? and press Return.

You can use these special characters as commands to the search function. If you want to search for a string that includes one or more of these characters, you must precede the special character with a backslash. To escape a backslash itself, type \\.

Refining the Search

You can make searches more precise by tagging the string with indicators for the following characteristics:

  • Beginning of line

  • End of line

  • Beginning of word

  • End of word

  • Wildcard characters

To match the beginning of a line, start the search string with a caret (^). For example, to find the next line beginning with "Search", type:

/^Search

To match the end of a line, end the search string with a dollar sign ($). For example, to find the next line ending with "search.", type:

/search\.$

Note that the period is escaped with a backslash.

To match the beginning of a word, type \< at the beginning of the string; to match the end of a word, type \> at the end of the string. Thus, to match a word, rather than a string, combine the end-of-word and beginning-of-word tags in the search pattern. For example, to find the next occurrence of the word--as opposed to the string--"search", type:

/\<search\>

To match any character, type a period (.) in the string at the location to be matched. For example, to find the next occurrence of "disinformation" or "misinformation," type:

/.isinformation

Because this is a search for a string and not a word, this search pattern might also find such constructions as "misinformationalist" and "disinformationism."

To search for alternative characters in a string, enclose the alternatives in brackets. The search pattern /[md]stringfinds strings that begin with either "m" or "d." Conversely, /[d-m]string finds strings that begin with any letter from "d" through "m."

To match zero or more occurrences of the last character, type an asterisk (*) in the string. You can effectively combine brackets and the asterisk to look for well-defined alternatives. For example, to find all strings beginning with a through z and ending with "isinformation" and to find all occurrences of the string "isinformation", type:

/[a-z]*isinformation

Replacing a Character String

The procedure for replacing a text string is based on the search procedures that are discussed previously. You can use all the special matching characters for searches in search-and-replace.

The basic command form is:

:g/search-string/s//replace-string/g

Then press the Return key.

Therefore, to replace every occurrence of the string "disinformation" with "newspeak," type:

:g/disinformation/s//newspeak/g

Then and press Return.

You can modify this command to halt the search and make vi query whether you want to make the replacement in each instance. The following command uses gc (adding c for "consult") to make vi stop at every occurrence of "disinformation" and ask whether you want to make the substitution. Respond with y for yes or n for no.

:g/disinformation/s//newspeak/gc

Note - You can cancel a "consulted" search-and-replace function by pressing Ctrl-C.


 
 
 
  Previous   Contents   Next