Originally Published: Monday, 11 September 2000 Author: Jim Jones
Published to: learn_articles_firststep/General Page: 1/1 - [Std View]

Easing Into Editors: emacs

We are going to be discussing the emacs editor. While we learned that the vim editor is primarily concerned with unadorned text files, the emacs editor is capable of a lot more than simple text file editing. Emacs can do virtually anything that you would want to do on a computer without having launch a separate program (I am sure that someone has even customized a "butter your toast" meta-function). It is that powerful! It can check, display, and manage your email, and keep you up to date on the latest network newsgroup postings. You can also use it to issue commands to the command shell, compile and debug programs, and play games without ever having to leave the editor's workspace.

Introduction

In the last Linux editor article, Easing Into Editors: vim, we took a look at some of the basics of using one of the "standard" text editors, vim, that is available by default on most Linux/Unix systems. We started off by discussing the fact that a lot of the work done in Linux is done in an editor of some sort. This makes it imperative that users jump into learning how to use those editors with both feet. We also learned that, despite appearances, vim is relatively straightforward, and after some practice, becomes quite easy to use while remaining a very powerful text editor. Working with text files is its sole job, and it does it very well.

In this article, we are going to be discussing the emacs editor. While we learned that the vim editor is primarily concerned with unadorned text files, the emacs editor is capable of a lot more than simple text file editing. Emacs can do virtually anything that you would want to do on a computer without having launch a separate program (I am sure that someone has even customized a "butter your toast" meta-function). It is that powerful! It can check, display, and manage your email, and keep you up to date on the latest network newsgroup postings. You can also use it to issue commands to the command shell, compile and debug programs, and play games without ever having to leave the editor's workspace.

Because of all of this functionality and versatility, mastering the emacs editor can be akin to mastering calculus or trigonometry. It is not impossible, but it can take a lot of time and trial and error. However, if in the end, you want something that can do it all (maybe you want to slip the surly bonds of straight text editing), then emacs is just what you ordered.

For the purposes of this article and to make it easier on ourselves, we will just be focusing primarily on editing simple files (such as quick notes or Linux configuration files). Once you have mastered that, you can start out on the never-ending quest towards mastering all of emacs' other parts, functions and intricacies.

A Brief History...

Emacs originally stood for Editor MACroS. Back then, it was just a bunch of editor commands and macros that were strung together for another less functional text editor of the time. It seems like everyone has since tried to make a more sensible acronym of emacs, including Eight Megabytes And Constantly Swapping or, in a nod to emacs' "keyboard kung fu," ESCAPE META ALT CONTROL SHIFT. Since then, emacs evolved into a software project all of its own with Richard Stallman (the founder of the Free Software Foundation, to whom Linux owes its development tool roots) at its helm. Emacs began its evolution before the X window system as Stallman's attempt to create a "windowed" environment and workspace. It is now in its 19th (plus) version and can handle tasks ranging from editing text files to compiling programs and reading newsgroups. Written in the C language (for portability between systems), emacs has a variation of the LISP computer language interpreter at its core that allows it to be extended in almost any way imaginable. In the end, if you don't like the way it works, you can invest a little time and make it work the way you'd like.

How emacs Compares With vim

Since we started with the vim editor, we should show how they differ from each other. As we have already discussed, the emacs editor is "feature rich", to say the least. It handles text in as skillful a manner as the vim editor, but also includes programmed facilities for working with text layout via the TeX/LaTeX layout tool set. Both editors are visually based, versus command-line, allowing for file edits to occur with a knowledge of their context.

One of the main differences between the editors is the concept of modes. As we discussed in the first article, vim works in a number of modes depending on whether you are adding or manipulating text. Generally, users switch between modes with the Esc Key and, depending on the mode, each key performs different tasks on the contents of the file. The emacs editor, in contrast, is modeless. So, instead of having to remember to switch modes in order to affect some magnificent change, commands for such operations are enacted through some special key combination (some of which can be quite complex... hence "keyboard kung fu"), while printable characters are entered simply through typing normally. This is very natural for people used to using shortcut keys in most modern word processors.

The other major difference of the emacs editor is its inclusion of a LISP interpreter into its repertoire of tools, which allow for it to be molded in any way that the end user sees fit. We won't go into this, but there are detailed help files available at the Free Software Foundation that can guide you through the process, should you be so inclined later.

That should do it for background basics and comparisons, let's take a look at how to do some work with emacs.

Starting the emacs Editor

Generally, in a command shell, the syntax for starting up the emacs editor to work with a new file (or for working with a file already in existence is:

$ emacs filename

However, if you are working in the X windows system, and you only want to work with the terminal based version of emacs, you'll need it tell it to ignore the X windows system with the following command line switch (or option):

$ emacs -nw filename

For our purposes, we will be working with a file named fstep.emacs, so we type:

$ emacs -nw fstep.emacs

This will open the emacs editor for working with the fstep.emacs file. You should see something that looks a little something like this:

Buffers Files Tools Edit Search Mule Help -------------------------------------------------

...cut for space...

----:---F1 fstep.emacs (Fundamental)--L1---All----------- (New file)

Other emacs startup options include: starting emacs without your customized .emacs file ($ emacs -q fstep.emacs), which is useful when learning emacs or working through examples in a book which probably assume a default configuration; starting the editor with your .emacs customizations ($ emacs -u username fstep.emacs) which assures that your customizations, or those of someone else's that you have access to, are in effect; and starting emacs at a specific line in the file that you are opening ($ emacs +n fstep.emacs).

Now let's look at adding some text to fstep.emacs.

Adding Text in emacs:

Let's add the party line definition of Linux to our file. We'll again borrow (for consistency) the initial definition from the FirstStep section of Linux.com (http://www.linux.com/firststep/#whatislinux).In case you didn't see it last time, here's what it says: Linux, the most popular Unix variant for PCs, is the only operating system not from Microsoft that is gaining significant market share. Created by Linus Torvalds and developed collaboratively over the Internet, Linux is the great success story of Open Source software development.

Inserting text into a file with the emacs editor is a simple matter of typing at any time. Since the editor is modeless it does not require you to enter a separate mode before you can enter text. Just start typing. It is pretty much that simple. One thing to note is that you should use the Delete key to backspace and remove a miss typed character as the default keybinding (the key combination that is linked to a single key on the keyboard) for the Backspace key is Control + h, which is the keyboard combination for the emacs help system. The Delete key is bound to Control + d, the emacs delete keyboard combination. While you could enter the text by hand in this case, it would be quicker to "mark" (or "highlight") the definition above and then use the X windows mouse paste operation (use the middle mouse button on a three button mouse or click the left and right mouse buttons at the same time) in the window that has your emacs session going. You should end up with something that looks a little like this:

Buffers Files Tools Edit Search Mule Help -------------------------------------------------------------- Linux, the most popular Unix variant for PCs, is the only operating system not from Microsoft that is gaining significant market share. Created by Linus Torvalds and developed collaboratively over the Internet, Linux is the great success story of Open Source software development.| |

...cut for space...

----:---F1 fstep.emacs (Fundamental)--L5---All------------------------ (New file)

That should have been pretty easy. At least we didn't have to worry about which mode we were in that time. Before we do anything else, let's save these changes to the file. Again, since emacs doesn't have modes, there is no need to do anything other than issuing the editors save command, which is the Control + x followed by Control + s keyboard combination.

Now it's time to look at the tools we will need to actually make changes to the files that we have created. In order to edit the file, we are going to have to learn how to maneuver around inside of it until we get to the position where the edit needs to occur. That's what we'll cover next.

Moving the Cursor in emacs

Basic cursor movement in the emacs editor is just as straightforward as in the vim editor, if not more so since you don't have to switch modes to do it. You can move anywhere in your file by using the UP, DOWN, LEFT, and RIGHT arrow keys on the keyboard to move one character at a time in each respective direction. The keyboard combos for the same movements include Control + p for up, Control + n for down, Control + b for left, and Control + f for right. Small edits on relatively small documents can pretty well be served with these movement increments, but will get old fast if having to move from the first line to the one hundred twenty fifth, one line at a time.

Larger documents may require a lot more work and single space cursor movements are going to wear your keyboard out in a hurry. For these types of situations, there is a broader set of movement tools. Emacs includes facilities for moving forward or backward on word at a time (ALT + f and ALT + b, respectively), forward and backward one sentence at a time (ALT + e and ALT + a, respectively) and the same for paragraphs (ALT + { and ALT + }, respectively). You can also move ahead and back one screen of information at a time (Control + v and ALT + V, respectively), re-center the screen at the current position of the cursor, and search forward or backward in the file for a particular text pattern (CONTROL + s + RETURN and CONTROL + r + RETURN). Of course, there are many (in the case of emacs, many many) more options and commands to make use of in the editor's environment, but these few should serve you pretty well until you have had time to study emacs' many paths, (Grasshopper).

Let's cover a few commands for cutting, pasting and removing text, and then we'll apply what we've learned to fstep.emacs.

Some Editing Commands for emacs:

Many people have worked with word processing programs at least once or twice in the past (if not, don't worry) and may have become accustomed to cutting, copying and pasting text in and about word processed documents. The keyboard combinations for those functions in most of those word processors are CONTROL + x to cut text, CONTROL + c to copy text, and CONTROL + v to paste it back in. As with vim, the keystrokes are different but the concepts are somewhat similar and pretty easy to get the hang of with some practice and patience.

To cut or copy text in emacs, you first have to "mark" the text that you want to manipulate. To "mark" the text that your want to work with, move the cursor to the beginning of the text block and type CONTROL + @ (CONTROL + SHIFT + 2). Next move the cursor to the end of the text block that you want to work with. Now, you have a choice. If you'd like to "cut" the text (copy the text block into an "invisible" workspace for later use while removing the orginal block), type CONTROL + w. If you'd like to "copy" the text (make a copy of the text block in an "invisible" workspace for later use while leavin the original text block), just type ALT + w.

To place the text block that you have copied or cut, just use your cursor movement commands to get the cursor into the place where you want the text to go. Once there, simply type CONTROL + y.

Emacs offers a number of tools for manipulating the text of a file in a number of ways. There are a number of keyboard combinations that facilitate the deletion of text from the editor. To delete the character before the cursor, you can use the Delete or the CONTROL + d keyboard combination. The ALT + d keyboard combination will delete the current word at the cursor's position. Typing CONTROL + k will delete the text from the position of the cursor to the end of the line, which can be useful in medium-sized edits without having to go overboard. Sometimes, editing a line in mid paragraph in linux can set things out of whack as the word wraps get put off due to the new text involved with the edit. Emacs comes to the rescue by allowing you to reformat the current paragraph to allow for these changes be "resetting" the word wrap, making sure each line is "full," with the ALT + q combination.

If your typing has ever got ahead or behind your thinking, you've surely run into the situation of simple transposition typos where two letters or two words are in the wrong order. This is common enough that emacs offers two commands to take care of this. CONTROL + t will transpose the two characters before the cursor, and ALT + t will transpose two words. To delete an entire word at a time, simply type ALT + Delete and to delete an "marked" passage (as explained above) CONTROL + W (you can get it back by pasting, or "yanking" in emacs speak by typing CONTROL + y). You can undo the last change you made to a file by twisting up your finders with the CONTROL + x,CONTROL + u. Finally, you can exit the emacs editor by typing CONTROL + x, CONTROL + c.

Now, let's apply some of what we learned.

An Example Using emacs

We're going to change the first line of our example into two separate sentences (this is already sounding like a standardized test). When we last left fstep.emacs, it looked like this:

Buffers Files Tools Edit Search Mule Help ------------------------------------------------------------- Linux, the most popular Unix variant for PCs, is the only operating system not from Microsoft that is gaining significant market share. Created by Linus Torvalds and developed collaboratively over the Internet, Linux is the great success story of Open Source software development.| |

...cut for space...

----:---F1 fstep.emacs (Fundamental)--L5---All----------------------- (New file)

In order to edit the first sentence we are going to have to get the cursor up there. To move to the beginning of the paragraph, you can type the ALT + } combination. Your screen should now look a little like this:

Buffers Files Tools Edit Search Mule Help ---------------------------------------------------------------- |L|inux, the most popular Unix variant for PCs, is the only operating system not from Microsoft that is gaining significant market share. Created by Linus Torvalds and developed collaboratively over the Internet, Linux is the great success story of Open Source software development.

...cut for space...

----:---F1 fstep.emacs (Fundamental)--L1---All-------------------------- (New file)

You should now see the cursor over the "L" in "Linux" as above.

Now use the arrow keys (or other movement commands we've discussed) to move the cursor over both commas in this sentence and delete them with the CONTROL + d command. Now, just type normally to change this sentence:

Linux, the most popular Unix variant for PCs, is the only operating system not from Microsoft that is gaining significant market share.

into these sentences:

Linux is the most popular Unix variant for PCs. It is the only operating system not from Microsoft that is gaining significant market share.

When you are all done it should look something like this:

Buffers Files Tools Edit Search Mule Help ------------------------------------------------------------- Linux is the most popular Unix variant for PCs. It is the only operating system not from Microsoft that is gaining significant market share. Created by Linus Torvalds and developed collaboratively over the Internet, Linux is the great success story of Open Source software development.

...cut for space...

----:---F1 fstep.emacs (Fundamental)--L1---All----------------------- (New file)

You can quit and save your changes by typing CONTROL + x, CONTROL + c.

That's it for now. I hope you will give emacs a chance in your future explorations with Linux. While it is tough to learn, it can be quite powerful and does away with mode-based editing, the major complaint of some other editors, *cough*vim*cough*. While this is by no means an exhaustive look at emacs, it should give you enough commands to get you started and keep you from an emacs-induced migraine. For further information on the editor you can type CONTROL + h from inside the editor to gain access to the online help. I also highly recommend taking the online tutorial within the editor, by typing CONTROL + h, t which should walk you through a number of exercises that will familiarize you with the workings of emacs. You can also check out the Emacs Beginner's HOWTO. The best advice I can give to mastering emacs is the same as that for vim. That is... go slowly and keep at it. If you learn a little something new each time you use it, mastery will be just around the corner (Grasshopper).