The Introduction to Editors briefly introduces the reader to the common Unix text editors and provides links to more information.
Many readers will be familiar with text editors that have graphical user interfaces similar to Notepad from Windows, TextEdit (in unstyled text mode) from Mac OS X, GEdit from GNOME, or KEdit or KWrite from KDE. Other readers will only know about word processors, which are like text editors, but have additional features for applying style and layout to the text. Text editors only deal with sequences of text characters, all in the same font.
The approach of this chapter is to introduce the earliest Unix text editors and progress to the Notepad-style editors. The early editors lack many common features of editors.
The need for a text editor edit
One who uses the command line, but knows not how to use a text editor, can still create text files using the cat tool and the shell redirection feature.
$ cat > newfile We the people, promoting the common keyboard and preserving a more perfect document for all, do ordain and establish three lines of text.
(Note: after entering cat > newfile, the cursor awaits for text to be inputted by the user (in this example: "We the people, [...]"). After all the desired text has been entered, one should type RETURN CONTROL-D to exit the cat tool.)
Suppose there is a want to change this file. In this example, "docmument" has an incorrect spelling; it should be document. Other wants could be revisions to this sentence and the addition of more sentences.
One can append to the file with cat >> newfile, but that leaves the first lines unmodified. One can replace it with cat > newfile again, but that requires retyping the entire file. (Users of graphical user interfaces such as X11 have an advantage: they can use the mouse to copy and paste the text before "docmument", type the correction, then copy and paste the remainder of the text.)
An interesting possibility is if there is a way to delete the extra "m" from "docmument", and to handle more complex tasks like inserting words and rearranging text.
One of the first editors that did this was ed, short for "edit". This has many features of Windows Notepad, but also lacks many. Observe what happens when one starts "ed". Here, "newfile" is the name of the text file to edit.
$ ed newfile 139
A number (here "139") appears. Then the program seems to stop, but no shell prompt appears. This is actually "ed" waiting for commands. On many computers, "ed" actually lacks a prompt.
The "139" indicates that "ed" read in 139 characters that are now ready for editing. We say that the file is open. To be more correct from the perspective of a C programmer, the file was opened and copied into a buffer. The buffer, not the file is open. This means that the disk or storage device will not be bothered until we save the file. Other text editors still follow this behavior, opening files, copying them into buffers, and requiring the user to save to write the file back to disk.
Notice that unlike many editors, "ed" has not yet shown the text of the file. We type a command ,p to do this.
$ ed newfile 139 ,p We the people, promoting the common keyboard and preserving a more perfect document for all, do ordain and establish three lines of text.
The command, roughly translated, is that for every line in the file (","), "p"rint that line to standard output.
Now to do some actual editing, we use a command 2s/docmument/document.
$ ed newfile 139 ,p We the people, promoting the common keyboard and preserving a more perfect document for all, do ordain and establish three lines of text. 2s/docmument/document and preserving a more perfect document for all,
For line "2", "s"ubstitute the first instance of "docmument" with "document". Here "ed" prints the changed line from the buffer.
We run two more commands, "w" to write the buffer back into newfile on disk, and "q" to quit "ed". If we forget "w", then our edit is lost.
$ ed newfile 139 ,p We the people, promoting the common keyboard and preserving a more perfect document for all, do ordain and establish three lines of text. 2s/docmument/document and preserving a more perfect document for all, w 138 q
We made only one minor change, but this already required four commands. Even worse, "ed" error messages are not useful when we mistype commands. Many Unix users never bother to learn "ed". For those readers with interest, this book has a chapter ed and sed (when someone writes that chapter).
ex is a line editor, a successor of ed and an ancestor of vi.
At some point, Unix systems introduced video screens that allow Unix to draw anywhere on the screen. Someone decided that it would be good to create a "visual editor" that allows the user to move the cursor through the file (as it appears with "cat" or the ed "p" command) and make changes. The name of this program is vi, which is short for "VIsual editor". Thus, the namers of this program intended one to call it "vee eye", not "vee" nor "six".
Observe what happens when one starts "vi". First you type the command:
$ vi newfile
Then the screen clears and becomes like this:
We the people, promoting the common keyboard and preserving a more perfect document for all, do ordain and establish three lines of text. ~ ~ ~ ~ newfile: unmodified: line 1
The screen might look different if you have a different version of "vi". Your screen also probably has more than eight lines. However, all versions of "vi" have these two features:
- a "~" shows a nonexistent line (though you could type a line with only "~" to be confusing)
- there is a status line at the bottom, here it reads "newfile: unmodified: line 1"
On many computers, you can use the arrow keys to move the cursor. If that does not work, you can use the standard "vi" keys:
- [h] moves left, [j] moves down, [k] moves up, [l] moves right
For example, one can press [l] for fifteen times to move the cursor from the "W" in "We" to the "p" in "promoting". (In fact, as a shortcut, vi lets one press   [l] (one, five, ell). This gives the number "15" to the [l] command, which in this case means to repeat the command fifteen times.) So it is possible to move the cursor through the file, which was not possible with ed.
But what if we want to type the letter "l" instead of moving the cursor? Press [i], which is a command to switch the vi editor from command mode to insert mode. Then type something. Here, the user, with the cursor at "p" in "promoting", typed [i] and then "nominally " (including one space):
We the people, nominally promoting the common keyboard and preserving a more perfect document for all, do ordain and establish three lines of text. ~ ~ ~ ~
Note that some copies of "vi", such as this one, by default never show at the screen whether the editor is in command or insert mode.
To exit insert mode, press [ESC] escape. To save and quit the editor, type [:] [w] [q] [RETURN] which inputs "wq" to the colon prompt.
To learn more about vi, read the Wikibook, Learning the vi Editor.
In new generation of Linux and Unix operating systems, the more improved version of VI editor was released called vim (version 7 latest). VIM incorporates almost all the features of VI and more, including color coding screen, highlights, and spell check within the document.
emacs was developed at around the same time as vi, and served as a different paradigm to text editing. It allows customization via an in-editor programming language called emacs-LISP, and is a modeless editor (as opposed to vi, a modal editor).
emacs is started much the same way as vi -
$ emacs newfile
It will show a screen resembling the below:
File Edit Options Tools Buffers Help We the people, promoting the common keyboard, and preserving a more perfect document for all, do ordain and establish three lines of text. --- GNU EMACS ----------------------------------
Moving around the file is accomplished with the arrow keys.
Entering text is accomplished by simply typing it, and other basic functions such as deleting are accomplished using the customary keyboard keys.
For example, in order to add the text protecting in place of preserving, simply move the cursor down to the space between the words preserving and a, then press the DELETE key ten times. Then, simply key the word protecting.
To save the file, use the keyboard shortcut CTRL-x CTRL-s, that is hold the control key and press the letter x. Let go of the control key. Then, hold the control key and press the letter s. (To save the file with a new name, then type CTRL-x CTRL-w and emacs will prompt for the name of the new file.) After having saved the file, press CTRL-x CTRL-c to close down emacs and return to the shell.