.emacs Tidbits
A collection of options and scripts I use or have used in my .emacs files.
Loading
Miscellaneous startup tasks and settings:
; Turn off startup message when loading into scratch buffer
(setq inhibit-startup-message t)
Display
Window Settings
Various useful window settings.
; Turn off menubar if running in text console; keep it in X
(if (not window-system) (menu-bar-mode -1) (menu-bar-mode 1))
; Turn off scrollbars and toolbars
(scroll-bar-mode nil)
(tool-bar-mode nil)
Fonts and Stylizing
; Enable font magic
(custom-set-variables)
(custom-set-faces)
(font-lock-mode 1)
(cond ((fboundp 'global-font-lock-mode)
;; Turn on font-lock in all modes that support it
(global-font-lock-mode t)
;; Maximum colors
(setq font-lock-maximum-decoration t)))
Line and Column Numbers
(line-number-mode t)
(column-number-mode t)
Enable Clock
Adds a clock to the status bar.
;; Turn on the clock!
(autoload 'display-time "time" "clock in status bar" t) ;shut up compiler
(if (locate-library "time")
(progn
(require 'time)
(defconst display-time-day-and-date t)
(defconst display-time-24hr-format t)
(display-time))
(message "Get time.el from your distro."))
Modes
Make text mode the default and register modes for TeX, DAML, OWL, and RDF files:
(setq default-major-mode 'text-mode)
(setq auto-mode-alist
(append '(("\\.tex" . tex-mode))
auto-mode-alist))
(setq auto-mode-alist (cons '("\\.daml$" . sgml-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.rdf$" . sgml-mode) auto-mode-alist))
File Handling
Newlines
This stops emacs from adding newlines when you try to scroll past the bottom of the document, and forces documents to close on a newline. The latter is not always desired, but is my general preference.
(setq next-line-add-newlines nil)
(setq require-final-newline t)
Scripts
Count Words in Region
Borrowed this from somewhere on delorie.com?. It binds Ctrl-x - to display in the minibuffer the number of words in the current region. Very useful in abstract and assignment writing.
;; Stolen from delorie.com
(defun count-words-region (beginning end)
"Print number of words in the region."
(interactive "r")
(message "Counting words in region ... ")
(save-excursion
(let ((count 0))
(goto-char beginning)
(while (and (< (point) end)
(re-search-forward "\\w+\\W*" end t))
(setq count (1+ count)))
(cond ((zerop count)
(message
"The region does NOT have any words."))
((= 1 count)
(message
"The region has 1 word."))
(t
(message
"The region has %d words." count))))))
(global-set-key "\C-x-" 'count-words-region)
Keys
Delete Words
This moved word delete to Ctrl-V, which is more natural for me:
(global-set-key "\C-v" 'backward-kill-word)
Fix Backspace
General
On some of my machines, backspace shows up as Ctrl-D rather than Ctrl-?. This is an easy fix for that:
(global-set-key "\C-D" 'delete-backward-char)
Note that I don't use the delete functionality much anyway.
C/C++ Mode
Worse, for some reason C and C++ modes on some of my machines completely disregard that binding. This is a fix for that problem:
(setq track-c-fix nil)
(defun fix-c-mode()
"Why the hell is this happening?"
(if (eq track-c-fix nil)
(normal-erase-is-backspace-mode))
(setq track-c-fix t)
(message "Backspace fixed...")
)
(add-hook 'c-mode-common-hook 'fix-c-mode)
(add-hook 'c++-mode-common-hook 'fix-c-mode)
Fix Home and End
On some, mostly Windows-oriented machines, the Home and End keys will go to the start and end of the line, which is a waste of keys in emacs. This sets them back to the traditional movement to the top and bottom of the document.
(global-set-key [home] 'beginning-of-buffer)
(global-set-key [end] 'end-of-buffer)
Upcase & Downcase
By default the upcase and downcase keys prompt to make sure you want to run them. These disable that message. Note that this happens automatically after the first time you use them. Upcase is Ctrl-x Ctrl-u; downcase is Ctrl-x Ctrl-l.
(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)
Goto Line
Move goto-line to the more natural Ctrl-x g.
(global-set-key "\C-xg" 'goto-line)