In this lecture, we finally create and modify files, a process known as editing.
A file is just a sequence of bytes. Text files are just sequences of characters split into lines.
There are two very popular editors used on Unix systems: vi and emacs. Many people have strong opinions on which editor is best. People will pontificate on the relative merits (or lack thereof) of each editor with an almost religious fervor. I'll just say that which editor you use should be a matter of personal taste.
Early Unix editors were line editors. Files are loaded into memory, edited, then saved back to the filesystem. Editing involved giving commands like: "display lines x to y", "replace line x with the following", or "delete line x". To use a line editor you have to have a mental picture of the layout of the file.
In contrast, vi and emacs display the text of the file on screen and allows you to modify it directly by moving a cursor to where you want to make changes. This is way of editing should be immediately familiar to everyone.
On most Unix systems, vi is the default editor: programs that need you to edit some text—a mail client, for example—will invoke the default editor to do so. In addition, nearly every Unix system ships with a copy of vi, and it may be the only editor available. Consequently, it's worth learning enough about vi to get around, even if you don't use vi on a daily basis. We'll discuss how to change the default editor in the next lecture. Vi has many variants, the most popular of which today is vim (vi improved).
Vi has a steep learning curve, but is small and fast (once you know what you're doing) and ubiquitous. Emacs, another popular editor, is more powerful and easier for new users to pick up, but is slower, and some would say bloated. For instance, emacs has it's own built-in email client, it's own newsreader, it's own web browser, and even it's own built-in psychotherapist.
Vi is a modal editor: the editor is always in one of two modes. In insert mode, you can type in text and have it appear on the screen and in the document. In command mode (sometimes called beep mode by vi's detractors), you can cut and paste text, delete text, even run the text through an external program.
Vi, by default, starts in command mode. In command mode you can navigate through the text using the following keys:
Notice that all four keys are on the home row of the keyboard, making movement fast. On more recent versions of vi, such as vim, you can use the arrow keys to move around. The following keys are also useful:
To insert text, you need to enter insert mode. Position the cursor where you want to insert text and type one the following:
Then type the text you wish to insert. To exit insert mode and return to command mode, hit the <Esc> key.
In command mode, you can also cut, copy, and paste text:
You can prepend any command with a number indicating how many times to repeat the command. For example, 3dd deletes 3 lines; 5yw yanks the next 5 words. You can also repeat insertions. For instance, 72i-<Esc> will insert 72 dashes.
You can also search and replace text with the :s. For example:
The strings to replace are actually regular expressions. We'll discuss these next week.
If you make a mistake, type u to undo the last change. In the original vi, hitting u twice will undo the undo—redoing the original command. In vim, hitting u again will undo the command performed before the command just undone; use Ctrl-r to redo the command.
The following commands deal with files:
When you start emacs on a file, you get full screen display split into a large edit area where the contents of the file appears, a separator line, called the mode line containing information such the file name, current line number, etc., and a one-line area at the bottom of the screen called the minibuffer.
If you type a "normal" key such as a letter, punctuation, a number, it gets inserted at the position of the cursor in the edit area. Other editor commands are performed using various key combinations. The notation for these combinations is:
What's the META key? Some keyboards actually have a META key, but most do not. META is sometimes mapped to the ALT key, but more often is mapped to <Esc>. If META is mapped to <Esc>, to do M-x, press <Esc> and release, then press x.
To move the cursor around, you can sometimes use the arrow keys, but it may depend on your system. Instead, the following keys are used:
Other key combinations that are useful are:
There are also double key combinations. If you press a key prefix such as C-x, emacs will wait for you to press another key to execute the corresponding command. If you change your mind, or you mistype, you can press C-g to get out.
The following key combinations are useful:
In emacs, you can edit more than one file at a time. This introduces the notion of a buffer. A buffer is typically associated with a file you are editing, although there are other ways to get buffers.
Once emacs is started, you will be editing in the scratch buffer You edit a new file by doing:
This creates a new buffer to edit that file. The previous file you were editing is still loaded, but it is hidden. Every buffer has a name. When the buffer is associated with a file, the buffer name is just the file name. To change to another buffer, the following commands are useful:
Everything in emacs is ultimately done by commands, or functions.
At startup, keys are bound to a function. For example, the key C-f is bound to the function forward-char;
the key C-x C-w is bound to the function write-file.
There are many more functions than there are key combinations, and it
is possible to change the function to which any given key is bound.
You can invoke a function by name:
Since this is so common, we use the notation M-x function-name to represent the call of a function function-name from emacs. Note that emacs has function name completion: if you start to type a function name in the minibuffer and then hit TAB, emacs will try to complete the function name or give you a list of possible completions in the buffer.
Some useful functions to know about are:
You can also write your own commands. Unfortunately, you'll have to learn the programming langauge Emacs LISP to do so.
Unix has several other editors available. One popular choice is Pico. Pico is the editor used by the email client Pine. It's easy to use, but not particularly powerful.