Emacs Reference Sheet ---------- This document describes some of the concepts and most important features of Emacs. It is in no way exhaustive! As you get more comfortable with Emacs, look for tutorials or information on particular features in the online Help or (of course) through Google. One such reference (on which some of this document is based) is: http://www.lib.uchicago.edu/keith/tcl-course/emacs-tutorial.html Basic Concepts ----- - Emacs is an extensible editor - Customize it via options or .emacs files - Many built-in tools - Designed for programmers - Edit, compile, debug, all within Emacs - Speed and convenience - Keyboard shortcuts for anything you can imagine, plus some - Tab completion on commands, filenames, etc - More than you can possibly learn (or any person knows) - Mail reader, calendar, version control, psychiatrist, etc, etc - Just learn what you need to, and anything else makes it better - Structure - Files: actual file contents on disk - Buffers: copy of file being edited in Emacs (usually, not always, a file) - Windows: view of a buffer (can have multiple windows) - Minibuffer: for displaying messages, typing commands - Confusing parts - Keyboard/character sets (because it's portable) - Some keys don't always work as expected (backspace, pg up, etc) - Multiple-key shortcuts - Control C-a - Meta (Alt, Esc) M-a (note that Esc isn't held like shift) - Type full name M-x command-name Basic Editing Commands ----- Open File C-x C-f Save File C-x C-s Quit Emacs C-x C-c Cancel Shortcut C-g Undo C-_ (or C-x u) (maybe multiple times) Movement: Go to line start C-a Go to line end C-e Go to next character C-f Go to prev character C-b Go to next/prev word C-arrow keys Regions (selected text between cursor/"point" and "mark"): Set mark C-space (or highlight with mouse) Swap mark and point C-x C-x Kill Ring (somewhat morbid cut/paste): Kill rest of line C-k Kill region C-w Yank from kill ring C-y Yank an earlier item M-y Delete character C-d Find/Replace: Incremental search C-s (repeat to find next) (starts looking as you type) Search backward C-r Regexp search C-M-s (incrementally finds regular expressions!) Replace M-% Run grep inside Emacs M-x grep Next grep result C-x ` (backtick) Buffers: Switch to buffer C-x b (then type buffer name) Show buffers C-x C-b Kill buffer C-x k Windows: Split window vertically C-x 2 Split window horizontally C-x 3 Remove all other windows C-x 1 Remove current window C-x 0 Switch to next window C-x o Integrated Shell ----- In addition to editing files in Emacs buffers, you can also run programs and tools. For example, it is very convenient to run a shell within Emacs in a buffer side by side with files you are editing. To run a shell, use the "M-x shell" shortcut. Backup Files ----- Emacs automatically saves backups of the files you edit, with a tilde (~) appended to the filename. It also auto-saves your files (with a hash (#) before and after the filename) in case Emacs is killed or the system crashes before you save. Customizing Emacs ----- You can heavily customize the way Emacs behaves by editing a .emacs file in your home directory. This file is written in a form of the Lisp language, and lets you change just about anything with how Emacs behaves, from keyboard shortcuts to syntax highlighting rules to fundamental behavior of the editor. For example, adding the following line to your .emacs file will bind control-L to the "goto-line" command (which has no keyboard shortcut by default, oddly enough): (global-set-key "\C-l" 'goto-line) Indenting / Modes ----- Emacs has many "modes" for editing text; most correspond to the programming language of the current file and provide rules for indentation, syntax highlighting, etc. In programming languages modes, Tab can always be used to re-indent the current line. If you start Emacs with a source file as an argument, it will usually be loaded in the correct mode. If you open a file and it isn't colored the way you expect, use "M-x global-font-lock-mode" to apply the appropriate mode for the file. Programming in Emacs ----- When you are developing programs in Emacs, you can easily run the compiler and view any errors without leaving the editor. The "M-x compile" shortcut will launch the compiler; by default this runs "make -k" (make is a tool for running build scripts), but you have an opportunity to run another compiler by hand, like "javac *.java" or "gcc myfile.c". You can step through any errors that are reported using "C-x `" (backtick). You can also run debuggers, such as gdb or jdb, within Emacs, using "M-x gdb" or "M-x jdb".