Dan Torop

some useful Emacs Lisp commands

commandmeaningexample
(setq VARIABLE VALUE) store VALUE in VARIABLE (setq foo 23) or
(setq foo "Testing")
(+ NUMBER-A NUMBER-B) return sum of the numbers (+ 5 17) ==> 22
(- NUMBER-A NUMBER-B) return difference between the numbers (- 23 8) ==> 15
(* NUMBER-A NUMBER-B) return product of the numbers (* 3 4) ==> 12
(> A B) return t if A is greater than B, otherwise nil (> 15 10) ==> t
(< A B) return t if A is less than B, otherwise nil (< 22 12) ==> nil
(= A B) return t if A is equals B, otherwise nil (= 15 12) ==> nil
(nth N LIST) return the Nth item from LIST, with N=0 as the first item (nth 2 '(A B C D)) ==> C
(read) prompt for input, and return that (set foo (read))
(random MAX) return a number between 0 and MAX-1 (set foo (random 10))
(insert "TEXT") place TEXT in the current buffer (insert "hello")
(newline) insert a newline character into the current buffer (newline)
(backward-char NUM) move cursor back NUM characters (backward-char 5)
(point) return the character in the file at which the cursor is (point)
(goto-char NUM) goto character # NUM from start of file (goto-char 1) or
(goto-char (+ 5 (point))
(while TEST
  COMMANDS)
execute COMMANDS over and over so long as TEST is t
(while (> foo 0)
  (newline)
  (setq foo (- foo 1)))
(if CONDITION
  TRUE-COMMAND)
if CONDITION is t run TRUE-COMMAND
(if (> foo 4)
  (insert "happy days"))
(if CONDITION
  TRUE-COMMAND
  ELSE-COMMANDS)
if CONDITION is t run TRUE-COMMAND, otherwise run ELSE-COMMANDS
(if (> foo 4)
  (insert "happy days")
  (insert "some other time"))
(dotimes (VARIABLE COUNT)
  COMMANDS)
execute COMMANDS COUNT times, incrementing VARIABLE from 0 to COUNT-1 each time
(dotimes (x 5)
  (newline))
(dolist (VARIABLE LIST)
  COMMANDS)
execute COMMANDS once for each item in LIST, with VARIABLE set to that item
(dolist (note '("do" "re" "mi"))
  (insert note))
(defun NAME (INPUT-VARIABLES)
  COMMANDS)
define a function called NAME, which places its options into INPUT-VARIABLES, and executes COMMANDS when called
(defun many-newlines (how-many)
  (dotimes (x how-many)
    (newline)))