This talk will look into one of my favorite tools the Emacs editor.
In this presentation I will use quotes from Wikipedia to illustrate some of the issues. They are an interesting site where the users are creating an on-line encyclopedia.
Lets start with a quick review. If you remember I have commented that configuration files, and many other type of file in a Unix system contain text. This likely stems from the fact that the original authors were programmers. Programmers tend to do a lot of typing creating the source code file and the documentation for programs.
The idea for the emacs editor came from a user who was used to creating macros for an editor called TECO. If you are interested, here is a copy of TEXT EDITOR AND CORRECTOR FOR THE VAX-11, PDP-11, PDP-10, and PDP-8 the original manual for TECO.
Now you might want to know why anyone would want to create macros for an editor. The reason is that many functions we take for granted in an editor today were not present. The idea of search and replace for example, did not exist in TECO.
Here is a little Emacs History from Wikipedia.
From Wikipedia
Emacs began at the Artificial Intelligence Laboratory at MIT. Prior to its introduction, TECO was the default text editor in ITS, the operating system on the AI Lab’s PDP-6 and PDP-10 computers.
Unlike modern text editors, TECO abstracted the task of typing from editing, so that modifying a document required writing a series of instructions in the TECO command language. This behavior is similar to the program ed, which is still in use.
Carl Mikkelsen, one of the hackers at the MIT AI Lab, added a display-editing mode to TECO, which allowed the screen display to be updated each time the user entered a keystroke. This more-intuitive behavior, which is used by most modern text editors, had been pioneered by the “E” editor written at the Stanford AI Lab. In 1974, Richard Stallman, another hacker, added a macro feature to the TECO display-editing mode. This allowed the user to define keystrokes for launching TECO programs to perform various editing tasks, such as reading or writing files.
The users at the AI Lab soon accumulated a large collection of custom macros. In 1976, Guy Steele began an effort to unify the many divergent macro sets. The project was completed by Stallman, who also wrote facilities for extension and self-documentation. The resulting program was called EMACS. Though built on TECO, its behavior was different enough to be considered a text editor in its own right.
This history of emacs point up one interesting fact. The original users wanted an editor which could grow and adapt. Realize that the editor grew out of the idea of adding to an editor. So the resulting design of emacs contained two parts which worked together. The core program is written in C, but inside the core program is a Lisp interpreter. This interpreter is important since it allows the editor to be modified on the fly. Changes made to the lisp portion of the editor are reinterpreted without restarting the program. This allows customizations to happen on the fly. Meaning that you can give the editor different personalities for different files.
The result of this is the ability of the editor to act differently when it is editing a Text file than when it is editing a C source code file. In the editor this is called modes. We will talk more about this later.
Now that we have taken in the history lets have a look at the pieces of the editor and talk a little about how they work.
This is the view presented to the user. Now emacs actually has 2 slightly different views. This comes from the fact that Emacs can be use either in a windows environment or in a terminal. This flexibility is unusual but not unique. The way it works is that on Linux the default display mode is the window mode. But if you start the editor with the option -nw it comes up without it’s own window. What this means is that if you are in a windowing environment, such as X windows, you can start the editor in it’s own window. But if you are telneting into a system and do not have a window interface, you can still use emacs.
From Wikipedia
Emacs stores text in objects called buffers. The user can create new buffers and dismiss unwanted ones, and several buffers can exist at the same time. Most buffers contain text loaded from text files, to be edited by the user. However, buffers can also be used for other purposes; for example, the user may create an empty buffer to store text temporarily.
Each Emacs screen (which is a single window on a windowing system, or the entire screen on a text terminal) can display one or more buffers at a time. If more than one buffer is displayed, the display area is split into multiple sections (which Emacs refers to, confusingly, as windows.) On a windowing system, multiple screens of Emacs can also be created (which Emacs refers to as frames.)
The user issues editing commands by entering keystrokes. In addition to the usual commands for inserting and deleting text, Emacs provides a host of more exotic commands, such as moving the editing point to the next sentence, opening all files in a directory, and performing a search-and-replace operation. Most of these commands can be activated with a combination of keystrokes. Recent versions of Emacs also provide graphical menus for the most common commands. (Most commands, even simple ones such as a
for inserting the character a, are actually calls to Emacs Lisp functions.)
Lets first see what the Emacs editor looks like in a windowing environment:
Now as a comparison, lets see the same view in a Terminal window.
One of the strengths of Emacs is the ability to add and redefine functionality through the use of Lisp code. Since the lisp is interpreted it allows the customizations to be modified within the editor itself.
The emacs editor reads a local configuration file named .emacs when it starts up. This runtime configuration file is typically stored in the user’s home directory. Lets have a look at a default .emacs file for some idea of what the lisp code looks like. Here is the default runtime configuration file that is added to a user’s home directory when a user is added to a Redhat system.
;; Red Hat Linux default .emacs initialization file ; -*- mode: emacs-lisp -*-
;; Set up the keyboard so the delete key on both the regular keyboard
;; and the keypad delete the character under the cursor and to the right
;; under X, instead of the default, backspace behavior.
(global-set-key [delete] 'delete-char)
(global-set-key [kp-delete] 'delete-char)
;; turn on font-lock mode
(global-font-lock-mode t)
;; enable visual feedback on selections
(setq-default transient-mark-mode t)
;; always end a file with a newline
(setq require-final-newline t)
;; stop at the end of the file, not just add lines
(setq next-line-add-newlines nil)
(when window-system
;; enable wheelmouse support by default
(mwheel-install)
;; use extended compound-text coding for X clipboard
(set-selection-coding-system 'compound-text-with-extensions))
The lines which begin with a semicolon are comments. The line which says (global-set-key [delete] ’delete-char) defines the Delete key as delete a character, as opposed to delete-backward-char. Notice that the statement is enclosed in parentheses, which is how lisp defines it’s statements, It starts with the emacs command global-set-key, Next you see the key named Delete set in square brackets. This is the emacs method of specifying a key there are other methods. Next is a single quote specifying the function to run, and the function is named delete-char. One of the interesting aspects to emacs is that it uses a name for every function. The advantage to this is that it allows you to map any operation to a key of your choosing. Now given the number of commands to be mapped it is not uncommon to find commands mapped to double or even triple key combinations. For example the command to open a file is ^Xf, or Control-X, f. When I am typing this presentation the key combination to enter a hyper link is ^C^Al, or Control-C, Control-A, l
The line (global-font-lock-mode t) simply sets the variable global-font-lock-mode to true. That variable is a true/false variable. The line which says when window-system is an if statement which is executed when the system detects that it is running in a windowing environment as opposed to a text terminal. Lets see what Wikipedia has to say about customization of Emacs.
From Wikipedia
The user can *extend* and *customize* the Lisp code from within Emacs
itself. Emacs provides extensive facilities to support this, including
commands for looking up the documentation of loaded functions and
variables (this is known as *self-documentation*.) By re-evaluating the
user-supplied Lisp code, the behavior of Emacs can be modified "on the
fly", without having to recompile or even restart the editor. Lisp is a
good choice of language for this purpose, because it allows functions to
be treated as data.
As a result, it is very easy to add custom functionality to Emacs. Emacs
includes many standard extensions, such as Emacs/W3, a web browser;
Gnus, a tool for reading e-mail and Usenet discussions; an
implementation of the game Tetris; and Doctor, an implementation of
ELIZA that performs basic Rogerian psychotherapy. In addition, Emacs can
serve as an IDE, allowing programmers to edit, compile, and debug their
code from within a single interface.
This customization allows you to create your own operations by either writing Emacs Lisp code or by creating keyboard macros which can be saved and bound to key bindings for later use.
Lets see how to create a keyboard macro for a quick editing task. Now I have bound the F12 key to call-last-kbd-macro to make it easier to re-execute the keyboard macros. Lets suppose I want to find a pair of HTML tags and make the text italics between them. Now they could be on different lines and the text between the tags might be different each time. I would start recording the keyboard macro with ^X ( , next I type ^S to start a search for <p>, when it is found I stop the search, insert a space and <i>, Next I do a ^S to start the search again and find </p> stop the search and move before the tag and insert a </i> finally I stop the keyboard macro with ^X ). OK I have created the macro. To use it again and again, I just press F12, (call-last-kbd-macro), and look at the results. Suppose I wanted to save this for future use. First I name the keyboard macro with the command: ESC-x name-last-kbd-macro followed by the name italicise-text. I can then use the command: ESC-x insert-kbd-macro to write the lisp code for the macro into the current buffer. If that buffer is the .emacs file, it will be reread each time I start emacs. The last step is to bind the macro I created to a key combination using the command (global-set-key \C-ci ’italicise-text) which will bind the macro to ^Ci key combination.
In this section I will try to enumerate some of the features which I find interesting in Emacs. This is in no particular order but these are some of the reasons I use this editor.
Emacs made the decision to give each operation of the editor a name. So move up a line is named: previous-line, delete the last character is named: delete-backward-char. Now this might seem trivial, but it means that the keys which perform a specific operation can be remapped by the user, or the mode. This ability can be seen in the operation above where we created a keyboard macro and then bound it to a key combination.
This is seen in some other editors now but when I started using Emacs, most other editors did not allow this operation. You can split the window vertically, or horizontally. This is similar to the way Word allows you to split the window horizontally.
One interesting feature that is enabled with a split window is the ability to compare the contents of two windows in a diff type operation. I find this very useful for finding differences between sections of files. You can split a single buffer or have different files in the windows.
Emacs had extensive on-line help which predated the Web. What this meant was that they developed a help system which included links inside the emacs editor. This help system is called info and the file are created as texinfo files. The advantage to this system was that it allowed on-line and printed manuals from the same source files.
The info system is so flexible that most of the manuals for the GNU tools today are written in texinfo format. This format is then translated into info pages, or TeX for printed manuals, or HTML for Web pages, or man pages for use with the man system.
As is common today, the F1 key brings up help. But on Emacs is brings up a second menu which asks if you want one of the following:
Again here is what the info pages look like when displayed in the KDE help system
And here is what info looks like in a terminal window.
This is the ability of the editor to change it’s operation to suite a particular type of file. Some of the common modes are for editing source files like C or Perl source. The list of modes is quite long and covers a broad range of editing tasks.
These modes are designed to make it easier to work with a particular type of file. Some of the common attributes which are enabled using Modes are indenting or Coloration. An additional benefit to the Modes is customizing the key bindings. This means that when you are in a particular mode some keys may act differently than when in another mode.
One example of this is that as I type this file the keys Alt-Enter enters the tags <p> </p> and places the cursor between these tags. This is how you start a new paragraph in HTML. This is part of a mode called HTML helper mode.
One feature I find myself using when I compile programs is the ability of Emacs to open a terminal window inside the editor. Lets assume you are all familiar with what a terminal windows is. In Windows we would call this a “Dos Box”, or in a windowing Linux it is just called a terminal. In short this is a windows where you have a prompt from a command interpreter such as command.com or bash respectively.
If you use the command Esc-x (execute-extended-command) you can enter any one of the emacs commands at a command prompt. In this case we will execute the command shell . This starts a command interpreter in our current buffer window.
I know you are asking why run a shell inside an editor. Well some of the reasoning is historical. Imagine, if you will, that you don’t have a windowing system. You do not have command line editing, or history recall. How do you get any work done? The answer, was to startup emacs, and then start a shell inside a buffer window. You now can copy/pasty the last command to the next command line. You can scroll back to see what has scrolled off the page. You can even split the window and edit a file at the same type you are keeping an eye on what is happening in the shell buffer.
To give you an idea how I use this feature. Last week we talked about compiling your own programs from source code. I discussed the program which comes with many software packages named configure . What I typically do is unpack the archive in the /usr/src directory. I then open the Emacs editor and have it view the directory /usr/src. Yes you can do directory listings inside emacs, but we will come back to that. Next I go to the directory containing the source code. I open the document Readme or Install to see how to compile the program. Often it says something like “run ./configure then make then make install”. So I split the screen into 2 buffer windows. Move to the lower window and open a shell inside the buffer with the shell command. Now I can execute the command ./configure inside the shell. If the shell script needs some input during it’s operation, the shell is interactive so I just respond. If there are errors, I run the command ./configure –help to show the options available and rerun ./configure with some options. Once configure runs successfully I execute the make command which compiles the application. Finally I use the su command to become root and execute the command make install. I then exit su since I want to do as little as possible as root. Finally I select the entire buffer contents and save them in a file called make.out. This file is a record of what was done to compile the program. If later I find a problem with the program, I can review the options and errors which happened during the compile.
If you remember the Unix system decided to treat everything as a file. One of the operations of the Emacs editor is called dired mode. This mode is especially useful for making mass changes to a number of files or capturing information from a directory into a file. Here is how the emacs manual describes this mode.
from Emacs Manual
Dired makes an Emacs buffer containing a listing of a directory, and
optionally some of its subdirectories as well. You can use the normal
Emacs commands to move around in this buffer, and special Dired commands
to operate on the files listed.
The Dired buffer is "read-only," and inserting text in it is not useful,
so ordinary printing characters such as d and x are used for special
Dired commands. Some Dired commands *mark* or *flag* the *current file*
(that is, the file on the current line); other commands operate on the
marked files or on the flagged files.
An example of how useful this dired mode is can be seen when I create a playlist of the mp3 files I have downloaded. I open the editor in Dired mode in one part of the window, and navigate to the directory containing the music. I then open a playlist file in the other buffer. Now, I cut and paste the location into the playlist file multiple times. I then use a keyboard macro to copy one by one the names of the files from the dired window into the playlist file. This creates a playlist I can use with several mp3 players.
Today many editors have the ability to undo the last command. But how many can undo all the way back to the beginning of the session. In Emacs it remembers the changes all the way back to the first changes to the buffer. These changes are saved even if you do saves during the session. For example, I often save a buffer of HTML so I can view it inside a browser. But even after these intermediate saves, the undo command will walk back to changes made all the way up to the first changes upon opening the file. It is tied to a session not to the saves.
Similarly if you have undone something which you wanted to keep, you can use the redo command to put it back.
Again, this is a function which has become common in some word processors, but the one in emacs is interesting. First of all the spell checker is not part of emacs but a stand alone program. Emacs just provides an interface to the outside spell checker. This is typical Unix functionality since instead of reinventing something, they use the existing one by providing an interface to it. One interesting feature of this spell checker is it’s ability to work on different type of files. For example I can use it spell check a web page and it knows to skip the HTML tags. I can use it on a TeX file and it will ignore the TeX information because it knows which words are part of the text and which are formatting commands.
One feature, if you want to call it a feature, is the ability to use Emacs on different Operating Systems. Now to be honest, I mostly use Linux and Windows. But I find that I have no trouble going from Emacs on Windows to Emacs on Linux. The editor does have some configuration differences depending on whether I am in Windows or Linux. But for the most part they are interchangeable.
Given that I do a LOT of typing at work, this is especially good for me. I can edit some web page under Linux, and turn around to my windows box and do the same editing operations. This is a big plus to my productivity because I don’t have to remember two different set of editor commands when moving from one environment to another. Now lets be clear, there is some extra work. In windows you use ^C to copy and ^V to paste. Emacs, by default uses Esc-w to copy and ^Y to paste. This becomes obvious when you do a log of copy and paste between windows tools and Emacs. I tend to do a lot of copy/paste operations since it helps me to keep track of what is going on in my testing.
But remember above we said we could change which keys do which operations in Emacs. Lets see how you would change the copy paste commands in Emacs to match those in windows. It would require 4 commands in you .emacs file.
That’s all it takes. Save this in you startup file .emacs and you are done. One note, in windows you need to set an environment variable for HOME so the program knows where to find you .emacs file.
This is a particularly interesting use of Emacs for me. You see I started using Emacs for email under Linux with a email program called MH. This email program is designed to use small commands from the command line. You use one command to view email, another to check if you have mail, another to compose email, etc. This sounds tedious, but remember that I started using this on Text only terminals.
What I found was an emacs mode called mh-e, which gives you a two buffer display of the email. You see the headers in one buffer and read the selected email in the other. The system is good at interfacing with procmail to sort the known email into folders as well. So what I would commonly do was to come home from work, skim through the email and sort any that was not presorted. I would then take all the for that day and create a zip archive of them. The file I would name something like mail.20040308.zip . This file I would then save on floppy disk.
Then, while I sat on the train in the morning on my way to work, where I had no internet connection I would fire up the computer and read the zip file inside emacs. Yes it lets me open the zip file and read and even modify the zip file inside the editor. Sometimes I would find something I wanted to save. I would split the window into 2 buffers, and open a notes file in the other buffer. This gave me a place to paste and information I had read which I wanted to keep for future reference. Since I was in an editor, it was easy to copy something from an email and paste it into a notes file.
You might say that’s easy, but what you are missing is that I was able to do this under DOS. Using dos I was able to simply turn off the computer before I got off the train. Additionally Dos is faster to start and stop. Since it was much simpler. The final addition to the mail program was a module called BBDB, (Big Brother Database). This mode captures the return addresses off the email you receive. Then when I compose an email inside emacs, I type the beginning of the name and press Tab. It finds the email addresses which correspond to the name typed.
There are so many modes we could spend all night discussing them. Here is a list from EmacsWiki
Other pages:
Other functionality specific to some modes:
See also CategoryCode if you want to write a new major mode, or if you are looking for Emacs features that augment the the basic modes listed on this page.
Taken from Wikipedia
The hacker community has a tradition of treating their favorite text
editor with a reverence bordering on religious fanaticism. Many flame
wars have been fought between groups insisting that their editor of
choice is the paragon of editing perfection, and deprecating the
other's. Most participants in these arguments recognize that it is
(mostly) tongue-in-cheek. There are related wars over operating systems
and programming languages, such as the One True Coding Style.
Editor wars are usually fought between the devotees of Emacs and vi, the
two most popular editors on Unix. Most Unix users and programmers use
one or the other of these editors. Many are familiar with both, at least
enough to get around, and so feel they are well-placed to make judgment
calls as to which is "better".
Frequently, at some point in the discussion, someone will point out that
ed is the standard text editor.
The *Church of Emacs* has been formed by Emacs users, to worship Emacs
and discourage the use of vi. It has its own newsgroup,
alt.religion.emacs. Richard Stallman has jokingly declared himself to be
St IGNUcius, a Saint in the Church of Emacs.
vi lovers have created an opposing *Cult of vi*, which some Emacs users
call "clearly a miserable attempt to ape their betters."
Perceived benefits of Emacs
Perceived benefits of vi-like editors
The best answer is to use it. But as to where to start, start with the built in tutorial. If you look above in the help section you will find that F1 t brings you into the tutorial.
If you are interested in starting to learn how to customize emacs I recommend having a look at .emacs . There is lots of good information here on how to setup your very own .emacs file.
OK, so after all this, why do I like the Emacs editor? The answer is probably best summed up by the fact that this web page, and all the web pages I create get written in Emacs. There is probably no other program that I use which gets as much usage as the Emacs, editor.
Another reason I seem to favor Emacs is that I use the keyboard much more than the mouse. I use the mouse when it makes sense or is easier. But I often use the keyboard because I do a lot of typing and find using a control key sequence much faster than picking up the mouse to copy/paste for example.
I am someone who tends to create content rather than consume content. Now don’t get me wrong, I read voraciously on line and else where. But I spend a lot of my time creating content, just like these talks.
Additionally, in case you have not noticed, I like to customize things to the way I want them to work. Not a big surprise why I like Linux where almost everything is customizable. Being able to customize a tool or an environment encourages me to be more involved in how they work, and how I use them.
One interesting note you might find useful. When I started using computer keyboards, it was common to have the control key next to the letter A instead of in the lower left corner. I was annoyed when IBM chose to move the control key are replace it with the caps lock key. I compensated by using keyboards which had the control key in the old location. Which might help explain why I find using control keys preferable to using the mouse. In Linux it is easy to remap the keys since the mapping between the physical keys and the key codes they produce is in a file. What was I saying about liking to customize things. I have found a method of doing the same thing in windows. So far I have modified Windows 95, 98, and 2000 to swap the control key and the caps lock key. If you are interested in learning how to do this on windows 2000, I would recommend having a look at the Windows 2000 Resource Tool Kit for the tool remapkey . Or if you are interested in doing this in Linux I would recommend Keyboard-and-Console-HOWTO. Just remember if you do this and someone else goes to use your keyboard, they will be confused. There are also keyboards which have either already swapped it or enable you to make the swap.
These are only a few of the resources available on the Internet for Emacs. I did a Google search for “Emacs help Online”, it returned 161,000 hits. So just like Linux, there is more help than you can reasonably read. When a question arises in Emacs, Google is your friend.
Written by John F. Moore
Last Revised: Wed Oct 18 11:01:35 EDT 2017