Customising Emacs
Here’s a recent configuration geared towards Emacs in console mode (emacs -nw / emacs-nox):
;;;;;;;;;;;;;;;;; Load Paths ;;;;;;;;;;;;;;;;;;;
(setq load-path (cons (expand-file-name “~/.emacs-clisp/”) load-path))
;;;;;;;;;;;;;;;;; Require Modes ;;;;;;;;;;;;;;;;;;;
;;–automatically load packages installed through portage
(require ’site-gentoo nil t)
;;–mutt email editing mode for emacs
(require ‘mutt)
;;–adds redo to emacs cf. undo – bound to C-_
(require ‘redo)
;;–convert emacs faces to html
(require ‘htmlize)
;; required for saving session – cursor position in file
(require ’session)
(add-hook ‘after-init-hook ’session-initialize)
;;–load editing modes – acutally loaded by site-gentoo above
;(require ‘ruby-mode)
;(require ‘python-mode)
;(require ‘php-mode)
;(require ‘nxml-mode)
;(require ‘css-mode)
;;–show contents of kill ring
(require ‘browse-kill-ring)
;;–text stats on current buffer – bound to C-ct
(require ‘textstats)
;;–highlight active line – use with highline-on
;(require ‘highline)
;;–highlight blank spaces – use with blank-mode-on
;(require ‘blank-mode)
;;–muttrc
(autoload ‘muttrc-mode “~/.emacs-clisp/muttrc-mode.elc” “muttrc-mode” t ‘nil)
;;–html-helper-mode
(autoload ‘html-helper-mode “html-helper-mode” “HTML major mode.” t ‘nil)
;;;;;;;;;;;;;;;;; Customisations ;;;;;;;;;;;;;;;;;;;
;;–turn on font highlighting
(global-font-lock-mode t)
;;–remove the startup message
(setq inhibit-startup-message t)
;;–highlight selection
(transient-mark-mode t)
;;–highlight matching parenthesis
(show-paren-mode t)
;;–emulates PC delete, backspace, home, end …
(pc-bindings-mode)
;;–emulates PC cursor keys, next, prior, home and end keys
;;(pc-selection-mode)
;;–make searches case-INsensitive
(setq-default case-fold-search t)
;;–do not use tabs for indentation
(setq-default indent-tabs-mode nil)
;;–number of spaces for tab interval
(setq tab-interval 2)
;;–don’t automatically add new lines at end of a buffer
(setq next-line-add-newlines nil)
;;–make sure file ends with NEWLINE
(setq require-final-newline t)
;;–return to same line on a scroll back
(setq scroll-preserve-screen-position t)
;;–highlight search and replace matches
(setq search-highlight t)
;;–highlight words during query replacement
(setq query-replace-highlight t)
;;–turn on the column numbering in the minibuffer
(setq column-number-mode t)
;;–turn on the line numbering in the minibuffer
(setq line-number-mode t)
;;–non-nil means replace should preserve case
(setq case-replace t)
;;–visual bell
(setq visible-bell t)
;;–maximum number of entries in the mark ring
(setq mark-ring-max 32)
;;–turn on line wrapping in text mode
(add-hook ‘text-mode-hook ‘turn-on-auto-fill)
;;–turn on flyspell in text & LaTeX modes
(add-hook ‘text-mode-hook ‘flyspell-mode)
(add-hook ‘LaTeX-mode-hook ‘flyspell-mode)
;;–the column beyond which words wrap
(setq default-fill-column 72)
;;–scroll scroll max lines
(setq scroll-step 1)
;;–automatic scroll lines
(setq scroll-conservatively 1)
;;–show line numbers even in very big files
(setq line-number-display-limit 10000000)
;;–set high garbage collection threshold
(setq gc-cons-threshold 5000000)
;;–set maximum log size
(setq message-log-max 3000)
;;–history elements never deleted
(setq history-length t)
;;–maximum length to which the kill ring can grow – def. 30
(setq kill-ring-max 200)
;;–full name
(setq user-full-name “Firstname Lastname”)
;;–email address
(setq user-mail-address “your@email.com”)
;;–make all “yes or no” prompts show “y or n” instead
(fset ‘yes-or-no-p ‘y-or-n-p)
;;–set aspell as default spell-checker
(setq-default ispell-program-name “aspell”)
;;–ispell settings
(autoload ‘ispell-word “ispell” “Check the spelling of word in buffer.” ‘t)
(autoload ‘ispell-region “ispell” “Check the spelling of region.” ‘t)
(autoload ‘ispell-buffer “ispell” “Check the spelling of buffer.” ‘t)
(setq ispell-dictionary “british” ispell-skip-sgml t)
;;–flyspell settings
(autoload ‘flyspell-mode “flyspell” “On-the-fly spelling checking” t)
(setq flyspell-default-dictionary “british”)
;;–add flyspell to mail-mode
(defun my-mail-mode-hook () (auto-fill-mode t))
(add-hook ‘mail-mode-hook ‘my-mail-mode-hook)
;;;;;;;;;;;;;;;;; Mode Hooks ;;;;;;;;;;;;;;;;;;;
(setq auto-mode-alist
(append
‘(
(”\\.txt$\\|\\.text$\\|\\.doc$” . text-mode)
(”\\.xml$\\|\\.xsl$\\|\\.rng$\\|\\.xhtml$” . nxml-mode)
(”\\.html$\\|\\.ohtml$\\|\\.phtml” . html-helper-mode)
(”\\.htm$\\|\\.asp$” . html-helper-mode)
(”\\.php3$\\|\\.php4|\\.php$$” . php-mode)
(”\\.css\\’” . css-mode)
(”\\.java$” . java-mode)
(”\\.perl$\\|\\.plx$\\|\\.pl$” . cperl-mode)
(”\\.py$” . python-mode)
(”\\.rb$” . ruby-mode)
(”^\/tmp\/mutt” . mail-mode)
(”\\.muttrc” . muttrc-mode)
(”\\.sh$|\\.zsh$|\\.bash$” . shell-script-mode)
(”\\.xinitrc|\\.bashrc|\\.z?profile” . sh-mode)
) auto-mode-alist))
;;;;;;;;;;;;;;;;; Key Bindings ;;;;;;;;;;;;;;;;;;;
;;–wrap text
(global-set-key [f2] ‘fill-paragraph)
(global-set-key [(control f2)] ‘fill-region)
(global-set-key [f27] ‘fill-region)
;;–comment region
(global-set-key [f4] ‘ewd-comment-region)
(global-set-key [(control f4)] ‘ewd-uncomment-region)
(global-set-key [f29] ‘ewd-uncomment-region)
;;–prepend text to region
(global-set-key [f5] ‘ewd-prepend)
(global-set-key [(control f5)] ‘ewd-unprepend)
(global-set-key [f30] ‘ewd-unprepend)
;;–Ispell
(global-set-key [f7] ‘ispell-word)
(global-set-key [(control f7)] ‘ispell-buffer)
(global-set-key [f32] ‘ispell-buffer)
;;–browse kill ring
(global-set-key “\C-xy” ‘browse-kill-ring)
;;–go to specific line in current buffer
(global-set-key “\C-cg” ‘goto-line)
;;–go to specific line in current buffer
(global-set-key “\C-ch” ‘goto-column)
;;–eat space at point up to non-space
(global-set-key “\C-ce” ‘fixup-whitespace)
;;–display text stats for current buffer
(global-set-key “\C-ct” ‘textstats)
;;;;;;;;;;;;;;;;; Misc Functions ;;;;;;;;;;;;;;;;;;;
;;–replace all ^M characters in buffer
(defun fixm ()
“Delete ^M from buffer.”
(interactive)
;;–Remember where we were
(let ((old-point (point)))
;;–Move to top
(set-window-point (get-buffer-window (current-buffer)) 0)
;;–Replace ^M’s
(while (search-forward “\C-m” nil t)
;; (replace-match “\n” nil t)) replace with new lines
(replace-match “” nil t))
;;–Move back to the old point
(set-window-point (get-buffer-window (current-buffer)) old-point)))
;;–convert from DOS > UNIX
(defun dos-unix ()
(interactive)
(goto-char (point-min))
(while (search-forward “\r” nil t) (replace-match “”)))
;;–convert from UNIX > DOS
(defun unix-dos ()
(interactive)
(goto-char (point-min))
(while (search-forward “\n” nil t) (replace-match “\r\n”)))
;;–add a string in front of all lines in the region **
(defun ewd-prepend (start end s)
“Add a string in front of all lines in the region.”
(interactive “*r\nMEnter a string: “)
(save-excursion
(save-restriction
(narrow-to-region
(progn (goto-char start) (beginning-of-line) (point))
(progn (goto-char end) (end-of-line) (point)))
(goto-char (point-min))
(beginning-of-line)
(while (not (eobp))
(insert s)
(forward-line 1)))))
;;–remove a string in front of all lines in the region **
(defun ewd-unprepend (start end s)
“Remove a string from the front of all lines in the region.”
(interactive “*r\nMEnter a string: “)
(save-excursion
(save-restriction
(narrow-to-region
(progn (goto-char start) (beginning-of-line) (point))
(progn (goto-char end) (end-of-line) (point)))
(goto-char (point-min))
(while (not (eobp))
(if (looking-at (regexp-quote s))
(delete-region (match-beginning 0) (match-end 0)))
(forward-line 1)))))
;;–add a comment character in front of all lines in the region **
(defun ewd-comment-region (start end)
“Add one comment character in front of all lines in the region.”
(interactive “*r”)
(or comment-start (setq comment-start (read-input “Comment char?: “)))
(ewd-prepend start end comment-start))
;;–remove a comment character from in front of all lines in the region **
(defun ewd-uncomment-region (start end)
“Remove one comment character from in front of all lines in the region.”
(interactive “*r”)
(or comment-start (setq comment-start (read-input “Comment char?: “)))
(ewd-unprepend start end comment-start))
This configuration relies on the following modes:
muttrc-mode
htmlize
redo
session
highline
browse-kill-ring
textstats
ruby-mode
python-mode
nxml-mode
html-helper-mode
css-mode
php-mode
java-mode
cperl-mode
python-mode
ruby-mode
Most of these should be included with your favourite Linux distribution – in my case they are loaded using Gentoo’s site-gentoo mode
** Credit to Elijah Daniel for the ewd-* functions.