VI tutorial - Lesson 8 - Correcting Text

Perfection is difficult to achieve; especially on the first try. It is probable that sooner or later (probably sooner) you will need to change the material you have placed in a file. This section explains how to alter characters, words, and lines by combining the action of an operator (delete or change) with a scope to result in a more powerful command.

8.1 Delete Text (d, D)

The delete command is used in command mode to remove portions of text from the file being edited. The delete command is available for use in either the upper-case "D" format as a command, or in the lower-case "d" format as an operator. The upper-case D" command removes the text on the current line from the cursor to the end of the line. Most people tend to forget the "D" and instead use the "d" with the addition of scopes.

The lower-case "d" operator is very flexible because it can be used in conjunction with scopes to delete characters, words, and lines. The scope must be specified after the delete operator. Some of the most common scopes used with the delete operator shown in the next table.

Delete Operator & Scope Resulting Action
dw delete word forward
db delete word backward
d$ delete from cursor to end of line (same as D)
d0 delete from cursor to beginning of line
dL delete from current line to end of screen
dG delete from current line to end of file
d) delete complete sentence forward
d( delete complete sentence backwards
dd delete complete line

It is important to remember that the current cursor position serves as the reference point for all of the scopes used with the delete operator. Notice that when the cursor is sitting in the middle of a word, the remainder of that word is counted as a scope unit. As with direction, arrow, or scope keys, the combination of "operator-scope" commands can be increased by preceding them with a number.

Thus in the following example, if you type "2dw" with the cursor on the "f" of the word "finished", the result will be that the words "finished" and "this" are deleted. Notice, that if the cursor is sitting midpoint in a word, such as the "h" of "finished", only the remaining portion of that word is removed plus the next word, "this".

Original Line After 2dw command
To be finished this month: To be month:
To be finished this month: To be finishmonth:

It will often be the case that you will want to delete whole lines. This can be accomplished by typing "dd". On most terminals when you delete a line, the editor will erase the line on the screen and replace it with an "@" character. This character symbolizes an empty line, much as the tilde (~) is used, and is not inserted into the text. This is done to save the cpu (central processing unit) the time necessary to repeatedly redraw the screen. If the "@" signs makes it difficult for you to read your file, type "control r" (some terminals require "control l") and the screen will be redrawn. For more information on redrawing the screen see the Miscellaneous Information section. As with any operator-scope combination, you can also delete more than one line at a time by preceding the "dd" command with a number. Typing "2dd" will delete two consecutive lines beginning with the current line. In the next example, if the cursor was sitting on the second line of a five line file and the command "2dd" is issued, you would see two "@" symbols replace the deleted lines and these symbols would disappear when the screen was redrawn.

Original file To do this month: a. see dentist b. buy gift c. 15 page paper d. start project
After 2dd issued To do this month: @ @ c. 15 page paper d. start project
After file redrawn To do this month: c. 15 page paper d. start project    

8.2 Change Text (c, C)

The change command is used to replace portions of the text in the file being edited with new material that is keyed in. The change operator differs from the delete operator in that the text input mode is activated when issued. When you have completed keying in the new material to replace the identified changed text, you are required to press the "ESC" key to return to command mode.

The "c" operator can be combined with the same scopes used with the delete operator, thus providing flexibility to change characters, words, and lines. Some of the most common scopes used with the change operator are:

Change Operator & Scope Resulting Action
cw change word forward
cb change word backward
c$ change from cursor to end of line (same as C)
c0 change from cursor to beginning of line
cL change from current line to end of screen
cG change from current line to end of file
c) change from cursor to sentence start
c( change from cursor to sentence end
cc change complete line

 

It is important to remember that the current cursor position serves as the reference point for all of the scopes used with the change operator. Experimentation with scopes will show you that when the cursor is sitting in the middle of a word, the remainder of that word is counted as a scope unit. For example, if you use the command "cw" and the cursor is sitting midpoint in a word, only the remaining portion of that word is changed; while if the cursor is sitting at the beginning of a word, the entire word is changed. Careful positioning of the cursor permits you to change a prefix and suffix without altering the rest of the word. Any of the operator-scope commands can be repeated by preceding them with a number. Typing "2cw" will replace the next two words with whatever is keyed in before the "ESC" is pressed.

To help you visualize the extent of the change that is going to take place, VI places a marker at the termination point of the forthcoming change command. This marker is a "$" and it overwrites the last character that is to be changed. Then as you type in the replacement text, the text on that line will begin to be overwritten. If the replacement text has fewer characters than the material being replaced, then the remaining old text between the final replacement character and the "$" maker will disappear when the "ESC" key is depressed. If the replacement text has more characters than the old text, then the new material will seem to overwrite the old text beyond the "$" marker. This is only temporary, and when the "ESC" key is pressed all the characters that were originally beyond the "$" marker will reappear.

Original Line 2cw Issued New Text After <ESC>
write the resume write th$ resume make make resume
write the resume write th$ resume draft new wrdraft new resume
write the resume write th$ resume ing sample writing sample resume

 

This can be done with the "cc". command. Immediately after giving the line change command, the editor will erase the current line and leave it blank and await replacement text. You may then proceed to key in a small amount of text or many paragraphs. You can change multiple text lines by preceding the "cc" command with a number. Thus typing "9cc" will change nine consecutive lines beginning with the current line. When you specify more than one line to be changed, the editor will immediately delete all the specified lines. On the screen the first line will be blanked with the cursor sitting on the first character space of that line and all remaining lines to be replaced with "@" symbols.

Examine the sample text below where two lines are being changed with "2cc".

Original file To do this month: a. see dentist b. buy gift c. 15 page paper d. start project
After 2cc Issued To do this month: a. see dentist   @ d. start project
Replacement Text To do this month: a. see dentist b. select classes @ d. start project
File Redrawn To do this month: a. see dentist b. select classes d. start project  

The upper-case "C" command changes the text on the current line from the cursor to the end of the line as does the command "c$". Many users disregard the "C" command and only remember the the lower-case "c$" command using the same format as previously learned for delete.

8.3 Replace Command (r, R)

The replace command is used to replace portions of text on the screen with new characters in an overlay fashion. The lower-case "r" command replaces the single character with the new character you key in. To accomplish this exchange, move the cursor so it sits upon the character to be replaced, press the "r" key, then press the key you want to see on the screen. VI will make the exchange. The editor knows when you use the "r" key that only one character will be replaced; therefore, you remain in command mode throughout the action.

Original Text The rat flew.
Press Replacement Character "b" The rat flew.
Locate Cursor and Press "r" The bat flew.

 

The upper-case "R" command replaces characters on the screen one at a time with characters you type in. Once you type "R", text input mode is activated permitting you to enter as much text as you want. It is important to remember that for each character you key in one is removed from the file until the end on the current line is reached; thereafter, any additional text will be inserted between the current line and the line immediately below it. This way you can actually replace the end of one line with several lines of new text. To stop removing and replacing text you must press the "ESC" key.

If you were to edit the next example using the "R" to replace the word "classes" with "program" followed by a "RETURN" and then continue keying in the extra line "c. do speech" and "ESC", the screen would look like the next table.

Original file To do this month: a. see dentist b. select classes c. start project d.order tickets  
During Replacement To do this month: a. see dentist b. select program c. do speechject d.order tickets  
After <ESC> Pressed To do this month: a. see dentist b. select program c. do speech c. start project d.order tickets

 

8.4 Erase Command (x, X)

The erase command (also known locally as the "gobble" command) makes a character vanish. The upper-case "X" will cause the character to the left of the cursor to disappear. At the same time, the cursor and the remainder of the line move one character space to the left. The lower-case "x" command will erase or "eat" the character the cursor is sitting on. As with many VI commands, users will frequently elect to remember only one option. With the erase command it is usually the lower-case "x", because the cursor is highlighting the character that will disappear; plus, the lower-case "x" is easier to type because it does not require using the shift key.

Original Text Purdue's Slayter Center was built in 1963.
Line before lower-case "x" command issued Purdue's Slayter Center was built in 1963.
As text appears after the "s" character is erased and the cursor and the rest of the line moves left one space Purdue's layter Center was built in 1963.

 

Original Text Purdue's Slayter Center was built in 1963.
Line before upper-case "X" command issued Purdue's Slayter Center was built in 1963.
As text appears after the "s" character is erased and the cursor and the rest of the line moves left one space Purdue's layter Center was built in 1963.

 

The erase command is wonderful, but unfortunately, it is easy to get into trouble using it. What happens is a user places the cursor on a line where a series of characters is to be removed and then proceeds to eliminate the unwanted characters by constantly holding down the "x". This is logical but, unbeknownst to the user, instructions to erase are being speedily sent (for example, ten instructions per second) to the editor while the editor erases characters on the screen at a slower pace (maybe five instructions per second). The user then sees the intended last character disappear and the erase key is released. Of course, the editor by now has a backlog of erase commands and to the consternation of the user proceeds to "gobble" across the line. To avoid this problem, tap the erase key rather than holding it down. Oh yes, the good news is that "gobble" will only "eat" one screen width of information. If ever this happens to you, you will be very grateful for the last command in this section the "U" (undo).

8.5 Transposition Command (xp)

Almost every UNIX users will have days, sooner or later, where for some reason their "brain-hand" coordination short circuits and they spell familiar words in new and unique ways. Words like "the" appear on the screen "teh" and "their" becomes "thier". Transpositions occur when our fingers mix up the proper order of letters within a word even when the correct spelling is known. Correcting this common error is easy with the "xp" (transposition) command.

To use "xp", locate the cursor on the leftmost letter of the two letter combination, press "x" to erase the left character. As this action is being completed, the remainder of the line will move one character space to the left. The cursor will now be sitting on the character that had been to the right of the erased character. Pressing the "p" will cause the deleted character to be put to the right of the cursor. Like magic without referencing either of the effected characters, simply moving the cursor to the proper location and typing the "xp" combination results in the two characters reversing order.

Original Text Pudrue once ran the world's shortest railroad (1 1/4 miles).
Text after "x" command issued and "d" is erased Purue once ran the world's shortest railroad (1 1/4 miles).
Line after the "p" command issued, the "d" is put and cursor moves right one character space Purdue once ran the world's shortest railroad (1 1/4 miles).

 

8.6 Undo Command (u, U)

The undo command is used to reverse or undo the effects of an already issued command that has changed the buffer. The lower-case "u" (undo) command undoes the effects of the last command (only one) that you issued.

After you make a number of changes to a single line, you may decide that you would rather have the original state of the line back. The upper-case "U" undoes all of the changes that you have made to the current line. However, it does not remember changes you might have made to that line on previous visits. For example, if you make changes to line 12, move elsewhere, then return to line 12 and make more changes, the "U" will only undo those changes made on the current visit to the line.

8.7 Replacing all ^M 's in a file

To replace the ^M 's on the screen in vi type

:%s/\r//

What that says is: for lines 1 to end, replace \r with nothing . This will replace only one instance of ^M per line. If you have two on a line for some reason simply run the command a second time.

8.8 Global search and Replace

For a general global replace command use:

:%s/oldText/newText/g

This will replace all instances of 'oldText' with 'newText' throughout the file.

 


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