Customising Emacs

Emacs has all sorts of extra modes. Your personal configuration file for emacs is called .emacs. Including lines in such a file affects the way emacs reacts to different file types/situations.

You can switch on font-locking (colours different types of text different colours in LaTeX, FORTRAN, C, C++ etc) with the following lines

(cond ((null window-system))
 ((fboundp 'global-font-lock-mode)
  (setq font-lock-maximum-decoration t)
  (global-font-lock-mode t)
  (if (load "lazy-lock" t)
  (add-hook 'font-lock-mode-hook 'turn-on-lazy-lock))))

and for even more colour in LaTeX (provided you have the tex-site line from above already in before this) add the lines

(if (eq window-system 'x)
(require 'font-latex))

This will give you a default set of colours but it is possible to change them.

The wheel function of the mouse can be activated by adding the line

(mwheel-install)

There is a matlab mode which can be added with the lines

(autoload 'matlab-mode "matlab" "Matlab Editing Mode" t)
(add-to-list 'auto-mode-alist '("\\.m$" . matlab-mode))
(setq matlab-indent-function t)

Other generally useful lines are

;; Highlight selected area
(if (fboundp 'transient-mark-mode)
    (transient-mark-mode 1))
;; and highlight matching parentheses
(require 'paren)
(show-paren-mode t)

;; Go to the matching parenthesis when you press
;; % if on parenthesis otherwise insert %
(defun goto-matching-paren-or-insert (arg)
(interactive "p")
(cond ((looking-at "[([{]") (forward-sexp 1) (backward-char))
      ((looking-at "[])}]") (forward-char) (backward-sexp 1))
       (t (self-insert-command (or arg 1)))))
(global-set-key "%" 'goto-matching-paren-or-insert)

;;visual bell instead of annoying beep
(setq visible-bell t)

;; Prevent Emacs from extending file when
;; pressing down arrow at end of buffer.
(setq next-line-add-newlines nil)
;; Silently ensure newline at end of file
;; (setq require-final-newline t)
;; or make Emacs ask about missing newline
(setq require-final-newline 'ask)

;; next line makes C-x C-j do a 'goto-line'
(global-set-key "\C-x\C-j" 'goto-line)

;; change save interval from 300 to 1000
;; keystrokes so it isn't so annoying
(setq auto-save-interval 1000)

;; Define a search for duplicate words and bind it to F5 key
;; Handy for for spotting errors like this this!
(defun search-duplicates ()
  "Search for two duplicate words in buffer."
  (interactive)
  (search-forward-regexp "\\(\\b\\w+\\b\\)[ \t\n]+\\b\\1\\b"))
(global-set-key [f5] 'search-duplicates)

;; Define a count of the number of words in a highlighted region and bind to F6
;; Handy for forms with word limits and titles with character limits
(defun word-count (start end)
  (interactive "r")
  (let ((words 0) (lines 0) (chars 0))
    (save-excursion
      (goto-char start)
      (while (< (point) end) (forward-word 1) (setq words (1+ words))))
    (setq lines (count-lines start end) chars (- end start))
    (message "Region has  %d lines;   %d words;   %d characters."
             lines words chars)))
(global-set-key [f6] 'word-count)

;; Demonstration of how to perform search and replace on selected area.
;; Handy for re-formatting data, etc.
;; The example below would change
;; [[x1,y1],[x2,y2],[x3,y3]]
;; into
;; x1    y1
;; x2   y2
;; x3   y3
(defun replace-string-region ()
(interactive)
  (let ((beg (point)) (narrow (get 'narrow-to-region 'disabled)))
    (put 'narrow-to-region 'disabled nil)
    (narrow-to-region (point) (mark))
    ;; Perform replace
    (goto-char (point-min))
    (replace-string "],[" "\n")
    ;; repeat replace
    (goto-char (point-min))
    (replace-string "," "\t")
    ;; repeat replace
    (goto-char (point-min))
    (replace-string "[[" "")
    ;; repeat replace
    (goto-char (point-min))
    (replace-string "]]" "")
    ;; TIDY UP
    (widen)
    (goto-char beg)
    (put 'narrow-to-region 'disabled narrow)
    (message "Done")))
(global-set-key [f7] 'replace-string-region)