My Favorite .emacs File Stuff


Configuring emacs is difficult. While this document is not a general reference to elisp or emacs configuration, it is enough to let you get started remapping keys and do a few other things.

Since writing "Emacs' Backspace is Broke" a few years ago a surprisingly large number of folks have sent me email thank-yous for posting that very simple document. I think this is an indication of the average emacs user's frustration with what is a rather steep learning curve on reconfiguring the editor. Below are what I've gleaned from a few years of very casual and occasional piddling around with my .emacs file.

For the still uninitiated, emacs reads a file named .emacs in your home directory each time it starts up. It executes the elisp expressions found in this file, and thus this is the right place to do things such as changing keybindings, etc.

The following lines are from my .emacs file. Explanations of each section of lines are further below.

(define-key global-map "\C-h" 'backward-delete-char)
(define-key global-map "\C-x?" 'help-command)
(define-key global-map [f2]   'replace-string)
(define-key global-map [f3]   'undo)
(define-key global-map [f4]   'repeat-complex-command)
(define-key global-map [f5]   'goto-line)
(define-key global-map [f6]   'delete-char)
(define-key global-map [f7]   'kill-line)
(define-key global-map [f8]   'yank)
(define-key global-map [f9]   'iconify-frame)

(define-key global-map [M-home] 'beginning-of-buffer)
(define-key global-map [C-home] 'beginning-of-buffer)
(define-key global-map [home] 'beginning-of-line)
(define-key global-map [M-end] 'end-of-buffer)
(define-key global-map [C-end] 'end-of-buffer)
(define-key global-map [end] 'end-of-line)

(define-key global-map [C-down]  'scroll-up)
(define-key global-map [C-up]    'scroll-down)
(define-key global-map [C-left]  'beginning-of-line)
(define-key global-map [C-right] 'end-of-line)

(put 'eval-expression 'disabled nil)
(setq suggest-key-bindings t)

(setq c-indent-level 0)
(setq c-continued-statement-offset 2)
(setq c-continued-brace-offset -2)
(setq c-brace-offset 2)
(setq c-brace-imaginary-offset 0)
(setq c-label-offset -2)

(setq perl-tab-to-comment t)

(setq perl-indent-level 0)
(setq perl-continued-statement-offset 2)
(setq perl-continued-brace-offset -2)
(setq perl-brace-offset 2)
(setq perl-brace-imaginary-offset 0)
(setq perl-label-offset -2)


The first 20 lines should be self-explanatory. These lines serve to fix the backspace/delete/Ctrl-H problem, define a new "help" command, define some function-keys which I find convenient, and make home, end, Ctrl+home/end, and Ctrl+up/down/left/right work the way I feel is natural. The 'repeat-complex-command' part is a real disappointment, by the way, since it doesn't usually do what I'd expect/want. But the rest works well.

The "'eval-expression 'disabled" part simply keeps emacs from complaining when I try to do the eval-expression command. Eval-expression (Meta-':' for me) is particularly useful for when you want to try out temporarily a command that might eventually put in your .emacs file. You can paste any of the lines above into eval-expression to see how that particular setting works for you without "committing" to it in the sense of modifying your .emacs file and reloading the editor.

The "suggest-keybindings" line means that when I execute a command by doing Meta-X and typing that command's name, emacs will pause for two seconds or so and flash in the status area the keystroke(s) which I could have also used to access that command. Thus if you forget a keybinding but you can remember the name of the command you can be easily reminded of the keys to press.

The next two groups of lines configure autoindent mode in C and Perl for my own preferred style, which puts braces on the same level of indent as the code they contain -- two characters in from the enclosing scope's indent. I find this style vastly more readable than the standard, and developed it when I used to write very large amounts of C and C++ code.

That style looks like this:
int Foo( int bar )
  {
  int a, b, c;

  if (bar == 3)
    {
    a = 21;
    b = 27;
    c = 32;
    }
  else return 23;

  a++;
  ...
  }

int Baz(void)
  {
  ...
  }


If you want to work out your own keybindings, you'll need to know the name of the key involved, as well as the name of the function you want it to invoke. A good place to start when looking for these is in 'loaddefs.el'. I'm used to this file being in /usr/local/share/emacs/19.34/lisp, though the 19.34 will obviously differ according to what version of emacs you have installed. Another system I use has it in /usr/contrib/lib/emacs/19.28/lisp, for instance.

[The 'locate' command is a good way to find this file -- simply enter 'locate loaddefs.el'. Note that for this to work your system needs to have an up to date locate database. If this isn't the case but you do have locate installed, you may want to ask your sysadmin to occasionally run /usr/libexec/locate.updatedb, or setup a crontab to do so. You can also use 'find' to find loaddefs.el, but a find starting at root is a relatively antisocial thing to do on a multiuser system due to the load it puts on the disks.]

If you're interested in reconfiguring the auto-indent behavior while programming, look at the *-mode.el files in the same directory as loaddefs.el. For Perl you would look at perl-mode.el, for C you would look at c-mode.el, etc.

Finally, I highly recommend Stephen Gildea's Emacs Reference Card (HTML) (in Postscript). I keep a printout of it next to each of my computers. For this and many other emacs resources and links see Jennifer Meyers' Emacs Reference Materials page.


Jason Campbell, April 1999