Guide to Emacs
Morgan McGuire <morgan@cs.williams.edu>
Contents
- Introduction
- Basic Commands
- Advanced Commands & Techniques
- Installation
- Customization
- My .emacs
- Resources
Introduction
Emacs is a text editor that has many ideal properties for programmers, including:Emacs is the editor of choice for many programmers. Like other professional tools, it is optimized for efficient use by a skilled user, not a shallow learning curve. It takes a week or two to get used to Emacs, but this is time well spent as it can dramatically improve development efficiency.
- Sophisticated editing commands (like regexp search and replace)
- Infinite undo/redo
- Edit multiple files
- Variable completion
- Integrates well with many tools like grep, gcc, gdb, cvs, and command shells
- Context sensitive syntax coloring, indenting and commenting
- Highly configurable
- Multi-platform
- Available at no cost
- Open source
Installation
Linux, FreeBSD, OS X and other Unix-derived operating systems provide Emacs as part of the default operating system installation. On Windows:The download has a README file you can consult as well once it is uncompressed.
- Download ftp://ftp.gnu.org/gnu/windows/emacs/emacs-20.7-bin-i386.tar.gz
- Download ftp://ftp.gnu.org/gnu/windows/emacs/utilities/i386/djtarnt.exe
- From the command line, execute
djtarnt -x emacs-21.3-bin-i386.tar.gz- Make a shortcut to
runemacs.exefrom the resulting emacs/bin directory.- Ensure that your HOME environment variable is set (it is by default on XP). Emacs looks for an optional configuration file called
.emacsin this directory.Basic Commands
You'll need to know these commands to use Emacs. XEmacs supports mouse gestures, but both Emacs and XEmacs can be entirely driven using the keyboard. Most Emacs commands are issued by pressing control ("C") or meta ("M") and then a key, e.g., "C-G" means press control and then press the "G" key. On most modern keyboards there is no explicit meta key. You can either use the Alt key on some computers, configure your keyboard (see the .emacs section below), or press and release the escape key (ESC) and then type a single key to follow it. That is, "ESC G" is the same as "M-G".
For some commands you need to begin with a modified key and then type keys without holding the modifier. For example, to quit emacs "C-X C", press and hold control while pressing "X", release both keys, and then press "C". You can think of this as "C-X" opens a file menu and then "C" selects quit.
I highly recommend learning some of the advanced commands as well once you are comfortable.
C-X C Quit emacs. C-X S Save file. C-X W Save file as. C-X F Open file. M-X revert Re-load file from disk. C-F, left arrow Cursor left. C-B, right arrow Cursor right. C-P, up arrow Cursor up. C-N, down arrow Cursor down. C-V, page down Page down. M-V, page up Page up. C-E End of line C-A Beginning of line home Beginning of buffer (i.e., top of the file). end End of buffer (i.e., bottom of the file). C-space Start selection. C-W Cut region. M-W Copy region. C-Y Paste. C-K Cut from current position to the end of the line. Hit repeatedly to cut a large section. Advanced Commands & Techniques
These are what make Emacs such a powerful programming tool.
C-X ( Begin defining a keyboard macro. C-X ) End defining a keyboard macro. C-X e Execute the keyboard macro just defined. C-M-f Move forward one expression. If you are on a open character like ({[<, this means jump to the corresponding close character. If you are on a word, this means jump to the next word. C-M-b Move backward one expression. C-U # (# means type a number) Repeat the next command a number of times. This is very handy for inserting a specific number of spaces, dashes or other character. Also, use this with C-X eto execute a macro repeatedly. To jump forward a specific number of characters, use C-U # followed by right arrow.M-/ Auto-complete the current word by looking backwards through all buffers. Hit this command repeatedly to cycle through all possible completions. M-space Collapse all whitespace between the current cursor position and the next non-whitespace character to a single space. M-x indent-region Auto-indent the currently selected region. M-x replace-string Replace all occurances of a string from the current cursor position to the end of the buffer. M-. Search all files indexed in a tags file for a string. You can create a tags file index using a program called etags.M-x tags-query-replace Search and replace a string in all files indexed by a tags file. This is good for changing all instances of a variable name throughout a number of files. M-x shell Run a command shell under Emacs. Since you can cut and paste easily between this shell and other buffers, this is handy. M-x goto-line Jump to the specified line. tab Auto-indent the current line. Customization
Emacs is configured by editing a file named.emacsthat sits in your home directory. Your home directory is the directory the HOME environment variable points to. If you don't know which directory this is, on Windows open a command prompt and typeecho %HOME%. On Unix open a terminal and typeecho $HOME.The .emacs file is actually a program written in a language named ELisp (Emacs LISP). You don't need to know how to program in this language to use a .emacs file however. Just paste in the sections of code below that you want. Most people accumulate commands from reading friends' .emacs files. The sections below generally alter the appearance of Emacs to make it follow general Windows appearance conventions.
To make "M-x shell" work well on Windows:
(setq process-coding-system-alist '(("cmdproxy" . (raw-text-dos . raw-text-dos)))) (add-hook 'comint-output-filter-functions 'shell-strip-ctrl-m)Turn on syntax-based text coloring, like most visual code editors.
; Syntax coloring (setq font-lock-maximum-decoration t) (global-font-lock-mode t)To show the selected region (the area between the mark and the cursor), in the way that most GUI editors do when you drag out a region, use:
; Show selections (transient-mark-mode 1) (set-face-foreground 'region "white") (set-face-background 'region "Navy")Here are the colors I use. Edit this to your preference.
Show the name of the file being edited in the title bar:
; Colors (set-face-foreground 'region "white") (set-face-background 'region "Navy") (set-face-foreground 'modeline "black") ; status bar (set-face-background 'modeline "grey") (set-background-color "white") (set-foreground-color "black") (set-face-foreground 'highlight "DarkGreen") ; hyperlink (set-face-background 'highlight "white") ; hyperlink (set-face-foreground 'font-lock-comment-face "DarkGreen") (set-face-foreground 'font-lock-variable-name-face "Brown");dimgray (set-face-foreground 'font-lock-string-face "DarkOrchid") (set-face-foreground 'font-lock-keyword-face "blue") (set-face-foreground 'font-lock-function-name-face "Navy") (set-face-foreground 'font-lock-type-face "Red") (custom-set-faces)The following practices (from the Emacs FAQ) may assist with your configuration process:
(setq frame-title-format "%b - Emacs")26: How do I set up a .emacs file properly? See "Init File" in the on-line manual. WARNING: In general, new Emacs users should not have .emacs files, because it causes confusing non-standard behavior. Then they send questions to help-gnu-emacs asking why Emacs isn't behaving as documented. :-) Emacs 20 includes the new "customize" facility, which can be invoked using M-x customize RET or via the Help menu. This allows users who are unfamiliar with Emacs Lisp to modify their .emacs files in a relatively straightforward way, using menus rather than Lisp code. While all the packages included with Emacs (are meant to) support Customize now, packages from other sources may not. While Customize might indeed make it easier to configure Emacs, consider taking a bit of time to learn Emacs Lisp and modifying your .emacs directly. Simple configuration options are described rather completely in the "Init File" section of the on-line manual, for users interested in performing frequently requested, basic tasks. 27: How do I debug a .emacs file? Start Emacs with the "-debug-init" command-line option. This enables the Emacs Lisp debugger before evaluating your .emacs file, and places you in the debugger if something goes wrong. The top line in the trace-back buffer will be the error message, and the second or third line of that buffer will display the Lisp code from your .emacs file that caused the problem. You can also evaluate an individual function or argument to a function in your .emacs file by moving the cursor to the end of the function or argument and typing "C-x C-e" (M-x eval-last-sexp). "C-M-x" (M-x eval-defun) is particularly useful for re-evaluating "defvar" and "customize" forms. Use "C-h v" (M-x describe-variable) to check the value of variables which you are trying to set or use.My .emacs
;; .emacs file for configuring emacs ;; ;; Morgan McGuire ;; Modified 2001-05-18 ;; Use F1 to get man page help (global-set-key [(f1)] (lambda () (interactive) (manual-entry (current-word)))) ;; Supress the GNU startup message (setq inhibit-startup-message t) ;; Turn off the menu bar ;;(menu-bar-mode -1) ;; Turn off the scroll bar ;;(scroll-bar-mode -1) ;; Make the buffer re-highlight when we recenter (global-set-key "\C-\\" 'hilit-highlight-buffer) (global-set-key "\C-l" 'recenter) ;; Get backspace key to work properly on many machines. (setq term-setup-hook '(lambda() (setq keyboard-translate-table "\C-@\C-a\C-b\C-c\C-d\C-e\C-f\C-g\C-?") (global-set-key "\M-h" 'help-for-help))) ;; No tabs-- use spaces when indenting (doesn't affect Makefiles, ;; does affect text files and code, doesn't affect existing tabs). ;; The use of setq-default means this only affects modes that don't ;; overwrite this setting. (setq-default indent-tabs-mode nil) ;; get C and C++ editting to work on the proper files (setq auto-mode-alist (append '(("\\.c$" . c-mode) ("\\.h$" . c++-mode) ("\\.C$" . c++-mode) ("\\.c[+][+]$" . c++-mode)) auto-mode-alist)) ;; give us perl editting features on .pl files (instead of prolog) ;;(load-file "/usr/gnu/lib/emacs/19.22/lisp/perl-mode.el") (setq auto-mode-alist (append '(("\\.pl$" . perl-mode)) auto-mode-alist)) ;; make a shortcut for the goto-line function (global-set-key [?\C-1] 'goto-line) (add-hook 'comint-output-filter-functions 'shell-strip-ctrl-m) (setq font-lock-maximum-decoration t) (global-font-lock-mode t) ;; Set "Windows" GUI colors for emacs, including showing selection. ; selection (transient-mark-mode 1) ; show selections (set-face-foreground 'region "white") (set-face-background 'region "Navy") ; status bar (set-face-foreground 'modeline "black") ; status bar (set-face-background 'modeline "grey") (set-background-color "white") (set-foreground-color "black") (set-face-foreground 'highlight "DarkGreen") ; hyperlink (set-face-background 'highlight "white") ; hyperlink (set-face-foreground 'font-lock-comment-face "DarkGreen") (set-face-foreground 'font-lock-variable-name-face "Brown") (set-face-foreground 'font-lock-string-face "DarkOrchid") (set-face-foreground 'font-lock-keyword-face "blue") (set-face-foreground 'font-lock-function-name-face "Navy") (set-face-foreground 'font-lock-type-face "Red") (custom-set-faces) (defun unixify () "Convert to unix linefeeds only." (interactive) '(untabify (point-min) (point-max)) (set-buffer-file-coding-system (quote undecided-unix) nil)) (global-set-key "\M-\C-m" 'unixify) (custom-set-variables ; '(tab-width 2) '(column-number-mode t) '(require-final-newline (quote ask)) ; Use unix line-feeds when saving (muck with the file-coding-alist to ; change them on loading). This doesn't actually do much since ; something in windows Emacs overrides this for every buffer. '(default-buffer-file-coding-system (quote undecided-unix)) ; Heavy handed approach to eliminating DOS EOLN's (13 10): force all ; files written to have unix EOLNs (10). '(coding-system-for-write (quote undecided-unix)) ) ;; Show buffer in window title (setq frame-title-format "%b - Emacs") ;; Turn off CVS for Emacs. This fixes mysterious problems where Emacs ;; makes CVS controlled files read-only sometimes. (setq vc-handle-cvs nil) ;; Support for change log. Set your name and e-mail address ;; so that C-4-a will add your entry to the ChangeLog automatically. (custom-set-variables '(add-log-full-name "Morgan McGuire") '(add-log-mailing-address "morgan@graphics3d.com"))Resources
GNU Emacs home page http://www.gnu.org/software/emacs/emacs.html GNU Project home page http://www.gnu.org/home.html Latest Windows binaries ftp://ftp.gnu.org/gnu/windows/emacs Emacs FAQ http://www.gnu.org/software/emacs/emacs-faq.text GNU Emacs on Windows NT and Windows 95/98 FAQ http://www.gnu.org/software/emacs/windows/ntemacs.html Acknowledgements
The author gratefully acknowledges Guy Steele and Richard Stallman as the original authors of Emacs, and thanks the thousands of people who have since contributed to Emacs directly through patches or indirectly by providing .el files.
Guide to Emacs © Copyright 2003, Morgan McGuire All rights reserved. (2003-09-10)
Revised 2007-01-31.