GNU Emacs
From Stackwiki
Contents |
Preamble
I wrote this for my friend stock's wiki, which is located at Fishcracker. I will keep both the Stackpointer version and the fishcracker copy of this tutorial in sync.
Getting Started
As of this writing, GNU Emacs-22.2 is the current stable version of GNU Emacs. GNU Emacs uses GTK2 in under the UNIX-like Operating Systems, the Windows version uses Windows' native widgets, and there are version available for Mac OS X that use Cocoa, or Carbon. Each platform also has command line versions available. There is also an unstable (i.e. testing) version floating around called emacs-23 which supports Unicode, and font anti-aliasing in UNIX-like OS's.
Obtaining stable emacs:
- Download the latest version of GNU Emacs from ftp.gnu.org/pub/gnu/emacs.
- tar zfx emacs-22.2.tar.gz
- cd emacs-22.2
- ./configure --with-gtk
- make
- <enter superuser mode>
- make install
Other install methods:
- Gentoo Linux: simply 'emerge emacs' for the stable version, or 'emerge emacs-cvs' for the pretest.
- FreeBSD:
- 'cd /usr/ports/editors/emacs' or 'cd /usr/ports/editors/emacs-cvs' then run 'make install clean'
- or if portupgrade is installed: portinstall emacs or portinstall emacs-cvs
- Windows: There are a couple different distributions of GNU Emacs available for Windows. The binaries provided by the GNU project are of version 22.1, and the ntemacs group provides a cvs version that is roughly six months out of date. So I suggest using the version provided by EmacsW32. They build a version from CVS daily, and patch it with some subtle fixes that you might otherwise miss (native gui, etc). I personally recommend the vanilla version provided GNU Project especially for build >= Emacs 22.2.
Basics
In emacspeak, C- means ctrl, and M- means meta (which is usually bound to the alt key). So C-x means press Ctrl, then press x. M-x is reserved for changing the mode of Emacs. You'll find that while you're editing, C- will make small changes and M- will make larger changes. For instance, C-b will move your cursor back one character, and M-b will move your cursor back one word.
At this point, I strongly urge you to use the GNU Emacs interactive tutorial which can be invoked with C-h t.
If for some reason you forget everything related to Emacs, and you are an ace vim user, you can enter a vim emulation mode: M-x viper-mode
Macro's
Macro's are the driving force behind Emacs. Indeed, Emacs itself stands for Editor Macros. Macro's are simply key strokes that you press to make Emacs do something for you. They range from simple operations such as saving, and jumping to the beginning of paragraphs to launching shells inside of Emacs, or compiling your project.
Basic Operations:
- Detach: C-x C-z
- Reattach: at your shell type: 'fg'
- Exit: C-x C-c
Buffer Operations:
- Open File: C-x C-f
- Save File: C-x C-s
- Save File As: C-x C-w <filename>
- Display All Buffers: C-x C-b
- Close Buffer: C-x k
- Moving around:
- Jump to Beginning: M-<
- Jump to End: M->
- Move Forward One Character: C-f or the '->' key
- Move Back One Character: C-b or the '<-' key
- Move Forward One Word: M-f
- Move Back One Word: M-b
- Jump to start of paragraph: M-{
- Jump to end of paragraph: M-}
Searching Operations:
- Search Forward for String: C-s <string>
- Search Backward for String: C-r <string>
- Search and Replace Forward: M-x replace-string <orginal string> RET <new string>
- Search and Replace Forward (verify): M-x query-replace <orginal string> RET <new string>
Window Operations:
- Split Horizontally: C-x 2
- Split Vertically: C-x 3
- Close Window: C-x 1
- Switch Window: C-x o
Useful Modes:
- Calculator: M-x calc
- Shell Mode: M-x shell
- Calendar Mode: M-x calendar
- Diary Mode: M-x diary
- Directory Mode: M-x dired
- IRC Mode: M-x erc
Games:
- Adventure: M-x adventure
- Psychotherapist: M-x doctor (funny!)
- Dunnet: M-x dunnet
- Tetris: M-x tetris
- Solitaire: M-x solitaire
- 5x5: M-x 5x5
- Snake: M-x snake
For a complete listing of games, type C-h p, and then select the games entry.
Elisp Extensions
Emacs is built upon a LISP dialect called Elisp. Basically all of the functionality you've used in Emacs was implemented in Elisp, which you can edit and execute while Emacs is running (LISP and Emacs lend themselves quite well to Agile development). If you intend on writing your own extensions, files are named with the .el extension. To load that file simply put (load "/path/to/file.el") in your .emacs file. If it's a long file that takes awhile to load, you can byte-compile it by opening the file and pressing C
If you're new to LISP, you should probably pick up a book on Common Lisp, and then look at Emacs Lisp code to pick up the dialectical differences. I've been studying Peter Siebel's Practical Common Lisp. I understand Paul Graham's ANSI Common Lisp is also great.
As an aside, you can execute Lisp statements in buffers (such as *scratch*), by writing an s-expression such as (+ 2 3), then placing the cursor just after the ending brace, and pressing C-j. Emacs Lisp interpreter will then execute the statement and print the result on the next line.
Useful Elisp Extensions
Over the past thirty years, a myriad of usefull elisp extensions have been released by developers. Here are some of which I find useful. To find more, feel free to browse the emacs wiki.
Org-Mode
Content shall be here soon. Gee, I wish I had some way to stay organized so I could write this. *wink* *wink*
- org-mode: Keeps notes, TODO lists, and helps organize your life.
htmlize
- htmlize: Exports code as HTML. You can export as HTML 4, HTML+CSS, or using the font tag.
slime
- slime: The Superior Lisp Interaction Mode, terrific for interactively developing Common Lisp code.
Customization
There are two ways to customize emacs. The easier method is to invoke the Customization mode (M-x customize), and use its interface to change settings. The other is to find out elsewhere (such as the emacs wiki) what needs to be changed, and add the settings to your .emacs file, which is located in your home directory. Basically everything within the editor is customizable (provided you know where to look). This it the beauty of how Emacs was implemented: it's a foundation & interpreter programmed in C, with the actual text editor implemented on top of that in emacslisp.
.emacs
All of your customizations will be stored in your .emacs file. It's helpful to look at other peoples and grab parts of them as you need them. Grabbing the whole thing and dumping it into your own will probably make for an interesting experience. You can find mine at http://ojuice.teamslack.net/dotemacs.
Fonts
The first thing you'll notice while editing a source file, is that font coloring is not enabled. Simply add (global-font-lock-mode t) to your .emacs file.
Tabs
Another thing you may encounter is that TAB's don't work quite as you expect. For instance, Emacs defaults to inserting spaces each time you depress the TAB button, and defaults to a width of eight characters. Personally, I prefer having the TAB character inserted into the file, and displaying it with a width of four characters. jwz has a rather lengthly write-up on the TAB war. Add this to your .emacs file to get such behavior:
(global-set-key (kbd "TAB") 'self-insert-command)
(setq default-tab-width 4)
The only caveat is that while editing, you still need to delete tabs as if they were characters.
Code Style Conventions
Emacs will help you out by formatting your code for you. You can select from predefined styles such as K&R, BSD, etc. for C mode, or Stroustrup, etc. for C++ mode. Creating your own is beyond the scope of this article, but here is how to select and modify styles. I use K&R for C mode, and Stroustrup for C++ mode. To select these, add the following commands to your .emacs file:
(setq c-default-style "k&r")
(setq c++-default-style "stroustrup")
Now, let's say something about K&R rubs you the wrong way, and you want it to use 8 char width TAB's. Add this to your .emacs file:
(add-hook 'c-mode-hook
'(lambda ()
(c-set-style "k&r")
(setq c-indent-level 4)))
And it follows that if you wanted to change C++ mode, you'd change "c-mode-hook" to "c++-mode-hook".

