Running Linux, 5th Edition - Matthias Kalle Dalheimer [362]
; C-c followed by s will put buffer into SGML mode."
(global-set-key "\C-cs" 'sgml-mode)
Comments in Emacs LISP start with a semicolon. The command that follows runs the command global-set-key. Now you don't have to type in the long sequence M-xsgml-mode to start editing in SGML. Just press the two characters C-c s. This works anywhere in Emacs—no matter what mode your buffer is in—because it is global. (Of course, Emacs may also recognize an SGML or XML file by its suffix and put it in SGML mode for you automatically.)
A customization that you might want to use is making the text mode the default mode and turning on the autofill minor mode (which makes text automatically wrap if it is too long for one line), like this:
; Make text mode the default, with auto-fill
(setq default-major-mode 'text-mode)
(add-hook 'text-mode-hook 'turn-on-auto-fill)
You don't always want your key mappings to be global. As you use TEX mode, C mode, and other modes defined by Emacs, you'll find useful things you'd like to do only in a single mode. Here, we define a simple LISP function to insert some characters into C code, and then bind the function to a key for our convenience:
(defun start-if-block()
(interactive)
(insert "if () {\n}\n")
(backward-char 6)
)
We start the function by declaring it interactive so that we can invoke it (otherwise, it would be used only internally by other functions). Then we use the insert function to put the following characters into our C buffer:
if () {
}
Strings in Emacs can contain standard C escape characters. Here, we've used \n for a newline.
Now we have a template for an if block. To put on the ribbon and the bow, our function also moves backward six characters so that point is within the parentheses, and we can immediately start typing an expression.
Our whole goal was to make it easy to insert these characters, so now let's bind our function to a key:
(define-key c-mode-map "\C-ci" 'start-if-block)
The define-key function binds a key to a function. By specifying c-mode-map, we indicate that the key works only in C mode. There is also a tex-mode-map for mode, and a lisp-mode-map that you will want to know about if you play with your .emacs file a lot.
If you'd like to write your own Emacs LISP functions, you should read the Info pages for elisp, which should be available on your system. Two good books on writing Emacs LISP functions are An Introduction to Programming in Emacs Lisp, by Robert J. Chassell (GNU Press).
Now here's an important customization you may need. On many terminals the Backspace key sends the character C-h, which is the Emacs help key. To fix this, you should change the internal table Emacs uses to interpret keys, as follows:
(keyboard-translate ?\C-h ?\C-?)
Pretty cryptic code. \C-h is recognizable as the Control key pressed with h, which happens to produce the same ASCII code (8) as the Backspace key. \C-? represents the Delete key (ASCII code 127). Don't confuse this question mark with the question marks that precede each backslash. ?\C-h means "the ASCII code corresponding to \C-h." You could just as well specify 8 directly.
So now, both Backspace and C-h will delete. You've lost your help key. Therefore, another good customization would be to bind another key to C-h. Let's use C-\, which isn't often used for anything else. You have to double the backslash when you specify it as a key:
(keyboard-translate ?\C-\\ ?\C-h)
On the X Window System, there is a way to change the code sent by your Backspace key using the xmodmap command, but we'll have to leave it up to you to do your own research. It is not a completely portable solution (so we can't show you an example guaranteed to work), and it may be too sweeping for your taste (it also changes the meaning of the Backspace key in your xterm shell and everywhere else).
There are other key bindings you may want to use. For example, you may prefer to use the keys C-f and C-b to scroll forward (or backward)