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 | |
---|---|
Command | Does the following |
!command | Executes 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 |
+n | Goes 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 |
-n | Goes 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) |
^n | Goes to the nth previous line |
a | Appends the current line |
c | Changes the specified lines |
d | Deletes the specified lines |
i | Inserts text before the current line |
n | Goes to line number n |
Press Enter | Displays the next line and makes that line current |
q | Quits the editor |
Q | Quits the editor without saving changes |
r file | Reads and inserts the contents of the file after the current line |
s/old/new/ | Replaces an old string with a new one |
u | Undoes the last command |
W file | Appends the contents of the buffer to the end of the specified file |
w file | Saves 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 | |
---|---|
Key | Does 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 |
W | Move the cursor one word forward |
B | Move the cursor one word backward |
Ctrl+D | Moves down half a screen |
Ctrl+U | Scrolls 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 | |
---|---|
Command | Does the following |
Insert Text | |
a | Inserts text after the cursor |
A | Inserts text at the end of the current line |
I | Inserts text at the beginning of the current line |
i | Inserts text before the cursor |
Delete Text | |
D | Deletes up to the end of the current line |
dd | Deletes the current line |
dG | Deletes from the current line to the end of the file |
dw | Deletes from the cursor to the end of the following word |
x | Deletes the character on which the cursor rests |
Change Text | |
C | Changes up to the end of the current line |
cc | Changes the current line |
J | Joins the current line with the next one |
rx | Replaces 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 |
L | Moves to the end of the screen |
l or → | Move one character to the right |
w | Moves to the beginning of the following word |
b | Moves to the beginning of the previous word |
Scroll Text | |
Ctrl+D | Scrolls forward by half a screen |
Ctrl+U | Scrolls backward by half a screen |
Refresh Screen | |
Ctrl+L | Redraws screen |
Cut and Paste Text | |
yy | Yanks(copies) current line into an unnamed buffer |
P | Puts the yanked line above the current line |
p | Puts the yanked line below the current line |
Colon Commands | |
:!command | Executes a shell command |
:q | Quits the editor |
:q! | Quits without saving changes |
:r filename | Reads the file and inserts it after the current line |
:w filename | Writes a buffer to the file |
:wq | Saves changes and exits |
Search Text | |
/string | Searches forward for a string |
?string | Searches backward for a string |
Miscellaneous | |
u | Undoes the last command |
Esc | Ends input mode and enters visual command mode |
U | Undoes recen changes to the current line |
No comments:
Post a Comment