Programmer's friend - the editor: Part 2 - emacs
1. Introduction to part two
In part one we introduced you to Linux editors and gave a storm course on vim. It's now time to dismantle the rumors that we're subjective and talk about the other side, emacs. In some ways, the two editors are opposite one another, mainly from historical reasons, as you will see. We hope you will enjoy this tour and that we'll help you make up your mind.
2. Introduction and using emacs
I remember writing somewhere in one of my articles that I won't under no circumstances reveal what's my editor/WM/DE/Hollywood actor of choice. Not because I consider myself important, but because I want to avoid any flame material. The true reason for which emacs has an entire article's space, while vim has only half (or less) is the differences between them, and this is what we'll talk about right now.
vim, through its' predecessor, vi, is very much linked to Unix in terms of evolution, just like emacs is with the GNU movement. Here's a crucial difference that influenced the design of the two editors. When Bill Joy developed vi in 1976, hardware resources were scarce, and every character sent to the terminal mattered. In order to imagine what we're talking about, imagine that vi version 2.0 was still (almost) too big to fit inside the memory of a PDP-11/70. So this is the reason why vi(m)'s commands are short and perhaps cryptic for a beginner, and maybe that's why it has its' well-known simplicity. emacs is a wholly different story. It has over 2000 (yes, two thousand) built-in commands and many critics acuse it for its' size and overly complex commands. The name stands for "Editing MACroS", but it's said that it also has to do with a certain ice cream store in Cambridge, MA. Why Cambridge? Because the man responsible for emacs is none other than Richard Stallman, aka RMS, who was working at MIT at the time. That leads to one conclusion: working at MIT equals Richard had access to more powerful hardware, where characters or buffer space weren't an issue, at least not in the amount Bill Joy had to deal with. So although the first year of existence is the same - 1976 - access to hardware made a difference. Not the only one, but an important one, for sure.
2.2. Introducing emacs
The reason emacs has so many built-ins is because it is more than a text editor. Much more. There are people that are using emacs almost exclusively in their everyday work, as it can also be an e-mail client, an IRC client, VCS, TeX editor and much more. It is also very customizable via its' Lisp interface, just as it is extensible, so it can have facilities like games, newsreaders, PIMs and whatnot. So now it hopefully becomes clearer why I made the choice of giving emacs a whole part in our articles article. And of course, only a small part of the information will fit. As proof, the PDF emacs manual or version 23 has almost 600 pages, and I again have the unwanted task of fitting at least a part of that info in a few hundred words. If you feel emacs is too big and complex for you, there are quite a few derivatives that offer an emacs-like editor without the cruft; we recommend micro emacs for a start.
2.3. Using emacs
As you can see, emacs gives you a menubar in the upper part of the screen, and a echo area at the bottom, where you have various information about the filename, syntax highlighting used, if any, line number, insert mode (insert or replace) and so on. You can access the menus in the upper side by pressing F10. Take some time to look around the menus and see the functionalities you get and the corresponding keystrokes. To start, just start typing: since emacs is a non-modal editor, you don't have to get into insert mode first. Just like vim (but not vi), emacs has a built-in filesystem browser, as well as tab completion. The key combo that deals with this is Ctrl+x Ctrl-f (abbreviated as C-x C-f in the emacs world, and we will henceforth use it as well). If you wanna create a new file, type it's name after the key combo or simply invoke emacs with the filename as the argument. vim's equivalent would be ':e'. 'Save As...' is C-x C-w, as it's done in vim by using ':sav'. You will see the use of the Meta key in most of the emacs manuals/tutorials in existence. By the way, we recommend you read the official documentation. The Meta key we were talking about is the Alt key on the regular PC keyboard, and still can be found on Mac (the command key), Sun (remember the diamond?) or, surprise, MIT keyboards. When working with PCs you can also use the Windows or Escape keys, or simply remap.
You will, at one time, want to save. That is done with C-x C-s. Similarly, exiting is a matter of C-x C-c, and emacs will ask you what to do if there are unsaved buffers. Moving around back and forth a word at a time is done with Meta-left/right. You can notice already that emacs spares no keystrokes, as opposed to vim, which tries to keep it minimal. This is by no means something good, bad, better or worse. It's all about what you feel more comfortable with.
If you're a bash user and know some of its' most used keyboard shortcuts you will (probably) feel at home with emacs. By default, bash uses the emacs keyboard shortcuts for moving around, but it can be set to use the vi ones. So if you know that Meta-a and Meta-e goes to the beginning/end of the line or that Meta-b and Meta-f (back and forth) are used to move around between words, equivalent to Meta-left/right, you'll feel right at home. Speaking of movement, C-n and C-p move to the (next, previous) line, and C-v and Meta-v will go forward/back one screen. M-a and M-e will go back/forth one sentence at a time.
Should you make a mistake and want to delete something, you can always use the tried-and-true backspace and/or delete keys. You can also use C-d for single characters or M-Backspace for backwards deletion of whole words. In emacs, one of the terms you'll find original (although used in other editors) is "kill", standing for "delete", mind you, not a gruesome murder. So C-k and M-k are used to kill lines and sentences, respectively. You can set marks with C-Space (just as you can do with 'V' in vim), and after you mark the text that has to be deleted, use C-w, which can also be used to delete paragraphs.
C-x u is used for what's called "advertised undoing", but you can also use C-g to stop the current command.
Just like in the first part, this material isn't even enough to get your feet wet, and that's how it's intended. For example, we haven't touched important matters like cut/copy/paste, but that's left as an exercise to the reader. Also, as said, emacs is a very programmable piece of software, and if you don't speak Lisp, no problem. Emacs Lisp is a dialect designed especially for the purpose and you will make advancements in no time. It will though take a while until you have the perfect setup, just as it is with vim, but it will be worth it, for your efficiency will boost.