copyright Steve J. Hodges   http://steveh.net/emacs-info.html

Configuring Emacs

Two questions I get asked most regarding emacs configuration are "How do I make the backspace key backspace (instead of bringing up the help menu)?" and "How do I turn on color syntax highlighting?". There are several reasons why these features may not work for you—often a solution lies in configuring emacs itself. In addition, some users would like for the lines of code to be numbered.

the .emacs file

The .emacs file (located in your home directory) contains your settings and extensions for the emacs text editor. The .emacs file uses the eLisp programming language, so emacs supports very powerful customization. You may edit your .emacs file and add any of the following code examples.

To re-map the backspace key

; map backspace to backspace and "C-x ?" to help (define-key global-map "\C-x?" 'help-command) (define-key global-map "\C-h" 'backward-delete-char-untabify)

To activate color syntax highlighting

;syntax hilite (global-font-lock-mode 1) (setq font-lock-maximum-decoration t) (custom-set-faces)


To number the lines without modifying the buffer contents

This is a more complex request with a correspondingly more complicated solution. To implement this, a line numbering program could be written in elisp and added to your .emacs file. One popular solution is setnu.el by Kyle Jones. (official copy) (local copy) For your convenience, I have copied this file to your home directory on Pengo. To "install" this, you could copy the contents of this file to your .emacs file. A better, and more modular, solution would be to keep the setnu.el file separate and include it into your configuration file by adding a load command to your .emacs file. Once you have "installed" setnu, the line numbering can be toggled on and off with the command "M-x setnu-mode".

(load "~/setnu")

Sample .emacs file with many options

This example is given to show that you can make extensive changes to emacs. I'm not recomending the following changes unless you like to experiment!

;;File: .emacs ;MISC SECTION ;; emacs will move down the buffer one ;; line at a time, instead of in larger amounts. (setq scroll-step 1) ;syntax hilite (global-font-lock-mode 1) (setq font-lock-maximum-decoration t) (custom-set-faces) ;show paren, brace, and curly brace "partners" at all times (show-paren-mode t) ;show column number in status bar (column-number-mode t) ;show more info in taskbar/icon than just "Emacs" (setq-default frame-title-format (list "%65b %f")) (setq-default icon-title-format (list "%b")) ;show time on status bar (display-time) ;make the y or n suffice for a yes or no question (fset 'yes-or-no-p 'y-or-n-p) ; don't automatically add new lines when scrolling ; of a buffer (setq next-line-add-newlines nil) ; Ctrl-X, u/l to upper/lowercase regions without confirm (put 'downcase-region 'disabled nil) (put 'upcase-region 'disabled nil) ;show ascii table (defun ascii-table () "Print the ascii table by Alex Schroeder asc@bsiag.com" (interactive) (switch-to-buffer "*ASCII*") (erase-buffer) (insert (format "ASCII characters up to number %d.\n" 254)) (let ((i 0)) (while (< i 254) (setq i (+ i 1)) (insert (format "%4d %c\n" i i)))) (beginning-of-buffer)) ;insert date into buffer (defun insert-date () "Insert date at point." (interactive) (insert (format-time-string "%a %b %e, %Y %l:%M %p"))) ; When bound to C-x C-c, allows you to close an emacs frame ; the same way, whether it's the sole window you have open, ; or whether it's ; a "child" frame of a "parent" frame. ; If you're like me, and use emacs in a windowing environment, ; you probably have lots of frames open at any given ; time. Well, it's a pain to remember to do Ctrl-x 5 0 to ; dispose child frame, and to remember to do C-x C-x to close ; the main frame (and if you're not careful, doing so will ; take all the child frames away with it). This is my solution ; to that: an intelligent close-frame operation that works in ; all cases (even in an emacs -nw session). (defun intelligent-close () "always quit a frame the same way" (interactive) (if (eq (car (visible-frame-list)) (selected-frame)) ;;for parent/master frame... (if (> (length (visible-frame-list)) 1) ;;close a parent with children present (delete-frame (selected-frame)) ;;close a parent with no children present (save-buffers-kill-emacs)) ;;close a child frame (delete-frame (selected-frame)))) ;;a no-op function to bind - to set a keystroke to null (defun void () "this is a no-op" (interactive)) ;;the following snippet was copied from ORA ;;"Writing GNU Emacs Extensions" by Bob Glickstein. (defalias 'scroll-ahead 'scroll-up) (defalias 'scroll-behind 'scroll-down) (defun scroll-n-lines-ahead (&optional n) "Scroll ahead N lines (1 by default)." (interactive "P") (progn (scroll-ahead (prefix-numeric-value n)) (next-line 1))) (defun scroll-n-lines-behind (&optional n) "Scroll behind N lines (1 by default)." (interactive "P") (progn (scroll-behind (prefix-numeric-value n)) (previous-line 1))) ;; buffer-switching methods, Ctrl-TAB and Ctrl-Shift-TAB ;; Original yic-buffer.el ;; From: choo@cs.yale.edu (young-il choo) ;; Date: 7 Aug 90 23:39:19 GMT (defun yic-ignore (str) (or ;;buffers I don't want to switch to (string-match "\\*Buffer List\\*" str) (string-match "^TAGS" str) (string-match "^\\*Messages\\*$" str) (string-match "^\\*Completions\\*$" str) (string-match "^ " str) ;;Test window is visible on a visible frame. ;;I can ALT-TAB to that visible frame, I never ;;Ctrl-TAB to that buffer. That would cause ;;a duplicate top-level buffer inside two frames. (memq str (mapcar (lambda (x) (buffer-name (window-buffer (frame-selected-window x)))) (visible-frame-list))) )) (defun yic-next (ls) "Switch to next buffer in ls skipping unwanted ones." (let* ((ptr ls) bf bn go ) (while (and ptr (null go)) (setq bf (car ptr) bn (buffer-name bf)) (if (null (yic-ignore bn)) ;skip over (setq go bf) (setq ptr (cdr ptr)) ) ) (if go (switch-to-buffer go)))) (defun yic-prev-buffer () "Switch to previous buffer in current window." (interactive) (yic-next (reverse (buffer-list)))) (defun yic-next-buffer () "Switch to other buffer (2nd) in current window." (interactive) (bury-buffer (current-buffer)) (yic-next (buffer-list))) ;;end of yic buffer-switching methods ;C/C++ SECTION ;set up my tab length as a variable (defun get-my-tab-length () 3) ;build a list from 1 to n (defun iota (n) (if (= n 0) '() (append (iota (- n 1)) (list n)))) ;build the tab list (defun create-tab-list (length) (mapcar (lambda (n) (* (get-my-tab-length) n)) (iota length))) (defun my-c-mode-hook () (local-set-key "\M-f" 'c-forward-into-nomenclature) (local-set-key "\M-b" 'c-backward-into-nomenclature) ;set up the tab stop list so Ctrl-I tabs to specific points (setq tab-stop-list (create-tab-list 60)) (setq indent-tabs-mode t) (setq tab-width (get-my-tab-length)) (setq c-basic-offset (get-my-tab-length)) (setq standard-indent (get-my-tab-length)) (setq c-style-variables-are-local-p nil) ;don't give me newline automatically after electric expr (setq c-auto-newline nil) ;if (0) becomes if (0) ; { { ; ; ; ; } } (c-set-offset 'substatement-open 0) ;first arg of arglist to functions: tabbed in once ;(default was c-lineup-arglist-intro-after-paren) (c-set-offset 'arglist-intro '+) ;second line of arglist to functions: tabbed in once ;(default was c-lineup-arglist) (c-set-offset 'arglist-cont-nonempty '+) ;switch/case: make each case line indent from switch (c-set-offset 'case-label '+) ;make the ENTER key indent next line properly (local-set-key "\C-m" 'newline-and-indent) ;syntax-highlight aggressively (setq font-lock-support-mode 'lazy-lock-mode) (setq lazy-lock-defer-contextually t) (setq lazy-lock-defer-time 0) ;make DEL take all previous whitespace with it (c-toggle-hungry-state 1) ;open-braces after a case: indent to 0 (default was '+) (c-set-offset 'statement-case-open 0) ;make a #define be left-aligned (setq c-electric-pound-behavior (quote (alignleft))) ;no restriction lines not top-level indented at least ;1 (was imposed by gnu style by default) (setq c-label-minimum-indentation 0) } (add-hook 'c++-mode-hook 'my-c-mode-hook) (add-hook 'c-mode-hook 'my-c-mode-hook)