EMACS

From JasonAntmanWiki
Jump to: navigation, search


GNU EMACS (Wikipedia) is a free (as in speech, and beer) text editor. It is highly extensible via the LISP language, supports syntax correction and highlighting for many languages, and has both text-mode and graphical versions.

Contents

Quick Reference to Keystrokes

As per convention, keystrokes are listed with C- standing for Ctrl and M- standing for the Meta key (or Alt). Therefore, C-x would be a simultaneous press of the Ctrl and "x" keys.

I've been using Emacs for a bit over a year now, and most keystrokes have become second-nature. As a matter of fact, when using a friend's computer running Windows and Microsoft Word, I found myself perplexed as I thought "save and exit" and found myself repeatedly banging C-x C-s on the keyboard.

While I will attempt to include a few basic commands, most of what will end up here are the ones so seldom used that I forget them.

Please note that this is a quick reference for people who already are familiar with the nomenclature and function of emacs.

Simple Keystrokes

Save buffer C-x C-s
Quit C-x C-c
Beginning of line C-a
End of line C-e
Set Mark C-(space)
Kill line C-k (add everything after pointer to killring)

More seldom-used ones

Keystroke Mapping

In your .emacs file, you can re-map some (or all) of the keystrokes.

I have mapped some more common functions to the F-keys:

;; DEFINE the FUNCTION KEYS
(global-set-key [f1] 'other-window)
(global-set-key [f2] 'list-buffers)
(global-set-key [f3] 'split-window-vertically)
(global-set-key [f4] 'undo)
(global-set-key [f5] 'goto-line)
(global-set-key [f6] 'shell)
(global-set-key [f7] 'compile)
(global-set-key [f8] 'next-error)
(global-set-key [f9] 'vc-next-action)
(global-set-key [f10] 'diff-mode)
(global-set-key [f11] 'delete-window)
(global-set-key [f12] 'kill-this-buffer)

While I don't use all of them all the time, the most useful ones (for me) are F4, F5, F6, F7, and F9.

.emacs

The .emacs file, commonly found at ~/.emacs, you can add as much customization as you like.

One use is Keystroke Mapping. You can also add many other features. Here are a few that I have added:

Automatic Time Stamping

This will automatically insert a time-stamp in each file, and update it on saves. It inserts the time-stamp at the location of the first occurrence of the text Time-stamp: " " in the first ten or so lines of a file. So, if I am writing a PHP script and include // Time-stamp: " " in my usual header, on the first save this will become // Time-stamp: "2007-07-18 01:44:13 jantman", a time stamp with my username.

;; setup automatic time-stamping
(if (not (memq 'time-stamp write-file-hooks))
    (setq write-file-hooks
          (cons 'time-stamp write-file-hooks)))

Java Compilation

This sets the default compile command to javac. It enables me to quickly (with Keystroke Mapping) press F7 and compile the current buffer with javac (being that Java is the only compiled language I normally code in).

;; COMPILATION settings

(add-hook 'java-mode-hook
	  (lambda()
	    (set (make-local-variable 'compile-command) (concat "javac " (buffer-name)))))

PHP Mode

This code allows use of syntax highlighting and correction for PHP. It requires the php-mode LISP file (php-mode.el) to be installed in your ~/emacs/ directory. PHP Mode can be obtained from the SourceForge project here.

;;load the php-mode LISP
(require 'php-mode)
(add-hook 'php-mode-user-hook 'turn-on-font-lock)

My Full .emacs

This is my current .emacs file, available on my web site and installed on every user account I have on every *nix machine that I use.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; File name: ` ~/.emacs '
;;; ---------------------
;;;
;;; If you need your own personal ~/.emacs
;;; please make a copy of this file
;;; an placein your changes and/or extension.
;;;
;;; Copyright (c) 1997-2002 SuSE Gmbh Nuernberg, Germany.
;;;
;;; Author: Werner Fink, <feedback@suse.de> 1997,98,99,2002
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Test of Emacs derivates
;;; -----------------------
(if (string-match "XEmacs\\|Lucid" emacs-version)
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;; XEmacs
  ;;; ------
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  (progn
     (if (file-readable-p "~/.xemacs/init.el")
        (load "~/.xemacs/init.el" nil t))
  )
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;; GNU-Emacs
  ;;; ---------
  ;;; load ~/.gnu-emacs or, if not exists /etc/skel/.gnu-emacs
  ;;; For a description and the settings see /etc/skel/.gnu-emacs
  ;;;   ... for your private ~/.gnu-emacs your are on your one.
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  (if (file-readable-p "~/.gnu-emacs")
      (load "~/.gnu-emacs" nil t)
    (if (file-readable-p "/etc/skel/.gnu-emacs")
	(load "/etc/skel/.gnu-emacs" nil t)))

  ;; Custum Settings
  ;; ===============
  ;; To avoid any trouble with the customization system of GNU emacs
  ;; we set the default file ~/.gnu-emacs-custom
  (setq custom-file "~/.gnu-emacs-custom")
  (load "~/.gnu-emacs-custom" t t)
;;;
)
;;;

;;load files from ~/emacs
(setq load-path (append load-path (list "~/emacs")))

;;load the php-mode LISP
(require 'php-mode)
(add-hook 'php-mode-user-hook 'turn-on-font-lock)

;; COMPILATION settings

(add-hook 'java-mode-hook
	  (lambda()
	    (set (make-local-variable 'compile-command) (concat "javac " (buffer-name)))))

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

;; define users email
(setq user-mail-address "jason@jasonantman.com")
;; define user's full name
(setq user-full-name "Jason Antman")
;; show line numbering
(line-number-mode 1)

;; setup automatic time-stamping
(if (not (memq 'time-stamp write-file-hooks))
    (setq write-file-hooks
          (cons 'time-stamp write-file-hooks)))

;; DEFINE the FUNCTION KEYS
(global-set-key [f1] 'other-window)
(global-set-key [f2] 'list-buffers)
(global-set-key [f3] 'split-window-vertically)
(global-set-key [f4] 'undo)
(global-set-key [f5] 'goto-line)
(global-set-key [f6] 'shell)
(global-set-key [f7] 'compile)
(global-set-key [f8] 'next-error)
(global-set-key [f9] 'vc-next-action)
(global-set-key [f10] 'diff-mode)
(global-set-key [f11] 'delete-window)
(global-set-key [f12] 'kill-this-buffer)

;; disable C-x, C-z
(global-unset-key [(control x) (control z)])   ; normally suspends or minimizes emacs (!)
;; enter the debugger every time an error is found in .emacs on load
(setq debug-on-error t)

External Links

GNU EMACS homepage
PHP Mode Homepage

Views
Notice - this is a static HTML mirror of a previous MediaWiki installation. Pages are for historical reference only, and are greatly outdated (circa 2009).