[Download postscript version]
next up previous
Next: Getting more information Up: Emacs Previous: Writing Keyboard Macros

.emacs File

The .emacs file is located in your home directory and it is loaded when emacs is started. It contains initializations and user defaults. It is common to define own key shortcuts, colors and fonts. A sample .emacs file can be loaded from [Per96].

At this place, I would like to give an example of how to use macros in conjunction with the .emacs file. A common task during programming is to write function headers. It is very practical to write only the function definition in the .h file, copy it to the .c file and apply a pre-written macro with a key shortcut.

We have the following function definition:

    void
deb_printf(const int level, const char *fmt, ...);

The definition for the prototype_to_function macro in the .emacs file looks as follows:

(fset 'prototype_to_function
   "^S(^B^@^A\367^P^P
/*^U77-^M * ^Y
*
* input:
*
* output: returns SUCCESS/FAILURE 
*
* sideeffects:
*
*^U77-
*/^S;^H^N^A{
return SUCCESS;}^H /* ^Y *./^?^?/^M")

(global-set-key "$\backslash$C-cn" 'prototype_to_function)

This definition looks very cryptic, but we actually never really need to edit or write the macro, as emacs takes care of this, when you use the commands in section 2.5. Typing C-c n with the cursor on the deb_printf name gives us the desired transformation:

/*-----------------------------------------------------------------------------
 * deb_printf
 *
 * input:
 *
 * output: returns SUCCESS/FAILURE
 *
 * sideeffects:
 *
 *-----------------------------------------------------------------------------
 */
    void
deb_printf(const int level, const char *fmt, ...) 
{
    return SUCCESS;
} /* deb_printf */

Such macros are very practical, they can save us a great deal of typing and speed up the work.

Setting colors for a given screen and taste can be difficult. Emacs provides the commands M-x list-faces-display to display the current font settings and M-x list-color-display to show the available colors. Syntax highlighting can be turned on with the command M-x font-lock-mode or automatically with a command such as (add-hook 'c-mode-hook '(lambda () (font-lock-mode 1))) in the .emacs file.

If you use emacs for programming, line numbers are often used. I have the following lines in my .emacs file:

(line-number-mode 1)
(global-set-key "\C-cg" 'goto-line)
The first line displays always the current line number in the mode line. If you need to jump to a specific line, you can just use C-c g, which I use quite often.


next up previous
Next: Getting more information Up: Emacs Previous: Writing Keyboard Macros

Adrian Perrig
Wed Jun 12 00:18:25 MET DST 1996