Mike's Emacs Help Page

Emacs is readily customizable by adding packages and modifying a special ".emacs" file. I create a file in my home directory named "emacs/" to store all of the packages, but they can also be directly installed where emacs is installed in the lisp directory. For me that directory is: /usr/share/emacs/21.2/lisp/, but I don't modify that directory so that the changes I make only affect my emacs usage, and so that I don't have to redo everything when emacs is reinstalled or updated.

I suggest taking a look at these following websites:


Here is my .emacs file with explanations:

This sets debugging for playing around with this file.

;; Turn on debugging (comment this out for normal use)
;(setq debug-on-error t)

This sets up ~/emacs/ as the load directory for the packages.

;;load path set to folder ~/emacs for .el files
(setq load-path (cons "~/emacs" load-path)) 

This gets rid of the horribly annoying "bell" and instead flashes the screen.

;; turn on visual bell
(setq visible-bell t) 

I hate the tool bar at the top of the screen, so get rid of it.

;; get rid of the toolbar on top of the window
(tool-bar-mode 0)

;; Show column number at bottom of screen
(column-number-mode 1)

Can be really useful for programming

;; turn on paren matching
    (show-paren-mode t)
    (setq show-paren-style 'mixed)

This doesn't work for me, but does for other people...

;; Turn on mouse wheel
(mouse-wheel-mode t)

I'm too lazy to hit y-e-s-RET, I prefer just y or n.

;; Use "y or n" answers instead of full words "yes or no"
(fset 'yes-or-no-p 'y-or-n-p)

I've always hated that start screen

;; Dont show the GNU splash screen
(setq inhibit-startup-message t)

Useful for programing

;;will make the last line end in a carriage return.
(setq require-final-newline t) 

I like 12 hour time, not 24 hour

;; Display the time in the mode line
;(setq display-time-24hr-format t)
(display-time)

This makes lines not wrap. Must have file .auto-show.el. I decided that I don't like this feature.

;; some more interface stuff, doesn't seem that useful...
;;  (set-default 'truncate-lines t) ;Don't wrap long lines.
;;  (require 'auto-show)            ;But still show their contents
;;  (auto-show-mode 1)
;;  (setq-default auto-show-mode t)

"alt-x setnu-mode" to enable line numbers. Must have file setnu.el

;; SETNU (a mode to show line numbers in the buffer)
;; http://www.wonderworks.com/
(require 'setnu)

A better way for undos and redos like a tape. Undo is the same "control _" and I set redo as "control +". Must have file redo.el.

;; REDO (a better way to handle undo and redo)
;; http://www.wonderworks.com/
(require 'redo)
(global-set-key [(control +)] 'redo)

A switching buffer package that I didn't really like

;; I don't like this one as much because it doesn't reorder the list
;; There is also a swbuff-advice but I couldn't get it to work
;; SWBUFF (buffer switching addition)
;; found it on http://www.cs.virginia.edu/~wh5a/personal/Emacs/
;(require 'swbuff)
;(global-set-key [(control tab)] 'swbuff-switch-to-next-buffer)

The best way to tab through the buffers that I found. It goes in the order of use and shows you the list of buffers you are going through at the bottom. Must have file wcy-swbuff.el.

;; WCY-SWBUFF (buffer tab switching code)
;; found it at http://www.cs.virginia.edu/~wh5a/personal/Emacs/wcy-swbuff.el
(require 'wcy-swbuff)
(global-set-key (kbd "<C-tab>") 'wcy-switch-buffer-forward)
(global-set-key (kbd "<C-S-kp-tab>") 'wcy-switch-buffer-backward)

This code adds some functionality to the centering code when you hit "control-l" so that the first time centers the line, the next time puts the line at the top, and the third time the line is at the bottom of the screen.

;; Centering code stolen from somewhere and restolen from 
;; http://www.chrislott.org/geek/emacs/dotemacs.html
;; centers the screen around a line...
(global-set-key [(control l)]  'centerer)

(defun centerer ()
   "Repositions current line: once middle, twice top, thrice bottom"
   (interactive)
   (cond ((eq last-command 'centerer2)  ; 3 times pressed = bottom
	  (recenter -1))
	 ((eq last-command 'centerer1)  ; 2 times pressed = top
	  (recenter 0)
	  (setq this-command 'centerer2))
	 (t                             ; 1 time pressed = middle
	  (recenter)
	  (setq this-command 'centerer1))))

Opens another buffer with a ascii table in it when you enter "alt-x ascii-table"

;; show ascii table
;; optained from http://www.chrislott.org/geek/emacs/dotemacs.html
(defun ascii-table ()
  "Print the ascii table. Based on a defun 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))

This just adds the date into the code when you type "alt-x insert-date". It looks like this Sun 2005-08-14 - 8:18 PM.

;; insert date into buffer at point
;; optained from http://www.chrislott.org/geek/emacs/dotemacs.html
(defun insert-date ()
  "Insert date at point."
  (interactive)
  (insert (format-time-string "%a %Y-%m-%d - %l:%M %p")))

This function kills all of the buffers but the one's that you specify below. Call it with "alt-x nuke-some-buffers".

;; Kills live buffers, leaves some emacs work buffers
;; optained from http://www.chrislott.org/geek/emacs/dotemacs.html
(defun nuke-some-buffers (&optional list)
  "For each buffer in LIST, kill it silently if unmodified. Otherwise ask.
LIST defaults to all existing live buffers."
  (interactive)
  (if (null list)
      (setq list (buffer-list)))
  (while list
    (let* ((buffer (car list))
	   (name (buffer-name buffer)))
      (and (not (string-equal name ""))
	   (not (string-equal name "*Messages*"))
	  ;; (not (string-equal name "*Buffer List*"))
	   (not (string-equal name "*buffer-selection*"))
	   (not (string-equal name "*Shell Command Output*"))
	   (not (string-equal name "*scratch*"))
	   (/= (aref name 0) ? )
	   (if (buffer-modified-p buffer)
	       (if (yes-or-no-p
		    (format "Buffer %s has been edited. Kill? " name))
		   (kill-buffer buffer))
	     (kill-buffer buffer))))
    (setq list (cdr list))))

This function kills all the buffer except the *scratch* buffer. Call it with "alt-x nuke-all-buffers"

;; Kills all them buffers except scratch
;; optained from http://www.chrislott.org/geek/emacs/dotemacs.html
(defun nuke-all-buffers ()
  "kill all buffers, leaving *scratch* only"
  (interactive)
  (mapcar (lambda (x) (kill-buffer x))
	  (buffer-list))
  (delete-other-windows))

This doesn't seem to work, but would be really cool if it did... Anyway, the file battery.el is required.

;; If not on AC power line, then display battery status on the mode line
 (and (require 'battery nil t)
      (functionp 'battery-status-function)
      (or (equal (cdr (assoc ?L (funcall battery-status-function))) "on-line")
          (display-battery)))

Puts the file name and computer title on the top of the screen.

; make file name and computer title
(set-default 'frame-title-format 
             (list "" "emacs" "@" (getenv "HOST") " : %f" ))

Adds pretty colors.

(setq font-lock-maximum-decoration t)

Sets the tab size to four spaces

(setq-default tab-width 4)

Sets up the python editing mode

;;python editing mode
(setq auto-mode-alist
      (cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist
      (cons '("python" . python-mode)
            interpreter-mode-alist))

(autoload 'python-mode "python-mode" "Python editing mode." t)

Mode for modifying CSS files

;; CSS Mode (editing Cascading Style Sheet files)
;; http://www.garshol.priv.no/download/software/css-mode/index.html
(autoload 'css-mode "css-mode")
(setq auto-mode-alist
     (cons '("\\.css\\'" . css-mode) auto-mode-alist))
; use C-style indenting in CSS mode
(setq cssm-indent-function #'cssm-c-style-indenter)

Lets turn off tabs for many modes

;; Turn off use of tabs for indentation in many modes
(setq indent-tabs-mode nil)


;; LaTeX mode
(add-hook 'latex-mode-hook '(lambda()
                              (setq indent-tabs-mode nil)
                              )
	  )

;; C mode
(add-hook 'c-mode-hook '(lambda()
                          (setq indent-tabs-mode nil)
                          )
	  )

;; C++ mode
(add-hook 'c++-mode-hook '(lambda()
                            (setq indent-tabs-mode nil)
                            )
	  )

;; Fortran mode
(add-hook 'fortran-mode-hook '(lambda()
				(setq indent-tabs-mode nil)
				)
	  )

;; perl mode
(add-hook 'perl-mode-hook '(lambda()
                             (setq indent-tabs-mode nil)
                             )
	  )

;; Lisp mode
(add-hook 'lisp-mode-hook '(lambda()
                             (setq indent-tabs-mode nil)
                             )
	  )

            ;;  (set-background-color "Black")
            ;;  (set-foreground-color "snow")
            ;;  (set-mouse-color "orchid")
            ;;  (set-cursor-color "orchid")
            ;;  (set-face-background 'region "darkred")
            ;;  (set-face-foreground 'region "white")
            ;;  (setq w3-node-style 'font-lock-keyword-face)
            ;;  (setq w3-address-style 'font-lock-comment-face)
            ;;  (setq w3-bold-style 'font-lock-keyword-face)
            ;;  (setq w3-italic-style 'font-lock-comment-face)

This stuff sets up the appearance of emacs when in x windows mode. I'm not sure about a lot the details, but I like the way it looks...

(if (eq window-system 'x)
    (progn
      (transient-mark-mode t)
      
      (if (fboundp 'what\ line) (fmakunbound 'what\ line))
      (if (fboundp 'set\ cursor\ free) (fmakunbound 'set\ cursor\ free))
      (if (fboundp 'set\ cursor\ bound)
          (fmakunbound 'set\ cursor\ bound))
      (if (fboundp 'set\ scroll\ margins)
          (fmakunbound 'set\ scroll\ margins))
      (if (fboundp 'what\ line) (fmakunbound 'what\ line))
      
      (if (x-display-color-p)
          (progn
            (eval-after-load
             "font-lock"
             '(progn
                (setq c-font-lock-keywords    c-font-lock-keywords-2
                      c++-font-lock-keywords  c++-font-lock-keywords-2
                      lisp-font-lock-keywords lisp-font-lock-keywords-2)))

            (global-font-lock-mode t)
            
            (mapcar (function
                     (lambda (flist)
                       (copy-face (car (cdr flist)) (car flist))
                       (set-face-foreground (car flist) (car (cdr (cdr flist))))
))
                          
                    '((comment-color            italic          "orange")
                      (doc-string-color         italic          "turquoise")
                      (string-color             italic          "green")
                      (function-name-color      default         "yellow")
                      (keyword-color            default         "greenyellow")
                      (variable-color           default         "cyan" )
                      (type-color               default         "skyblue")
                      (italic-blue              default         "skyblue")
                      )
                    )
            
            (setq font-lock-comment-face       `comment-color
                  font-lock-doc-string-face    `doc-string-color
                  font-lock-string-face        `string-color
                  font-lock-function-name-face `function-name-color
                  font-lock-keyword-face       `keyword-color
                  font-lock-variable-name-face `variable-color
                  font-lock-type-face          `type-color
                  )
            
    
;            (set-face-foreground 'font-lock-comment-face "saddle brown")
;            (set-face-foreground 'font-lock-doc-string-face "chocolate")
;            (set-face-foreground 'font-lock-string-face "firebrick")
;            (set-face-foreground 'font-lock-function-name-face "blue")
;            (set-face-foreground 'font-lock-keyword-face "slate blue")
;            (set-face-foreground 'font-lock-type-face "steel blue")
            (set-face-foreground 'modeline "black")
            (set-face-background 'modeline "lavender")
;           (set-face-foreground 'font-lock-type-face `type-color)

;;;;;;;;;; Background color
;            (set-background-color "\#0D0A28")
            (set-background-color "Black")
            (set-foreground-color "snow")
            (set-mouse-color "orchid")
            (set-cursor-color "orchid")
            (set-face-background 'region "darkred")
            (set-face-foreground 'region "white")
            (setq w3-node-style 'font-lock-keyword-face)
            (setq w3-address-style 'font-lock-comment-face)
            (setq w3-bold-style 'font-lock-keyword-face)
            (setq w3-italic-style 'font-lock-comment-face)
            )
                                        ; else x-display-color-p
        (if (eq 'gray-scale (x-display-visual-class))
            (progn
              (set-face-background 'region "DarkSlateGrey")
              )
          (progn
            (set-face-background 'region "White")
            (set-face-foreground 'region "Black")
            (setq hilit-background-mode 'mono)
            )
          )
        )
      )
  )

I add some global keys to do some useful things:

adds color

(global-font-lock-mode t)

adds goto line function

(global-set-key "\M-g" 'goto-line)

ispell, a great way to spell check stuff. Need file ispell. Also need to have aspell installed.

(global-set-key "\M-$" 'ispell-complete-word)

Reverts the buffer to the one on disk

(global-set-key [f1]     'revert-buffer)

updates the coloring for comments and such

(global-set-key [f2]     'font-lock-fontify-buffer)

toggles auto save on and off

(global-set-key [f11]    'auto-save-mode)

Toggles the tool bar, which I hate

(global-set-key [f12]    'tool-bar-mode)

sets home to the beginning of the buffer and end to the end of the buffer.

(global-set-key (kbd "<home>") 'beginning-of-buffer)
(global-set-key (kbd "<end>") 'end-of-buffer)

more coloring and such

(mapcar (function
        (lambda (flist)
                (copy-face (car (cdr flist)) (car flist))
                (set-face-foreground (car flist) (car (cdr (cdr flist))))
))
         '((comment-color            italic          "orange")
            (doc-string-color         italic          "turquoise")
            (string-color             italic          "green")
            (function-name-color      default         "yellow")
            (keyword-color            default         "greenyellow")
            (variable-color           default         "cyan" )
            (type-color               default         "skyblue")
            (italic-blue              default         "skyblue")
            )
          )

Set some file endings for different modes

(setq auto-mode-alist
      (append
    '(("\\.ftn$"  . fortran-mode)
      ("\\.for$"  . fortran-mode)
      ("\\.F$"    . fortran-mode)
      ("\\.inc$"  . fortran-mode)
      ("\\.pfp$"  . fortran-mode)
      ("\\.car$"  . fortran-mode)
      ("\\.edt$"  . fortran-mode)
      ("\\.temp$" . fortran-mode)
      ("\\.lex$"  . c-mode)
      ("\\.C$"    . c++-mode)
      ("\\.cc$"   . c++-mode)
      ("\\.c$"    . c++-mode)
      ("\\.h$"    . c++-mode)
      ("\\.cxx$"  . c++-mode)
;      ("\\.html$" . html-mode)
      ("\\.pm$"   . perl-mode)
      ("\\.py$"   . python-mode)
        )
    auto-mode-alist))

Some indenting specifications for different modes

(setq c++-indent-level 4
      c++-continued-statement-offset 4
      c++-brace-offset -4
      c++-argdecl-indent 4
      c++-label-offset -4)

;(setq fortran-do-indent 4
;      fortran-if-indent 4
;      fortran-comment-indent-style nil
;      fortran-continuation-char 38)

; some interesting modes:
(add-hook 'c++-mode-hook (function (lambda () (setq indent-tabs-mode nil) (c-set-style "Ellemtel"))))
                            (setq indent-tabs-mode nil)

Great html mode stuff. Makes a skeleton file for html files when you open a new one. Also adds the date between the lines:

<!-- hhmts start -->
<!-- hhmts end -->

Needs the file html-helper-mode

;;; Auto dating for html files
;;html-helper-mode.el is saved in /cdf/home/Mike/emacs
(autoload 'html-helper-mode "html-helper-mode" "Yay HTML" t)
(setq auto-mode-alist (cons '("\\.html$" . html-helper-mode) auto-mode-alist))
(setq html-helper-do-write-file-hooks t)

Tramp is a wonderful program that opens a local version of emacs and then uses scp to load remote files and then scp again to save them. It is wonderful over slow or intermittant internet connections so that emacs won't close. The only problem is with auto-save, because it takes so long to send the file (which for some reason emacs can't just do in the background), but the last line sets it up to save local copies of the file for auto-save, so that it doesn't take so long. Requires the file tramp.el. To open a remote file, open the file "/[username@computername]filename". Don't forget the beginning / or you will just open a file in the local directory with a really weird name...

;;; Tramp stuff
(add-to-list 'load-path "~/emacs/tramp/lisp/")
(require 'tramp)
(setq tramp-default-method "scpx")
(custom-set-variables
 '(load-home-init-file t t))
(custom-set-faces)
(setq tramp-auto-save-directory "~/emacs/tramp-autosave")

Some functions I wrote to switch back to the last buffer using control-shift-tab. I don't use it because I found the better function, but it is nice if you want to emulate some of the other control tab functions programs have.

;--- Function for Control tab -----
(defun c-tab-buffer () 
  ;allows one to use control tab for buffers
  (interactive) 
  (switch-to-buffer (other-buffer (current-buffer) t)))

;(global-set-key [(control tab)] 'c-tab-buffer)

This function switches between the last three buffers used when you hit control-shift-tab. Again I don't use it...

;--- Function for Control shift tab -----
(defun c-shift-tab-buffer ()
  ;switches to two buffers ago
  (interactive)
  (switch-to-buffer (other-buffer (other-buffer (current-buffer) t)))
) 

;(global-set-key [(control kp-tab)] 'c-shift-tab-buffer)

This is a package that adds tabs to the top of the screen for the different buffers. This works a lot like mrxvt and the tabs in mozilla after the short cuts I added. The bug difference is that are catagories of buffer lists (I define them below) and so you can switch between the buffers in a catagory, the catagories, and between the buffers of all the catagories. Now control-pageup goes back a buffer of the current catagory and control-pagedown goes forward a buffer. Control-shift-left goes back a buffer, control-shift-right forward, but this will go through all of the catagories. Control-shift-up and Control-shift-down, will switch between catagories. The file tabbar.el is required.

;;; Tabbing stuff
(require 'tabbar)
(tabbar-mode) ;comment out this line to start without the tab on top
(global-set-key [(control shift h)] 'tabbar-mode)
(global-set-key [(control shift up)] 'tabbar-backward-group)
(global-set-key [(control shift down)] 'tabbar-forward-group)
(global-set-key [(control shift left)] 'tabbar-backward)
(global-set-key [(control shift right)] 'tabbar-forward)
(global-set-key [(control next)] 'tabbar-forward-tab)
(global-set-key [(control prior)] 'tabbar-backward-tab)

;(global-set-key [(control tab)] 'tabbar-last-selected-tab)

This modifies tabbar to include different catagories, for instance I make a writting catagory for html, text, and latex files...

(defun tabbar-buffer-groups (buffer)
  "Return the list of group names BUFFER belongs to.
Return only one group for each buffer."
  (with-current-buffer (get-buffer buffer)
    (cond
     ((or (get-buffer-process (current-buffer))
          (memq major-mode
                '(comint-mode compilation-mode)))
      '("Misc")
      )
     ((member (buffer-name)
              '("*scratch*"))
      '("Misc")
      )
     ((member (buffer-name)
              '("*Messages*"))
      '("Misc")
      )
     ((member (buffer-name)
              '("*Completions*"))
      '("Misc")
      )
     ((member (buffer-name)
              '("*Ediff Registry*"))
      '("Misc")
      )
     ((eq major-mode 'dired-mode)
      '("Main")
      )
     ((memq major-mode
            '(fundamental-mode help-mode apropos-mode Info-mode Man-mode))
      '("Misc")
      )
     ((memq major-mode
            '(tex-mode latex-mode text-mode xml-mode css-mode))
      '("Writting")
      )
     ((memq major-mode
            '(rmail-mode
              rmail-edit-mode vm-summary-mode vm-mode mail-mode
              mh-letter-mode mh-show-mode mh-folder-mode
              gnus-summary-mode message-mode gnus-group-mode
              gnus-article-mode score-mode gnus-browse-killed-mode))
      '("Mail")
      )
	 ((memq major-mode
            '(perl-mode))
      '("Main")
      )
     (t
      '("Main")
      )
     )))

This is an awesome package that saves the last state of emacs in the directory that emacs was opened in. The next time you open emacs in that directory, it will open all of the files at the last location. All of the following code is required for the save state to work. You are asked if you want to save the state of emacs when emacs closes.

;---Save the State of Emacs to be Loaded next time it is started---
(defun gk-state-saver ()
;;Save names and cursor positions of all loaded files in ".emacs.files"
  (interactive)
  (setq fname (format "%s.emacs.files" gk-startdir))
  (cond 
   ((buffer-file-name)
    (setq currentbuffer (buffer-name)))
   (t
    (setq currentbuffer nil)))
  (cond
   ((y-or-n-p (format "Save state to %s? " fname))
	(switch-to-buffer "*state-saver*")
	(kill-buffer "*state-saver*")
	(switch-to-buffer "*state-saver*")
	(setq bl (buffer-list))
	(while bl
	  (setq buffer (car bl))
	  (setq file (buffer-file-name buffer))
	  (cond 
	   (file 
		(insert "(find-file \"" file "\")\n")
		(switch-to-buffer buffer)
		(setq mypoint (point))
		(switch-to-buffer "*state-saver*")
		(insert (format "(goto-char %d)\n" mypoint))))
	  (setq bl (cdr bl)))
	(cond
	 (currentbuffer
	  (insert (format "(switch-to-buffer \"%s\")\n" currentbuffer))))
	(set-visited-file-name fname)
	(save-buffer)
	(kill-buffer ".emacs.files")
	(cond
	 (currentbuffer
	  (switch-to-buffer currentbuffer))))))


;--- Save state when killing emacs ----------
(add-hook 
 'kill-emacs-hook
 '(lambda () 
    (gk-state-saver)))

;--- Remember from where emacs was started --
(defvar gk-startdir default-directory)
(message "state save directory is: %s" gk-startdir)
;(sleep-for 1)


;--- Load files from .emacs.files -----------
(cond
 ((file-exists-p ".emacs.files")
  (load-file ".emacs.files")))

Last modified: Wed Aug 24 10:31:46 CDT 2005