Using the Emacs Text Editor Handout No. 3 CSC UNIX Series March 1991 ----------------------------------------------------------------------------- In the world of UNIX text editors, Emacs is a special case. It is more than just an editor--it is a complete Lisp environment and can do, with sufficient prodding, just about everything. Emacs has a huge set of commands, each of which executes some particular function. Many of these functions are bound or attached to a key. Even alphabetic characters are really Emacs functions--the 'A' key is bound to the command "self-insert," which inserts the key typed into the text buffer. There are a number of different variations or flavors of Emacs. The one described in this handout is GNU Emacs. There are several different modes that Emacs runs in. Unlike many other text editors, Emacs does not have separate edit and input modes; its different modes are environments designed for specialized purposes. The basic mode for editing text is the Fundamental mode. A C mode, for editing C programs, provides automatic indentation and brace matching. Another mode is Lisp mode for editing Lisp programs. In addition to specialized programming modes, Emacs can compile programs without leaving the editor. The command "compile" will open a second window and run the make program. The output from the compilation will be sent to this window, allowing you to view compilation errors alongside the actual source code. This handout will not go into great detail regarding all of Emacs--to do so would require a document nearly as large as the actual Emacs manual, which is just under an inch thick (and available for reference in the Program Library, CSC room 3326). It will, however, give a sufficient introduction to Emacs for the casual user, and will help point you in the right direction for learning more about some of the more advanced features of this environment. There are other text editors that can be used on UNIX systems; one of the most popular of these is vi. Vi is available on all of the UNIX systems maintained by the Computer Science Center. A handout describing basic vi commands, Using the Vi Text Editor, UNIX Handout #2, is available from the Program Library (CSC room 3326). ----------------------------------------------------------------------------- Entering Emacs Commands As mentioned above, most Emacs commands are bound to keys. Some, like "self-insert," are simply bound to keys producing printable characters. Most others, though, are bound to Control keys, or to Sequences-of-Control keys. To enter a command, you simply type the key sequence to which the command is bound. ----------------------------------------------------------------------------- Emacs Command Notation Most Emacs function names are multiple words, separated by hyphens; e.g., "describe-variable" or "save-buffers-kill-emacs." Their bindings are represented by C- for control key bindings, and M- for Meta key bindings. The Meta key on most keyboards is the Escape key. Control key bindings are entered by holding down the Ctrl key, and then pressing the given key. For instance, C-v is bound to "scroll-down." To scroll downwards through the text, simply hold down the Ctrl key and then press the v key. Meta key sequences, on the other hand, differ depending on the meta key. On VAXstations, M-% (query-replace) is invoked by holding down the Compose Character key and pressing the % key. On a terminal using the Escape key, press and release the Escape key, and then press %. Some commands are accessed through multiple Meta and Control sequences. To save all buffers and exit Emacs, for example, use the two-key sequence C-x C-c. All Emacs commands can be bound to multiple or different keys, using other Emacs commands, or a startup file (often called .emacs on UNIX systems). In addition, a list of all current bindings can be seen by invoking describe-bindings, which is usually bound to C-h b (the two-key sequence of a Ctrl-h, followed by a b). The command bindings described in this handout are the default keybindings. All commands can be invoked directly by using the M-x command (this is the only way to invoke a command that has no binding). This gives a prompt at the bottom of the screen, and the name of the command to be executed should be entered there. ----------------------------------------------------------------------------- Getting Help in Emacs Emacs has an extensive on-line help feature. This feature includes such things as command lookup, binding lists, and a tutorial. By far the best way to learn about Emacs is to use it as often as possible. The tutorial gives an introduction to general Emacs use, and is a good place for beginners to start learning Emacs. The other help functions, particularly describe-function, can help better explain many of the commands described here. A hyper-text documentation browser is also provided--to run this, use the info function (it is fairly self-documenting). The help prefix is C-h. Typing C-h C-h will give a list of possible help options; typing C-h three times gives a short description of each help option. C-h t will start the tutorial. Function Binding -------------------------------------- command-apropos C-h a describe-key-briefly C-h c key info C-h i describe-mode C-h m help-with-tutorial C-h t where-is C-h w describe-bindings C-h b describe-function C-h f describe-key C-h k view-emacs-news C-h n describe-variable C-h v ----------------------------------------------------------------------------- Moving the Cursor Moving the cursor in Emacs is a little different than with most text editors--rather than placing the keys in a sort of diamond or row, Emacs uses (in most cases) letters which correspond to English words, to make their functions easier to remember. Some commands deal not with characters, but with English constructs like words or sentences. These are often the same letter as an analogous character-based function, but with a Meta key instead of a Control key. Function Binding -------------------------------------- previous-line C-p next-line C-n forward-char C-f forward-word M-f backward-char C-b backward-word M-b beginning-of-line C-a recenter C-l backward-sentence M-a end-of-line C-e forward-sentence M-e scroll-down C-v scroll-up M-v beginning-of-buffer M-< end-of-buffer M-> transpose-chars C-t ----------------------------------------------------------------------------- Cutting, Copying, and Pasting When you delete a line, it is stored in a kill ring, which basically is a list of all recent deletions. Text stored in the kill ring can be yanked back into the document at a later time by using Ctrl-y. To yank something other than the most recent kill, use Meta-y (yank-pop), after first yanking with C-y. Each time M- y is used, the last deletion is removed from the screen and replaced with the previous deletion. In this way you can cut and paste several different blocks of text at once. To copy a block, just delete it, yank it, and then move to where it should be copied, and yank it again. Only commands with "kill" in their name get stored in the kill ring--that is, simply deleting 20 characters with C-d won't work. Function Binding -------------------------------------- delete-char C-d kill-word M-d kill-line C-k kill-sentence M-k open-line C-o undo C-x u isearch-forward C-s yank C-y yank-pop M-y delete-backward-char DEL backward-kill-word M-DEL delete-blank-lines C-x C-o query-replace M-% isearch-backward C-r ----------------------------------------------------------------------------- Files, Buffers, and Windows Emacs can work with several files at once, each with its own named buffer. The name of the current buffer is shown in the control line at the bottom of the screen. To load a file into the current buffer, use C-x C-f. You will then be prompted for the filename. Also, Emacs can split the screen into different windows, and put different buffers (files) in each window. There are several functions relating to windows, and most operate relative to the current window (where the cursor is). A region is the space between the cursor and a mark, which may be set by using C-c C-c (or C-Space if your terminal allows it, or Ctrl with most numeric keys if you're using a modem). Function Binding -------------------------------------- find-file C-x C-f save-buffer C-x C-s save-some-buffers C-x s switch-to-buffer C-x b list-buffers C-x C-b kill-buffer C-x k kill-some-buffers (none) find-alternate-file C-x C-v write-file C-x C-w save-buffers-kill-emacs C-x C-c list-directory C-x C-d insert-file (none) split-window-vertically C-x 2 delete-other-windows C-x 1 kill-window C-x 0 other-window C-x o enlarge-window C-x ^ set-mark C-c C-c write-region (none) ----------------------------------------------------------------------------- A Teaser As mentioned before, this is just an introduction to the world of Emacs. To further the claim that Emacs can do just about everything, here are some of its other features and abilities: * TeX and Nroff modes * Lisp function evaluation * Tag tables * Rectangle cutting, copying, and pasting * Lisp Debugger * Macros * Abbreviations * Built-in mail handler (Rmail) * Shell commands/buffers * Customizing key bindings * Emulation of VMS EDT and UNIX vi editors * Built-in amusements: doctor -- the Eliza program, yow -- for "when you are feeling strange," hanoi -- the "towers of Hanoi" game ----------------------------------------------------------------------------- Getting Further Help For more help with using Emacs, contact the Consulting Lab (CSC room 3326) at 405-1500 or by electronic mail at consult@umail.umd.edu. You can also refer to the Emacs manual described in the introduction that is on reserve in the CSC Program Library. -----------------------------------------------------------------------------