Computer Laboratory

Common tasks

...being a collection of pieces of Lisp code which can be placed in your .emacs file to do useful things. If your favourite isn't in here please let me know: ckh@cl.cam.ac.uk.

1. General Behaviour

1.1 Don't add lines at end of file

- the next-line function by default automatically adds new lines when you run past the end of a file. This turns that behaviour off.

(setq next-line-add-newlines nil)

1.2 Set the column beyond which automatic line-wrapping should happen

(setq-default fill-column 72)

1.3 Get rid of annoying beeps when you do something wrong

(it substitutes a slightly less annoying screen blink)

(setq visible-bell t)

1.4 Enable the use of the command eval-expression without confirmation

(put 'eval-expression 'disabled nil)

1.5 Make searches case sensitive by default

(in all buffers that do not override this).

(setq-default case-fold-search nil)

1.6 Specify your own email address

if emacs can't figure it out correctly.

(setq user-mail-address "whoever@cl.cam.ac.uk")

1.7 Highlight a region of text

(transient-mark-mode t)

1.8 Highlight different areas of syntax

in C mode:

(add-hook 'c-mode-hook 'turn-on-font-lock)
in all modes:
(global-font-lock-mode 1)

2. Key Binding

2.1 Redefine some keys

(global-unset-key "\M-z")
(global-unset-key "\M-a")
(global-set-key "\M-z" 'end-of-buffer)
(global-set-key "\M-a" 'beginning-of-buffer)

2.2 Rebind ctrl-2 to set mark

(ie mimic emacs-18 behaviour)

(global-set-key  [?\C-2] 'set-mark-command)

2.3 Bind some function keys

(global-set-key [f1] 'scroll-down)
(global-set-key [f2] 'scroll-up)

2.4 Make C-x C-v undefined

(global-unset-key "\C-x\C-v")

2.5 Rebind the key C-x l to run the function make-symbolic-link

(global-set-key "\C-xl" 'make-symbolic-link)

or

(define-key global-map "\C-xl" 'make-symbolic-link)

2.6 Rebind the key C-x l to run the function make-symbolic-link for Lisp mode only

(define-key lisp-mode-map "\C-xl" 'make-symbolic-link)

2.7 Redefine all keys which now run next-line in Fundamental mode

so that they run forward-line instead.

(substitute-key-definition 'next-line 'forward-line
                           global-map)

3. Modes

3.1 Disable the default C mode

(setq auto-mode-alist
      (apply 'append
             (mapcar '(lambda (x) (if (equal (car x) "\\.c\\'") nil (list x)))
                     auto-mode-alist)))

3.2 Make TAB in C mode just insert a tab

if point is in the middle of a line.

(setq c-tab-always-indent nil)

3.3 Make Text mode the default mode for new buffers

(setq default-major-mode 'text-mode)

3.4 Turn on Auto Fill mode automatically

in Text mode and related modes.

(add-hook 'text-mode-hook
  '(lambda () (auto-fill-mode 1)))
or
(add-hook 'text-mode-hook 'turn-on-auto-fill)
or, to turn it on in all modes
(setq-default auto-fill-function 'do-auto-fill)

3.5 Show parenthesis matches

(show-paren-mode 1)

4. Packages

4.1 Add another directory to the file search path

(eg the directory ~yourusername/lisp)

(setq load-path (append
  (list (expand-file-name "~yourusername/lisp"))
    load-path))

4.2 Load an installed Lisp library

named foo (actually a file foo.elc or foo.el in a standard emacs directory).

(load "foo")

4.3 Load a compiled Lisp file

named foo.elc from your home directory.

(load "~/foo.elc")

4.4 Load the AucTeX package for work with TeX/LaTeX files

(require 'tex-site)

4.5 Load the calc package

(sort of a poor-man's Mathematica)

(require `calc)

5. Appearance

5.1 Change the initial startup behaviour so the the minibuffer is separate

You will probably have to play with the widths and height values to tailor them for your screen.

(setq initial-frame-alist 
  '((top . 1) (left . 1) (width . 100) (height . 60) (minibuffer . nil)))
(setq minibuffer-frame-alist
  '((top . 1) (left . 1) (width . 180) (height . 2)
    (name . "Minibuffer") (user-position . f)))
(setq default-frame-alist 
  '((top . -1) (left . 1) (width . 80) (height . 15) (minibuffer . nil)))

5.2 Put a vertical scroll bar on the right

(custom-set-variables
 '(scroll-bar-mode (quote right)))

5.3 Add line numbers to the mode line

ie put the line number you currently have the input cursor on into the black line at the bottom of a buffer.

(line-number-mode 1)

5.4 Add column numbers to the mode line

(setq column-number-mode t)

5.5 Set the background colour

This can also be done with .Xresources, but this is easier

;;use the Unix command showrgb to see a list of colour names
(set-background-color "ivory")

5.6 Set the text colour

;;use the Unix command showrgb to see a list of colour names
(set-foreground-color "slateblue")