Saturday, November 04, 2006

Gnu/Emacs - The First & Right Steps!

It has often been said that all that Emacs actually requires is a good operating system. With that much power and utilitarian approach built into it, Emacs is certainly not the easiest of the lot to learn.

GNU/Linux utilities and widgets are characterised by fine and detailed manual pages (info pages), yet a newbie is daunted by the formidable array of options and the beguiling number of facilities details in info emacs. This document attempts at being a hand holder for those who feel the need to begin utilising Emacs for their daily variety of key pushing.

EMACS means?

Lets start with expanding the term GNU Emacs. Generally Not Used Exceptionally Mediocre Algorithm for Computer Scientists. Well, this expansion may be a joke, but this very mindset has caused many eager minds to avoid the almighty editor and all the grace that it encapsulates. But this is not at all the true picture. At least for me and other fellow emacsites (as RMS prefers the disciples of the Church of Emacs to be known as), Emacs stands for "Emacs Makes All Computing Simple" (yet another fine recursive acronym) and above all, a lifestyle altogether.

A quote from the discussion in comp.os.linux.misc on the intuitiveness of commands, especially Emacs, might just put things in perspective

Who wants to remember that escape-x-alt-control-left shift-b puts you into super-edit-debug-compile mode??

Ok, so Emacs is?

Emacs is an advanced, self-documenting, customizable, extensible, real-time display editor.. and much more.. to be nearly precise. Originally, an acronym for Editor MACroS, the first Emacs was a set of macros written at MIT by Richard M. Stallman (popularly known as RMS) for the editor TECO (Text Editor & COrrector, previously known as Tape Editor and COrrector) in 1976 under ITS on a PDP-10. But, Emacs was started by Guy Steele as a project to unify the many divergent TECO command sets and key bindings at MIT and was finally completed by RMS. Oh no, don't go away, give it a try and you might end up being a frequent visitor to alt.religion.emacs. Oops, I might cause a buffer overflow.

And so let us proceed.....

Emacs is big. But that didn't bother me as I didn't want to bother myself about it. This is what I think made my life easier with it. I started out with Emacs not because I did not like vi or any other ones and I don't remember anyone providing external stimuli either. The info pages were dreadful to me as these pages tend to say it all at a go and almost made me look for an alternative. Now, as I look back to those days when novices were novices and scratched their own heads, I feel an earnest urge for story-telling. As I started to play with the huge repository of commands and their key sequences, I often went on trying stupid ways to remember them and often tried hard not to remember some of them. Please bear with me for the next few paragraphs and you will get a clue on my early days of triumphs and sufferings with GNU Emacs.

GNU Emacs was conceived to be the ultimate editor. But being rich in tools that go way beyond simple cut, copy, paste etc, there is a price to pay. And that price is speed and space. It occupies a lot of space if you compare it with other editors and if you are running on low-end hardware, you can expect it to be annoyingly slow. I think, I can consider these two factors as redundant for people having lots of space and processing power. The rest, can stop reading. No! don't. If you understand why Emacs was built to be Emacs, then you will surely find everything from this point onwards helful and will probably end up thanking me. While running on low-end hardware, if you try to fire up a Emacs session every time you want to open a file or create a new one, you will get into serious problems with speed. Emacsites usually start their Emacs session just after logging in and close it just before they log out. I hope you got my hint. That's the very reason for calling it a lifestyle as people tend to spend their whole *computing life* under it. It has got almost everything you need, except a kitchen sink and that's why the icon for GNU Emacs is sometimes found to be a kitchen sink!!.

A few things that it can do?

  1. Delete a whole document that you forgot to save or backup? With Emacs, you can avoid a disaster like this.
  2. Emacs can help you get an overview of your files in many ways
  3. From within Emacs you can control any part of your working environment, manage files and directories, read and send mail and news and compile and debug programs.
  4. Emacs gives you an integrated environment where you can edit any of your files - HTML and LaTeX files; C, C++ and Java programs and configuration files. Editing different kinds of files with the same editor has the advantage that any task that is not specific to the file type is shared in common with all file types. For example, undo, cut & paste, spell-check and search and replace etc all work the same for any of the tasks.
  5. Unlike any other editor available on Unix, Emacs has a very powerful macro mechanism with which you can automate the monotonous part of your editing.
  6. You can read your mail and news with the same interface from within Emacs. Given the power of Emacs, it is no surprise that creating a mail and news reader in it makes Gnus (MUA) the most powerful reader available.
  7. Emacs?s search capabilities go far beyond your wildest dreams. They include searching for text incrementally (Emacs finds the next location as soon you type a letter) and searching for text in multiple files.
  8. Almost any possible kind of editing facility is available in Emacs, either as the core part of Emacs or as an extension available on the Internet.
  9. Gnu Emacs runs on Microsoft Windows (yeah yeah, you guys are also blessed).
  10. When you get really good at Emacs, you can build on it using the Lisp programming language.

Avoiding such a giant tool just because it's giant doesn't sound acceptable to me at all. Emacs is not at all difficult to learn, it's just different. The need and the driving force is to understand the elements that make it different. Until you get the right frame of mind and the right way to go through it, you will find it's resources and tools unattainable. Thus, I will try to present the few basic concepts, blended with my own experiences and end-user insights so that you can implement your own ways to conquer the almighty GNU Emacs.

And the users' words are?

I'd crawl over an acre of Visual This++ and Integrated Development That to get to gcc, Emacs, and gdb. Thank you.? (By Vance Petree, Virginia Power)

On a normal ascii line, the only safe condition to detect is a BREAK - everything else having been assigned functions by Gnu EMACS.? (By Tarl Neustaedter)

GNU Emacs - the console story

From this section on I will be talking about GNU Emacs 21.2.1. I urge you to use the console version first. That way you will do much better. I still use the console mode although GNU Emacs has a very attractive X window interface.

Now let me explain how I want you to handle the key bindings which you will encounter very often from this section on.

When I say M-x, I want you to hold down the left ALT key and while keeping it pressed, press "x". Throughout the Emacs info pages, the ALT key has been represented by the term "META". Keep that in mind. The key sequence C-_ is to be done the same way. Instead the ALT key the Esc key can be used. Some key combinations have alternatives. [C-x u] is equivalent to C-_. Here I want you to act a little differently. While holding down the left CTRL key press "x"; release both of them and press "u". Similarly, [C-x C-b] means, hold down the left CTRL key and while keeping it pressed, press "x", release "x" and while still holding the CTRL key down, press "b".

NOTE: GNU Emacs has a very useful set of key combos which represent the function oops-what-have-I-done. Although a function name of such caliber doesn't exist, there exists the key combination C-g. This stands for keyboard quit. Pressing the ESC key three times successively will do the same.

The Screen Layout

You invoke GNU Emacs just by typing "emacs" (without quotes) at the command prompt.

Emacs will dump you into its initial screen which contains information about some of it's helpful features. As I have said earlier, Emacs is different and it's difference with other editors is reflected from the very beginning; in this case, right from the initial screen layout. Scattered around the screen, you will find different kinds of information regarding your editing sessions.

At the very top of the screen, there is a line in inverse video. This is the menu bar. The menu bar contains regular entries such as File, Edit, Options, Buffers, Tools and Help. In addition to these entries some entries are added when you edit a file in a particular mode (See section Major & Minor modes). The Menu Bar can be accessed through the F10 key. M-` will also do. Activating the Menu Bar is not recommended as it is quite cumbersome. Once you choose an entry in the list, the drawback is that you can go back. You have to use the ESC key three times to get you out and start afresh.

Towards the bottom of the screen, there is a similar line in inverse video. This is called the Mode Line. This is a very important section of the Emacs screen and is often ignored or regarded cryptic by new users. That shouldn't be so!! Lets start decrypting the Mode Line then. The format of the Mode Line in general is:

-uu-:**-FN buffer_name (major & minor modes)"LN"N% where N stands for any positive integer.

At the left most part, the uu stands for the encoding status of the content you are editing. Followed by that, the ?**? tells you that the text has been modified in Emacs but hasn?t been saved to the disk yet. "*" tells you that the file hasn't been modified. "%%" means that the file is read-only.

Rarely, if it shows "%*", it's telling you that the file is read-only, but you have nevertheless been able to edit it!! This is a special case and needs special actions to be taken in Emacs.

Next comes the entry FN. Here, if N = 1, then F1 means that this is the first "Frame" (See section FRAMES & WINDOWS). The whole Emacs screen is a single Frame. Similarly, F2 stands for the second frame.

The next element is the name of the buffer. For the time being, you can consider the word "buffer" to be a representation of whatever you see between the Menu Bar and the Mode Line. When the buffer doesn't relate to an actual file on the disk, such as output from the command "grep" or error messages due to compiling a file, the buffer name has an "*" on both sides of it.

Coming next, within brackets the Major mode and minor modes are shown (See section Major & Minor Modes). Following that, comes the line number.

The last entry shows your position in the buffer. If you are at the top of the buffer, this entry will display "Top" and similarly "Bot" if you are at the bottom of the content in the buffer. If it shows some percentage, say 55%, then it implies that the first line on-screen is 55% from the top.

Below the Mode Line, is the Mini-buffer. This is the place where Emacs interacts with you. It asks all types of questions regarding your editing session, displays the functions and key-bindings you type in, shows status messages of functions, or sometimes displays a line like "Psychoanalysis!! I thought this was a nude rap session!!!?.

The mini-buffer asks you all types of questions. The simplest being allowed to answer in either yes or no. Well, there are basically two types of "yes or no" questions. This sometimes irritates the new user who wonders why in one case is it necessary to answer just "y" for yes and in another "yes" for yes.

Confused??? Let me simplify the underlying logic for this strange conduct. When your answer will have a large influence on the current situation, Emacs will force you to answer in either "yes" or "no". On the other hand, in situations where a wrong step can cause a slight irritation, but nothing fatal, Emacs will let you answer in either "y" or "n".

An example of the first situation is : Buffer modified; kill anyway? (yes or no).

And for the second one: Quit spell checking? (y or n). Simple.

Buffers, Windows and Frames

In GNU Emacs, the term Buffer was devised to create an entity to relate to the content on which you are editing. If you open a file from disk on to Emacs, the contents of that file are kept in the buffer and you do all your editing on that content. Until saved, the edited changes in the buffer are kept local. Thus, a given action will operate on the buffer and not on the actual file on the disk.

By default, Emacs gives you two buffers. Those are, scratch and Messages. Type C-x C-b and you will be shown the buffers list. As I have pointed out earlier, buffers that do not relate to actual files on disk have "*" on both their sides.

The first thing to be mentioned when talking about ?Frames and Windows? in Emacs is that this is a strictly "No Microsoft Zone". This is because, MS-Windows users must learn that a building can have multiple windows and not vice versa.

In GNU Emacs, the space between the Menu Bar and the Mode Line is called the window. This is where your buffer appears and your editing magic is reflected. The whole Emacs screen is a single "Frame". Thus, a single frame can contain multiple windows.

Multiple buffers can be opened in multiple windows in a single frame. As you will gradually go deep into Emacs, you will need to read the info pages that it provides.

If you do not keep the concept of frames and windows in your mind, then you will find it hard to walk through the documentation.

Point, Mark and Region

The term "Point" refers to the insertion point in the buffer. This marks the place where, when you type in, inserts the characters you type. You will have to refer the point as the "point" and nothing else as cursor position or whatever you have been used to. The point is located between two characters and blinks continuously.

Suppose you need to select a section of the buffer to cut, copy, paste etc. For that, you have to mark the text in Emacs. The marked part of the buffer is called the "region". A region has two ends. One point and one "mark".

In Emacs, you can set a mark by typing C-SPC or C-@. When not using the mouse, you can "mark a region" by going to one of it?s boundaries, type C-SPC, to set the mark there and then go to the other end. The point will now be placed in this end. The buffer portion between the mark and the point will be highlighted.
If the region is not highlighted, put the following line in /home/your_account/.emacs :-
(transient-mark-mode t)

Selecting a portion of text is a very common job in editing. But still newcomers to Emacs neglect these simple terms and get confused later on when they read the manuals.

Now, take note of the following.

Point is a characteristic of a window, but mark is a characteristic of the buffer. This generally means that, having two different regions for the same buffer in two different windows is not possible because they share the same mark. You can transpose the point and the mark by typing [C-x C-x].

Check this out and look for yourself why this feature may be of help.

Major and Minor modes

GNU Emacs provides different major and minor modes for different types of files and programs.

Emacs has modes for almost all kinds of editing jobs. For example, there exists "cc" mode for editing .c files, HTML mode for editing .html files and so on. For a given buffer, only one major mode is enabled at one time, although you can enable multiple minor modes in a single buffer.

In a major mode, Emacs gives you additional commands and functions, in addition to the general set of functions which are applicable to all kinds of buffers and modes. Most of the major modes includes specialized key bindings, specialized functions, variable configurations and many others. To get a list of special key bindings for a particular enabled major mode, type [C-h m].

The major mode is local to the buffer. By default, Emacs enables the "Fundamental mode" which is a major mode with no special functions. The prefix C-c is often used to bind special functions in a major mode. Type [C-c C-h] to find out which bindings exist with the C-c prefix. Simply typing M-x (mode name) puts you into that mode.

Emacs by default, recognizes most common file formats by looking into the file name extension and enables the major mode appropriate for it. To disable a particular major mode, you have to enable a different mode.

Minor modes takes the power of customized editing facilities way beyond other editors. You can enable multiple minor modes (including zero) at the same time in a buffer. Some minor modes are local to the buffer and some are global in nature. Enabling and disabling minor modes have the toggling nature. That is, you can type M-x (minor mode name) to either enable or disable a minor mode. Here are some samples:

icomplete-mode > When this mode is enabled, when you type M-x, available completions are listed as you type.

flyspell-mode > This mode enables on-the-fly-spell-check facility in Emacs.

overwrite-mode > Here typed in characters replace existing text on a one-for-one basis.

mouse-avoidance-mode > This minor mode is helpful in the X windows version of Emacs. This is a quite funny but useful mode that moves the mouse cursor if it comes too close to the point. After enabling this mode, it will ask for an option. Hit the TAB key to get possible completions. My favorite option is "Proteus".

Keyboard layout

Elaborating on the "expansion factor", Emacs in real computing can be expanded lively as "Escape Meta ALT Control Space".

I don't know if RMS looked at it this way from the very beginning, but this time it surely makes sense, as you will gradually see.

Emacs is a huge repository of key sequences combining the above mentioned keys. You master the key combos and you master Emacs. But sometimes, some functions are easy to remember by name than their equivalent key combinations. That of course, I leave as a choice for you to implement.

When you start using GNU Emacs, the very first thing you should do is memorize as many commands and key sequences as soon as possible.

No, never, don't.

Those will be your first wrong steps. Instead you should learn the basics that govern the overall keyboard layout of the huge repository of key bindings. This way you will have no trouble in learning more and more key sequences as you need them. Engulf the following:

Functions which are used less often are bound to the prefix C-x. [C-x C-c] exits Emacs, [C-x C-f] opens or creates a new file, [C-x C-s] saves the current buffer etc.

Functions specific to a given mode are prefixed with C-c.

Functions which are rarely used are not bound to the keyboard at all. These functions are accessible by typing M-x (function name).

All functions that are used very often, are available with one modifier key. That is either the Control or Meta prefix. C-_ is undo, C-e takes you to the end-of-line, M-e takes you to the end-of-sentence, C-w is cut, M-w is copy, C-y is paste, C-s is search forward incrementally, C-r is search backward incrementally etc.

If the Control prefix, grouped with a particular key works on characters, then the Meta prefix works on words for that particular key.

Example: C-t transposes two characters, whereas M-t transposes two words. Similarly, if the Control prefix works on lines, then the Meta prefix will work on sentences[Note?Emacs needs sentences to end with two spaces after the period(.) to be recognized].

Example: C-e moves point to the end-of-line, whereas M-e moves point to the end-of-sentence.

The prefix C-x 4 is used for functions that operate on another window.
[C-x 4 C-f] opens a file in another window (find-file-other-window).
The prefix C-x 5 operates on other frames.
Similarly [C-x 5 C-f] opens a file in another frame.

All letters, numbers and symbols insert the given label if used without any modifier key.

A word about the movement keys in GNU Emacs.

[C-h t] starts a tutorial in Emacs. There you will find a dreadful set of key combos for movement within the emacs screen. C-f for forward one character, C-b for backward one character, C-p for previous line, C-n for next line, M-v to scroll down and blah-blah-blah-blah?.!

Honestly, these scared me. The solution? Just don't look at them. I promise you won't need them until your keyboard doesn't have the arrow keys.

These key combos were designed in such a way that they can be used with any kind of keyboard. It took me some time to realize that I was wasting my time learning these keys.

The key sequences are always there to be customized. You can create your own key bindings and assign functions to them. You can even write Lisp expressions to automate certain jobs or to make Emacs behave the way you want it to.

Tail enders

Well, GNU Emacs has a lot of features.

One of them is the ability to read and write mail and news. The default mail reader in GNU Emacs is RMAIL and the news reader is GNUS.

Although Gnus can be used to be a mail reader too. Often newcomers consider using these tools very geekish. But, unless you really need to use those features, do not go into the details of learning them. It kills more time than any "first person shooter" would. Pay attention to the basics first.

When I first went online, I did configure Rmail and Gnus. Who knows, I may get a job of a Linux Sysadmin in Djibouti where my boss is a warlord and tribal chief who believes that there is only one church ? The Church of Emacs!!!!!!

Use the Almighty TAB. It's everywhere in the GNU/Linux world. Emacs is not an exception. Use it everywhere, be it opening a file or typing a function name using M-x.

Command history is another important omnipresent feature here. The up arrow key is used to fetch previous commands. For example, if you type M-x up-arrow, then you will be shown the previous function names you have executed.

Similarly, type [C-x C-f] and then use the up arrow to recall previously visited files. But command history is local to your Emacs session. You exit Emacs and you don't know who you are, that is, you loose your history.

GNU Emacs has been so well crafted that it is considered as a complete operating system. But this fact overshadows the fact that it has a very good editor to be utilized to your heart's content. Learn the editor first. It's a profound experience.

Rmail and Gnus are dauntingly heavy to learn. You can always give them a try later in case you get a job in Djibouti!! You may even try a hand at learning Lisp someday and write extensions for Emacs.


1 comment:

smdeep said...

Vim rock :P