Previous | Table of Contents | Next |
A keymap tells Emacs what definition to call when you press a key or mouse button. Thus, in Text mode, typing the a key runs self-insert- command, which inserts the letter a. Typing the keychord C-a runs beginning-of-line, which moves point to the beginning of the current line.
A key such as C-x 4 b (switch-to-buffer-other-window) is a three-event sequence. The first two events make up the prefix key. In this case, the prefix key consists of pressing the C-x key and then the 4 key. The complete key consists of the whole sequence.
Often, two or three keymaps are active (in use) in a buffer at the same time. First, there is the global map, which is shared by all buffers. Second, there is the local keymap associated with the buffers major mode, such as Text mode, C mode, Mail mode, or Calc mode. Finally, there may be a minor mode keymap, such as Outline minor mode. Local bindings shadow (overrule) the corresponding global bindings, and minor mode bindings shadow the local and global bindings.
You can set a key in several different ways. Most simply, you can use the global-set-key function in your .emacs initialization file.
Here, for example, we rebind C-h a from the default function command-apropos to apropos:
;;; Use apropos instead of command-apropos (global-set-key \C-ha apropos)
command-apropos shows all the commands, that is, all interactively callable functions, that match a regular expression. This leaves out those functions that are not interactive. apropos, on the other hand, shows all bound symbols whose names match a regular expression. If you are using Emacs as a writing environment, command-apropos is appropriate because it helps you find commands you use. But if you are programming in Emacs Lisp, you may find apropos more relevant because it provides more information.
Note that the syntax \C- means that the following character is a control character. Thus, \C-h stands for the prefix key C-h. \M- means that the following character is a meta character.
Instead of the global-set-key expression, you can do the following:
(define-key global-map \C-ha apropos)
The global-set-key command is an interactive function that checks whether the key is in the correct format and then evaluates the expression:
(define-key (current-global-map) key command)
in which key and command are the arguments you pass global-set-key.
The define-key built-in function, on the other hand, does not check format and is not interactive. (You can read the source for global-set-key in subr.el and the source for define-key in keymap.c.)
A function key is specified in brackets, as in the following example. (See the section Rebinding Function Keys in The GNU Emacs Lisp Reference Manual.) This sets the function key F3 to run the view-buffer command:
(global-set-key [f3] view-buffer)
A mouse click is also listed in brackets, like this:
[mouse-2]
for a click on the second mouse button, which is usually the middle button. (Mouse button events can be fairly complex, with codes for initial, drag, and repeat events, but you can do simple things simply, as shown in the following example. See the section Input Events in The GNU Emacs Lisp Reference Manual for more information.)
Here is an example, taken from my .emacs initialization file, of how to bind a middle button mouse click to a command to select the message you click on in your RMAIL-summary buffer:
;;; [mouse-2] in RMAIL-summary to goto message (defun rmail-summary-mouse-select (event) Select the message whose line you click on. (interactive e) (goto-char (posn-point (event-end event))) (rmail-summary-goto-msg)) (define-key rmail-summary-mode-map [mouse-2] rmail-summary-mouse-select)
First, define an interactive function; this uses the e argument to interactive, which handles events. Then, define the key for the local RMAIL-summary mode map, using define-key.
GNU Emacs has two modes for editing Emacs Lisp: Lisp Interaction mode and Emacs-Lisp mode. Lisp Interaction mode is for an interactive session with Emacs Lisp, and Emacs-Lisp mode is for editing source files of programs to run in Emacs Lisp. The two modes are nearly identical.
Like other modes, the Lisp modes provide help in carrying out a somewhat specialized activity, editing Lisp in the case of the Lisp modes, editing text in the case of Text mode, or working out dates in the case of Calendar mode.
The Lisp modes provide M-C-q (indent-sexp) and M-C-\ (indent-region) to help you format expressions properly, provide M-TAB (lisp-complete- symbol) to automatically complete the name of a symbol, provide M-C-x (eval-defun) to evaluate a function definition, and provide C-x C-e (eval-last-sexp, which works in any mode) to evaluate any expression.
In addition, in Lisp Interaction mode the C-j key or Line Feed key is bound to eval-print-last-sexp. This means you can position point after an expression, press C-j (or the LFD key if your keyboard has it), and the Lisp interpreter reads and evaluates the expression before point and prints the returned value in the buffer. This is like typing C-u C-x C-e, but simpler.
Emacs Lisp mode has several keys for working with Edebug. (See section 2.19.2 for more information.)
Often, you will begin writing code in the *scratch* buffer (the Lisp Interactive mode buffer that appears when you start Emacs) because it is simplest and then save that code in a file for later use.
By convention, Emacs Lisp code is stored in files whose names end in .el, which tells Emacs to enter Emacs-Lisp mode when you visit the file.
In addition to the specialized Lisp commands, the Lisp modes provide all the usual editing features: incremental search, regular expression search, replacement, copy, cut, paste, and so on. See the section Introduction in The GNU Emacs Manual.
Previous | Table of Contents | Next |