Tuesday, May 18, 2010

Text Editing with ed and vi

Using ed

When you use ed, you work in command mode or text input mode:

  • Command mode is what you get by default. In this mode, anything that you type is interpreted as a command.
  • Text input mode is for type text. You can enter input mode with the commands a (append), c (change), or i (insert). After entering lines of text you can leave input mode by entering a period (.) on a line by itself.
To practice editing a file, copy the /etc/fstab file to you home directory by issuing the following commands:

cd
cp /etc/fstab .

Now you have a file named fstab in you home directory. Type ed -p: fstab to begin editing a file in ed. The editor responds:

878
:

This Expamle uses the -p option to set the prompt to the colon character (:) and opens the fstab file (in the current directory, which is you home directory) for editing.  The ed editor opens the file, reports the number of characters in the file (878), displays the prompt(:), and waits for a command.

When you're editing with ed, make sure that you always turn on a prompt character ( use the -p option). Without the prompt, distinguishing whether ed is in iuput mode or command mode is difficult.

After ed opens a file for editing, the current line is the last line of the file. To see the current line number (the current line is the line to which ed applies you command), use the .= command like this:

:.=
9

This output tells you that the fstab file has nine lines. (Your system's /etc/fstab file may have a different number of lines).

You can use the 1,$p command to see all lines in a file, as the following example shows:

:1,$p
# /etc/fstab: static file system information.
#
# Use 'blkid -o value -s UUID' to print the universally unique identifier
# for a device; this may be used with UUID= as a more robust way to name
# devices that works even if disks are added and removed. See fstab(5).
#
#            
proc            /proc           proc    defaults        0       0
# / was on /dev/sda1 during installation
UUID=2d09f69e-b3b9-4f77-bd34-728bc5d5b97e /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=896133ec-9044-4ea4-a341-e0033120076f none            swap    sw              0       0
/dev/scd1       /media/cdrom0   udf,iso9660 user,noauto,exec,utf8 0       0
/dev/scd0       /media/cdrom1   udf,iso9660 user,noauto,exec,utf8 0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0
:

To go to a specific line, type the line number:

:15

The editor responds by displaying that line:

/dev/scd1       /media/cdrom0   udf,iso9660 user,noauto,exec,utf8 0       0
:

Suppose you want to delete the line that contains cdrom. To search for a string, type a slash(/) followed by the string that you want to locate:

:/cdrom
/dev/scd1       /media/cdrom0   udf,iso9660 user,noauto,exec,utf8 0       0
:

The editor locates the line that contains the string and then displays it. That line becomes the current line.

To delete the current line, use the d command as follows:

:d
:

To replace a string with another, use the s command. To replace cdrom with the string cd, for example, use this command:

:s/cdrom/cd
:

To insert a line in front of the current line, use the i command:

:i
      (type the line you want to insert)
.     (type a single period to indicate you're done)
:

You can enter as many lines as you want.  After the last line, enter a period (.) one a line by itself.

When you're happy with the changes, you can write them to the file with the w command. If you want to save the changes and exit, type wq to perform both steps at the same time:

:wq
857

If you want to quit the editor without saving any changes, use the Q command.




Commonly Used ed Commands
CommandDoes the following
!commandExecutes a shell command.
$Goes to the last line in the buffer
%Applies a command that follows to all lines in the buffer. For example, %p prints all lines.
+Goes to next line
+nGoes to the nth next line (where n is a number you designate)
,Applies a command that follows to all lines in the buffer. (For example, ,p prints all lines.) This is similar to %.
-Goes to the preceding line
-nGoes to the nth previous line
.Refers to the current line in the buffer
/text/Searches forward for the specified text
;Refers to a range of lines---current through last line in the buffer
=Prints the line number
?text?Searches backward for the specified text
^Goes to the preceding line. (See also the - command)
^nGoes to the nth previous line
aAppends the current line
cChanges the specified lines
dDeletes the specified lines
iInserts text before the current line
nGoes to line number n
Press EnterDisplays the next line and makes that line current
qQuits the editor
QQuits the editor without saving changes
r fileReads and inserts the contents of the file after the current line
s/old/new/Replaces an old string with a new one
uUndoes the last command
W fileAppends the contents of the buffer to the end of the specified file
w fileSaves the buffer in the specified file. (If no file is named, it saves in the default file ---- the file whose contents ed is currently editing.

Using vi

The vi editor loads the file into memory and displays the first few lines in a text screen and positions the cursor on the first line.

The last line shows the pathname of the file as well as the number of lines (21) and the number of characters(771) in the file.Later, the last line in the vi display functions as a command entry area. The rest of the lines display the file. If the file contains fewer lines than the screen, vi displays the empty lines with a tilde(~) in the first column.

The current line is marked by the cursor, which appears as a small black rectangle. The cursor appears on top of a character.

When using vi, you work in one of three modes:

  • Visual command mode is what you get by default. In this mode, anyting that you type is interpreted as a command that applies to the line containing the cursor. The vi commands are similar to the ed commands.
  • Colon command mode is for reading or writing files, setting vi options, and quitting vi. All colon commands start with a colon(:). When you enter the colon, vi positions the cursor on the last line and waits for you to type a command. The command takes effect when you press Enter.
  • Text input mode is for typing text. You can enter input mode with the command a (insert after cursor), A (append at end of line), or i (insert after cursor). After entering lines of text, you have to press Esc to leave input mode and re-enter visual command mode.
One problem with all these modes is that you can't easily tell the current mode taht vi is in. You may begin typing only to realize that vi isn't in input mode, which can be frustrating(令人沮丧的).

If you want to make sure that vi is in command mode, just press Esc a few times. (Press Esc more than once doesn't hurt.)

To view online help in vi, type :help while in colon command mode. When you are done with help, type :q to exit the Help screen and return to the file you're editing.
Cursor Movement Commands in vi
KeyDoes the following
Move the cursor one line down
Move the cursor one line up
Move the cursor one character to the left
Move the cursor one character to the right
WMove the cursor one word forward
BMove the cursor one word backward
Ctrl+DMoves down half a screen
Ctrl+UScrolls up half a screen

To search for a string, first type a slash(/). The vi editor displays the slash on the last line of the screen. Type the search string and then press Enter. The vi editor locates the string and positions the cursor at the beginning of that string. 

Commonly Used vi Commands
CommandDoes the following
Insert Text
aInserts text after the cursor
AInserts text at the end of the current line
IInserts text at the beginning of the current line
iInserts text before the cursor
Delete Text
DDeletes up to the end of the current line
ddDeletes the current line
dGDeletes from the current line to the end of the file
dwDeletes from the cursor to the end of the following word
xDeletes the character on which the cursor rests
Change Text
CChanges up to the end of the current line
ccChanges the current line
JJoins the current line with the next one
rxReplaces the character under the cursor with x(where x is any character)
Move Cursor
h or ←Moves one character to the left
j or ↓Moves one line down
k or ↑Moves one line up
LMoves to the end of the screen
l or →Move one character to the right
wMoves to the beginning of the following word
bMoves to the beginning of the previous word
Scroll Text
Ctrl+DScrolls forward by half a screen
Ctrl+UScrolls backward by half a screen
Refresh Screen
Ctrl+LRedraws screen
Cut and Paste Text
yyYanks(copies) current line into an unnamed buffer
PPuts the yanked line above the current line
pPuts the yanked line below the current line
Colon Commands
:!commandExecutes a shell command
:qQuits the editor
:q!Quits without saving changes
:r filenameReads the file and inserts it after the current line
:w filenameWrites a buffer to the file
:wqSaves changes and exits
Search Text
/stringSearches forward for a string
?stringSearches backward for a string
Miscellaneous
uUndoes the last command
EscEnds input mode and enters visual command mode
UUndoes recen changes to the current line

No comments:

Search This Blog