Friday, September 11, 2009

The emacs Editor

The emacs editor is an extremely popular editor that appeared before UNIX was around.
Developers liked it so much that they ported it to the UNIX environment, and now it’s been ported to the Linux environment. The emacs editor started out as a console editor, much like vi, but it has migrated to the graphical world.

The emacs editor still provides the original Console Mode editor, but it now also has the ability to use graphical X Windows to allow editing text in a graphical environment. Normally, when you start the emacs editor from a command line, it’ll determine whether you have an available X Window session. If so, it will start in Graphical mode. If not, it’ll start in Console mode.

Although the emacs editor isn’t installed by default in Ubuntu, you can easily add it to your text editing toolbelt by using the Add/Remove feature in Ubuntu and searching for emacs22. You can also find it listed in both the All and Accessories categories.

After installing the Ubuntu emacs22 package and restarting your system, you’ll see two new entries in the Accessories section of the Applications menu:
• emacs22 (client)
• emacs22 (X11)

The emacs22 client entry starts an emacs session if you have an emacs server running on the system. Because we didn’t install the emacs server, this entry won’t be of any use now.

The emacs22 X11 entry starts a windows-based emacs session for you to use. Alternatively, you can use a console-based emacs session from the Ubuntu command line prompt. This section describes both the Console mode and Graphical mode emacs editors so that you’ll know how to use either one if you want (or need) to.



Using emacs on the Console
The Console mode version of emacs is another editor that uses lots of key commands to perform editing functions. The emacs editor uses key combinations involving the Control key (the Ctrl key on a PC keyboard) and the Meta key. In most PC terminal emulator packages, the Meta key is mapped to the PC’s Alt key. The official emacs documents abbreviates the Ctrl key as C- and the Meta key as M-, Thus, to enter a Ctrl–x key combination, the document shows C-x. I’ll do the same here so as not to confuse you. To avoid this problem you can use the Esc key as the Meta key in emacs. The only difference is that you don’t hold down the Esc key as a key combination. Instead, you press and release the Esc key first, then press the required key to perform the function.



The Basics of emacs
To start a console-based version of emacs, open a Terminal session and, at the command prompt, type

$ emacs -nw mytest.txt

When emacs starts, it displays a basic information and help screen.

Pressing any key displays the contents of the text file. You’ll notice that the top of the
Console Mode window shows a typical menu bar. Unfortunately, you won’t be able to use the menu bar in Console mode. You can use the menu bar only in Graphical mode.

Unlike the vim editor, where you have to move in and out of Insert mode to switch between entering commands and inserting text, the emacs editor has only one mode. If you type a printable character, emacs inserts it at the current cursor position. If you type a command, emacs executes the command.

To move the cursor around the buffer area, you can use the arrow keys, and the Page Up and Page Down keys, assuming emacs detected your terminal emulator correctly. If not, you can use these command keys for moving the cursor around:
• C-p moves up one line (the previous line in the text).
• C-b moves left (back) one character.
• C-f moves right (forward) one character.
• C-n moves down one line (the next line in the text).

There are also commands for making longer jumps with the cursor within the text:
• M-f moves right (forward) to the next word.
• M-b moves left (backward) to the previous word.
• C-a moves to the beginning of the current line.
• C-e moves to the end of the current line.
• M-a moves to the beginning of the current sentence.
• M-e moves to the end of the current sentence.
• M-v moves back one screen of data.
• C-v moves forward one screen of data.
• M-<> moves to the last line of the text.

There are several commands you should know for saving the editor buffer back into the file and exiting emacs:
• C-x C-s saves the current buffer contents to the file.
• C-z exits emacs but keeps it running in your session so you can come back to it.
• C-x C-c exits emacs and stops the program.

You’ll notice that two of these features require two key commands. The C-x command is called the extend command. This provides yet another whole set of commands to work with.

The emacs editor also includes a fancy way of mass-killing text. Just move the cursor to the beginning of the area you want to kill, and hit either the C-@ or C-SPACEBAR keys. Then move the cursor to the end of the area you want to kill and use the C-w command. All of the text between the two locations is killed.

No comments: