justlinux.com
Sat, 11-Feb-2012 20:09:27 GMT

Forum: Registered Users: 75960, Online: 301
nhfs Here you can view your subscribed threads, work with private messages and edit your profile and preferences Registration is free! Calendar Find other members Frequently Asked Questions Search Home Home

Help File Library: Basic emacs


By Bryan Dotzour

Table of Contents

What is emacs?

The Basics of emacs

How emacs Looks The Basic Commands

What is emacs?

Well, how to begin is a question for philosophers, so I'm just going to dig in and go for it. The first question you may ask is, "What exactly is emacs?" And that would be a very good question. Some highbrow users may raise their eyebrows and reply, "What isn't emacs?" But not here, we will go into detail about the purpose, uses, and general basics of the emacs editor.

As mentioned in that last sentence, emacs, in the most basic sense, is a text editor. It allows you to create, edit, and save new or existing text files on your machine. But its scope and power is not limited to mere text editing. Emacs incorporates various operational modes specific to the type of document that you are working on. This document is being written in emacs under the text-mode. Other modes include HTML, C++, LISP, and SCHEME.

"What," you may ask, "is the purpose of these operating modes?" Excellent question! For now, let's just say that they make emacs smarter and friendlier to use when editing these types of files. For any programmers out there, can you say automatic parentheses matching?

By now, you have a very basic understanding of what emacs is and what it is capable of. So, given that, let's start working with emacs.

The Basics of emacs

Starting Up emacs

So, now you're ready to begin using the emacs editor. There are several ways of invoking emacs, but we'll talk about two main ones in this document.

If you are using and X-windows interface, you may have a graphical representation of emacs. This method of "emacs-ing" is very nice because you have access to the mouse for highlighting text and moving around in the window. On my standard KDE desktop environment, the graphical edition of emacs is found in /usr/bin/emacs.

The other option is to run emacs within a terminal window or console. This method does not include mouse functionality, but everything else is there. The executable is /usr/bin/emacs-nox. Your system may not support X or does not have an X-compatible emacs. Just try typing "emacs" from a terminal and see what you get.

So, the way you use emacs is up to you. Just like most things on your computer, it's a matter of personal preference. Now, it's really time to work with emacs. Start it up in whatever way you've decided upon and let's begin.

How to Issue Commands

Commands in emacs are usually invoked by a sequence of key combinations involving the CONTROL key and the META (on most systems ESC) and any other normal key. This is not a big deal, but it takes some getting used to. Throughout this document, these combinations will be notated in the following way :

C-x : means hold down the CONTROL key and press the 'x' key. M-x : means hold down the ESC (or META) key and press the 'x' key.

Commands may involve one of these sequences followed by a standard key. These commands will be written as :

C-x-o : means C-x, let up on both keys and press the 'o' key.

Commands often involve a series of these combinations, they will be written in the following manner :

C-x-C-w : means C-x AND THEN C-w.

Emacs provides its own tutorial that covers the very basics. It can be run at any time by C-h-t. The same basics covered in the tutorial will be covered in the next section.

How emacs Looks

So, you have a nice emacs interface started up, but what do you do with it? First of all, familiarize yourself with the general setup of the screen. If your emacs is graphical, you will probably have a menu bar at the top of the window. This is very useful, but not necessary. As a true student of emacs, you will find your mouse all but useless. Well, maybe not, but you can certainly get along without it. From this point on, almost all of the commands will referenced as keyboard commands. It should be fairly simple to find what you need in the menu if necessary.

Take a look at the bottom of the screen. There is a white bar with some very useful information about what's going on in your document.

The '---' on the far left indicates the save status of the current file you are working with. If no changes have been made since you last saved, the file looks like '---'. If you have made changes since then it will look like this '-**'.

The next bit is the name of the file you are working on. If you just started emacs up, it will say '*scratch*'. The scratch pad is the default document that comes up when emacs starts. For the most part, you can just ignore the scratch pad.

To the right of the file name, right in the middle of the white bar, is the operation mode that emacs is currently running. The default mode of the *scratch* file is (Lisp Interaction). What this means is not important right now. What is important is that you see that this mode is dependent on the type of file we use. We will see this shortly, I promise.

To the right of the operation mode is the line number on which the cursor currently resides. When you first start out, it is probably on line 1 which looks like 'L1'. If you move the cursor down a few lines, the line marker will go along with you. This can be very handy in many situations.

Next to the line marker is the file position indicator. It is telling you whether you are at the 'Top' or 'Bot'tom of the file.

Directly under the white bar is the message box which will display the keyboard commands that you execute as well as any messages about the file on which you are working on.

The Basic Commands

Creating a New File

To get started, you are going to want to create your own file to work with. This is done by issuing the open file command. Type C-x-C-v. In the message box the path of the file will appear. What you will probably see looks like this : '~/'. This indicates that emacs is looking in the directory from which it was called for the file.

Hopefully, you started emacs up from your home directory, or wherever you like to keep your files. So, it'll be just fine to type in the name of the document you want to open. If it doesn't exist in the current directory, a new file of that name will be created. So, type in 'example.txt' or anything that ends with the '.txt' extension, and hit Enter. And presto! You have a new, blank emacs document to work with. Take note of the changes that have occured on the white info bar at the bottom of the screen. The filename you chose is now there and the operation mode is now (Text Fill). This is the standard plain text editing mode. You're ready to type away.

Saving a File

Now that you have played around and created this new, or edited an existing, document you may find it useful to save the changes you have made. This is easily done by typing the command C-x-C-w to save those changes. This allows you to save the document and continue working.

Exiting emacs

When you have had enough of emacs, the command C-x-C-c will lead you out. If you try to exit emacs without saving your document it will ask you if you really want to discard the changes you have made. You can answer with a 'y' for yes, 'n' for no, or '!' to force quitting with no questions asked. This precaution can be a pain, but it only takes once to learn how useful it is for the computer to ask before your changes are deleted.

Moving Around in emacs

For moving around from space to space or from line to line, it is probably easiest to simply use the arrow keys. However, if the arrow keys are not mapped on your terminal, or if you like to do things the "special" way, here are the movement commands.

C-f forward one character
C-b backward one character
C-n move to next line
C-p move to previous line

Now, there are some other movement commands that are useful no matter what kind of emacs you are using. A few of the most useful are listed here for your convenience.

C-a moves to the beginning of a line
C-e moves to the end of a line
C-v moves forward one screen
M-v moves backward one screen
C--> moves right to the end of the next word
C-<- moves left to the beginning of previous word

There are a number of editing commands that are also helpful in basic text-editing mode.

C-k deletes everything to the right of the cursor on current line
C-d deletes characters from the left (unlike standard DEL)