On most Unix systems, there is a line editor available, called ed or edit. To see if the ed command is available, for instance, you can type:
which edIf you get a response like
ed: Command not foundthen ed is not available on your system, or you haven't set up your path so that it is available. If the ed command is available, then you will instead see something like
/usr/bin/ed
Assuming you are using the ed editor, then you can edit an existing file or create a new one simply by typing
edit filenameYou can also simply open the edit command by typing
editIn that case, the program assumes you are typing information that is to go into a new file which you haven't named yet. (You do that using the w command, and including the name of the file then.)
If you were working on a file using the editor, and your edit session was terminated unexpected, then your changes will be lost. Actually, though, the editor usually saves a copy of your commands. In that case, you may be able to recover your work by typing
edit -r filenameThis will try to restart the editor on the old file, reading in the saved copy of your commands from the interrupted edit session.
When you are done making changes, you would normally terminate by "writing" the changes, (that is, saving them), and then quiting. These two commands can be combined into a single input to the editor:
wqSince the "w" command saves your changes, you can issue it at any time.
wWhen you write your changes, the old version of your current file is replaced. At any time, you can specify that you want the changes to go to a different file by a command like:
w newfileOnce you specify a file in this way, it becomes your current file; that is, if you make some more changes inside the editor, and use the plain "w" command, the updated information will again go to newfile.
When you are editing a file, the editor numbers each line of the file, and places the initial editing position at a new line just after the last line of the input file. The editor always keeps track of the current line you are editing. This line can be referred to by its number, or by the shorthand name ".". In particular, to see the number of the current line, you can type the curious expression:
. =and the editor should respond with something like
line 2946
To print out the text of the current line, you can use the "print" command
pbut if you want to print a particular line, you can specify its number first:
12 pand to print out a range of lines, you specify the first and last, separated by a comma:
10,25 p
To move to a particular line, you can simply type its number.
2946To move to the last line of the file, you can simply type
$because "$" is another name for the last line of the file. For obvious reasons, the name of the first line is just "1"!
Hitting RETURN will move you to the next line of the file, and should also display its content. Thus, you could actually print out the first 10 lines of text of your file by typing "1" to go to line 1, and then hitting RETURN 10 times.
You can also move by using the relative motion operators
+5moves ahead 5 lines, and
-11moves back 11 lines.
You can also move to the next line containing a particular string by typing the string preceded by a slash:
/Fred sayswill find the next line containing the string "Fred says". Now you often want to keep looking for the next line with that same string. To do this, you need only type the slash again!
/
While you are editing the file, there are a variety of commands that allow you to move to specific locations in the file, search for certain patterns, change or substitute items.
You can return to the HTML web page.