Lisp was created by John McCarthy in 1958 at the Massachusetts Institute of Technology (MIT). Lisp is the second-oldest high-level programming language (Fortran beat it by one year).
- Basics
- "set" assigns a value to a symbol
(set 'fname "Mitch")
Since quoting the symbol name is so common a second assignment operator was created, "setq"
- "setq" sets the value of a variable,
(setq fname "Mitch")
but can take multiple arguments,
It returns the last value set.(setq varname value [varname value]...) (setq fname "Mitch" lname "Fincher")
- "set" assigns a value to a symbol
- ";" is the comment character
; I'm a comment (setq fname "Mitch") ; and so am i
- List operators
A List is a sequence of zero or more lisp expressions enclosed in parens.
Function Description Example car returns the first element of the list (car '(a b c d)) => a
cdr returns the list without the first element (cdr '(a b c d)) => (b c d)
list makes a list from its arguments (list 'a 'x "asdf" 6) => (list a x "asdf" 6)
cons prepends its first argument to its second (cons '(a b) '(c d)) => ((a b) c d)
append strips the outer parens from its args and then
munges all of them into a single list(append '(a b) '(c d (e))) => (a b c d (e))
reverse reverses a list's top level elements (setq x '(a b c d))(reverse x) => (d c b a)
length returns number of top level list elements (length '(a b c (e f))) => 4
nthcdr returns the 'nth' cdr (nthcdr 2 '(a b c d e)) => (c d e)
nth returns the nth element of a list (nth 2 '(a b c d e)) => c
mapcar given a function and a list, it calls
the function on each element of the list and returns a list(mapcar 'length '((a)(a b)(a b c))) => (1 2 3)
apply (apply 'max '( 3 6 1))> 6
setcar sets the value of the car in a cons cell (setq a (cons 'x 'y)) => (x . y)
(setcar a 'w) =>(w . y)
setcdr sets the value of the cdr in a cons cell (setq a (cons 'x 'y)) => (x . y)
(setcdr a 'w) =>(x . w)
sort sorts a list (sort '(5 3 9 27) '<) => (3 5 9 27)
Note on characters: Characters are represented by prefacing them with "?". ?a denotes the character a. Characters can also be represented by their ascii integer value.
- String functions
Function Description Example (string= s1 s2) compares two strings for equality (string= "asdf" "asdf")=> t (stringp x) tests if arg is a string (stringp 3) => nil (concat a b [c...]) concats its args together (concat "I " "went to " "the woods ")=> "I went to the woods " (length s) returns the length of s (length "I went to the woods ")=> 20 (aref s i) returns the ith char (0 based) (aref "abcd" 2)=> 99 (aset s i ch) sets the nth char (aset "run" 1 ?a) => (substring s from [to]) (substring "Gaul is" 0 4) => "Gaul" - List background.
Lists are composed of smallers units called 'cons cells'. A cons cell has two parts, a 'car' and a 'cdr'. Lists are made by having data in the 'car' part and the 'cdr' part pointing to another cons cell. The last cdr of a proper list is nil. - Data Types
- Integer
- Floating Point
- Character
- Symbol
- Sequences:
- List
- Array
- String
- Vector
- Arithmetic
Function Description Example + addition (+ 1 2 3 4) => 10 - subtraction (- 10 1 2 3) => 4 max returns largest of arguments (max 14 99 18 44 ) => 99 min returns smallest of arguments (min 14 99 18 44 ) => 14 / division (/ 16 2)=> 8 * multiplication (* 4 2 3) => 24 % modulo (remainder) (% 5 2)=> 1 - Logical functions (and, or, not, eq)
Function Description Example or evaluates args in order and returns first true one (or nil 5) => 5
= tests if two numbers are equal (= 1 2) => nil
/= tests if two numbers are not equal (/= 1 2) => t
>,<.>=,<= kinda what you would think (< 1 2) => t
numberp Is argument a number? (numberp 5)=> t
vectorp Is argument a vector? (vectorp 5)=> nil
consp tests if its argument is a cons cell (consp '(a b)) => t
(consp "asdf") => nilatom tests if its argument is an atom (atom '(a b)) => nil
(atom "asdf") => tlistp tests if its arg is a list
true for cons cells and nil(listp '(a b)) => t
(listp nil) => tnull tests if its argument is nil (null nil) => t
(null 7) => nilbolp is point at the beggining of a line eolp is point at the end of a line bobp is point at the beggining of a buffer eobp is point at the end of a buffer => (setq a nil) (setq b "mike") (setq c "jeremiah") (and b c ) ;; if all are true, it returns the last true value = "jeremiah" (and b a c ) ;; = nil
eq takes two args and tests if they are the same object (eq 23 23) => t
equal takes two args and tests if the values are equal
(if (equal "Monday" (format-time-string "%A" (current-time))) (insert "- timesheet\n"))
- Conversion
number-to-string string-to-number char-to-string int-to-string - Condition structures
- if statement
(if test then else1, else2,...)
If test is true the then clause executes. If test is false all the else clauses execute.
Example:(setq testme nil) (if testme (setq a "1") (setq a "3") (setq a "2") ) (message a)
when testme is nil, the value of a is "2". When testme is true a is "1"
Note: "if" returns the value of the last expression executed.
- cond
cond returns the first true condition
(setq home-dir-emacs (cond ((string= emacs-version "22.1.1") "c:/opt/gnu/emacs-22.1/") ((string= emacs-version "21.3.1") "c:/opt/gnu/emacs-21.3/") ))
- if statement
- Function Template
(defun function-name ([arg1 ... argN]) "optional, but encouraged documentation". (interactive cryptic-argument-passing-codes) body...)
any arg with a type embedded in the name (ie, integer1) is assumed to be of that type.
- Errors
(defun errortest() (if nil (error "something is wrong in errortest") (message "everything is peachy")) )
- Hooks
(add-hook 'find-file-hooks 'display-file-info) (add-hook 'java-mode-hook '(lambda () (turn-on-font-lock) (modify-syntax-entry ?_ "w") ;asdf_asdf (modify-syntax-entry ?- "w") ;asdf-asdf (c-toggle-auto-state t) (c-toggle-auto-hungry-state t) (define-key c-mode-map "\ea" nil) ;; use global value (define-key c-mode-map "\el" nil) ;; use global value )) ;; to remove the hook (remove-hook 'find-file-hooks 'display-file-info)
Commands and functions can also have hooks. These are created using "defadvice".
- let
creates a temporary space for vars.
(setq a "apple") (let ((b "banana") (a "avacado")) (message a)(sit-for 1)) (message a)
This will display "avacado" and then "apple"
uninitialized variables may also be used.
(let ((b "banana") (a "avacado") c d)
This creates temporary variables "c" and "d", but assigns no value. - let* ;; forces sequential execution
- The yes-or-no-p function
(defun nice-day-p () (if (yes-or-no-p (format "Did you have a good day?")) (message "great") (message "sorry"))) (nice-day-p)
- defvar
syntax (defvar varname defaultvalue "documentation string")
defvar is like setq, but offers some advantages like a description and the defaultvalue is set only if the varname is undefined.
if the documentation string begins with a "*", the variable is user definable with the M-xset-variable command
- Window Positioning functions
- (point) This is the number of characters into the buffer we are. (point) returns the value.
- (window-start) returns the position of the character in the upper left corner.
- (goto-char n) takes cursor to the nth character in the buffer
- (set-window-start nil n) ;;sets the window start of the current buffer to n.
- Useful, but hard to remember misc key sequences and commands:
nxml-mode is a more powerful xml-mode. To use by default: (add-to-list 'auto-mode-alist '("\\.xml\\'" . nxml-mode)) when deleted a file goes to the OS's trash folder: (setq delete-by-moving-to-trash t) whitespace-mode ;; to see tabs and spaces delete-matching-lines ;;removes lines matching a regex C-s C-w ;; search for the word under the cursor, multiple C-w widens search target (setq mouse-wheel-scroll-amount '(1)) ;; set the mouse scroll wheel to move 1 line per event (setq fill-column 72) ;; set the wrap margin column (setcdr (nthcdr (1- (length my-list)) my-list) nil) ;; kill last item in list C-u - M-xfont-lock-modeRET ;; turns off any mode (font-lock-mode is example) C-u 1 M-xfont-lock-modeRET ;; turns on any mode describe-mode ;; tells all about the current minor and major modes copy-to-buffer ;; copies current region to a file M-xedit-options ;; to see all the user definable variables - way cool C-Xb *ftp[TAB] ;;; to get to the ftp buffer C-XC-E eval-last-sexp in a lisp buffer C-y then M-y ;;; to go thru the yank ring In keyboard macros, if you need to copy part of a block of text and then paste it later, you can you something like, ;; move forward through the file set-mark-command ;; set the mark to be used as the beginning of region copy-region-as-kill ;;; copies the text into the clipboard ;; move to where the text is needed clipboard-yank ;; pastes the text into your buffer occur ;; shows all lines in a buffer matching a regular expression (message "howdy") (sit-for 1) ;; shows a message and waits (redisplay) ;;forces redisplay, same as (sit-for 0) (setq debug-on-error t) ;; goes into debug mode on errors (setq debug-on-quit t) ;; goes into debug mode when C-g is entered ; (buffer-string beg end) ;; returns the text within the two positions (buffer-substring (point) (mark)) ;; returns a string given two positions (setq mytext (replace-regexp-in-string "\\(public\\|private\\|protected) " "\\1 " mytext)) ;;wrap html span tags around some keywords cool functions where-is func_name;; shows all the keys bound to the function list-colors-display shows all the available colors. font-lock-face-attributes contains all the current colors set-fill-prefix ;; takes the text *on the current line* and makes it the automatic start of any wrapped lines. you need to be in fill-mode. kill-rectangle ;; deletes the rectangle defined in the highlighted region (ok its really between point and mark yank-rectangle ;; inserts the rectangular text back into the buffer. clear-rectangle ;; replaces all the text in the rectangle with spaces. open-rectangle ;; inserts spaces in the defined rectangle. indented-text-mode ;; special mode to created automatically indented paragraphs edit-picture ;; special mode to do ascii art stuff; exit with C-cC-c ;; C-c^ text goes up ;; C-c. text goes down C-c< left C-c> right C-c` C-^ C-c' ` | ' ` | ' ` | ' `|' C-c< -----------\----------- C-c> /|\ / | \ / | \ / | \ / | \ C-c/ C-c. C-c\ dired mode; D marks a file for deletion x deletes the ones marked g refreshes the buffer
- Insert Random Number into a file
Use with a temp macro to stuff random values into a file
(defun randomizer() (interactive)(insert (format "%010d" (random 10000000000))) )
- Symbol Properties.
Each symbol has an associated Property list that can be used as an associative array. The get and put function access them
(setq automobile "Mitchs") (put 'automobile 'year "2018") (put 'automobile 'make "tesla") (message (concat automobile " car is a " (get 'automobile 'year) " " (get 'automobile 'make) ) ) => Mitchs car is a 2018 tesla
Nil is returned when using "get" on a symbol that does not have a binding for the requested property.
- Quotes and Backquotes
Quotes delay evaluation
(set x '(a b)) ;; sets x to a two item list (set x (a b)) ;; sets x to the result of calling function a with argument b
Backquote does something simaliarly, but items prefaced with a comma in the list may be evaluated
- Control Structures
- while - looping (always returns nil)
(setq i 0) (while (< i 10) ;; do somthing interesting here (setq i (1+ i)) )
- while - looping (always returns nil)
- reduce
reduce takes a function which takes two arguments and applies it successively to a list. In this example we use '+' to sum all the elements of a list. ((((+ 1 (+ 2)) (+ 3))(+ 4))(+ 5))
(reduce '+ '(1 2 3 4 5)) => 15 ; equivalent to: (+ 1 (+ 2 (+ 3 (+ 4 (+ 5)))))
Another reduce example which creates a regular expression substring based on a list of keywords
(setq keywords (list "public" "private" "protected" "using" "internal" "static" "this" "return" "namespace" "new" "class" "var" )) (setq regex-keywords (reduce '(lambda(x y) (concat x " \\|" y)) keywords)) ==> "public \\|private \\|protected \\|using \\|internal \\|static \\|this \\|return \\|namespace \\|new \\|class \\|var"
- Macro Functions
These are like regular functions, but do not evaluate their arguments
this prints "3"
(defmacro dec (var) "decrements a variable" (list 'setq var (list '- var 1))) (setq x 4) (dec x) (message (format "%d" x))
- misc
- defsubst
defsubst works like defun but "inlines" the function. Works like "Inline" in C++. - progn
aggregates a series of expressions into a single one and returns the value of the last one.
(progn expr1 [expr2...])
(progn (setq a "fish")(setq b "fowl")) => "fowl"
- prog1
Use prog1 to return the value of the first expression
(prog1 (setq a "fish")(setq b "fowl")) => "fish"
- defsubst
- interactive
Letter Action Example * as the first character in the interactive line of a function
implies that it can be run only in buffers with write permissionUser Input n a number (interactive "nNumber of Rows:") N a number, unless a invoked with a prefix argument s string (interactive "sYour name:") b name of existing buffer B name of non-existing buffer e event f name of existing file F name of new file S Symbol Environmental r the hilighted region defun myfunc (begin end)
(interactive "r") - format
Code Meaning %s String %c Character %d Integer %e Exponental %f Floating point %g Shortest of %f or %e example:
(message "%s is brought to you by the number %d and the letter %c" "This message" 7 ?q)
- Help
KeyStroke Command Result C-h a command-apropos Give a substring, and see a list of commands (functions interactively callable) that contain that substring. See also the apropos command. C-h b describe-bindings Display table of all key bindings. C-h f describe-function Type a function name and get documentation of it. C-h C-f Info-goto-emacs-command-node Type a function name; it takes you to the Info node for that command. C-h F view-emacs-FAQ Shows emacs frequently asked questions file. C-h i info The info documentation reader. C-h k describe-key Type a command key sequence; it displays the full documentation. C-h C-k Info-goto-emacs-key-command-node Type a command key sequence; it takes you to the Info node for the command bound to that key. C-h l view-lossage Shows last 100 characters you typed. C-h m describe-mode Print documentation of current major mode, which describes the commands peculiar to it. C-h s describe-syntax Display contents of syntax table, plus explanations C-h v describe-variable Type name of a variable; it displays the variable's documentation and value. C-h w where-is Type command name; it prints which keystrokes invoke that command. - Vectors
Vectors are similar to lists, but have a fixed size and elements can be accessed directly.
(setq a 1 b 2 c 4) (vector a b c) => [1 2 4] (make-vector 6 "ab") => ["ab" "ab" "ab" "ab" "ab" "ab"] ;; makes a vector with 6 elements with the default value of "ab" (aset myvector 4 "cd") ;; sets the fourth element to cd. (aref myvector 3) ;; gets the third element (vectors are zero based) (setq myvector (make-vector 6 "ab")) (message "%s" myvector) ;; shows the vector (length myvector) ;; returns the number of elements
- Some of my favorite functions
;; this shows the use of markers. (defun simple-html-create-href () "given a bare url, this creates the href for it. If www.fincher.org is highlighted, then this will produce <a href=\"www.fincher.org\">www.fincher.org" (interactive "*") (setq simple-html-start-marker (make-marker)) (setq simple-html-end-marker (make-marker)) (set-marker simple-html-start-marker (mark)) (set-marker simple-html-end-marker (point)) (copy-region-as-kill (mark) (point)) (goto-char simple-html-start-marker) (insert "<a href=\"") (goto-char simple-html-end-marker) (insert "\">") (clipboard-yank) (insert "</a>") ) (defun simple-html-insert-table (nrows ncols) "inserts a table template -mdf" (interactive "nNumber of Rows: \nnNumber of Columns: ") (beginning-of-line) (insert "<table border=2>\n") (setq i -1);; -1 instead of 0, since the header row takes up one loop (while (< i nrows) ;; loop over the rows (insert "<tr>") (setq j 0) (while (< j ncols) ;; loop over the cols (if (eq i -1) (insert "<th> </th>") (insert "<td> </td>") ) (setq j (1+ j)) ) (insert "</tr>\n") (setq i (1+ i)) ) (insert "</table>\n") (forward-line (- (+ nrows 3))) (message "%d rows, %d number of columns" nrows ncols) ) (defun convert-html-angles () " replaces all < and > to < and > in the region" (interactive "*") (narrow-to-region (point) (mark)) (goto-char (point-min)) (replace-string "<" "<") (goto-char (point-min)) (replace-string ">" ">") (widen) ) (defun delete-leading-whitespace () (interactive) (narrow-to-region (point) (mark)) (goto-char (point-min)) (replace-regexp "^[\t ]*" "") (widen) ) (defun unify-region (top bottom &optional macro) "removes all carriage returns in region" (interactive "r") (save-excursion (let ((end-marker (progn (goto-char bottom) (beginning-of-line) (point-marker))) next-line-marker) (goto-char top) (if (not (bolp)) (forward-line 1)) (setq next-line-marker (point-marker)) (while (< next-line-marker end-marker) (goto-char next-line-marker) (save-excursion (forward-line 1) (set-marker next-line-marker (point))) (save-excursion ;; command goes here (end-of-line)(delete-char 1) )) (set-marker end-marker nil) (set-marker next-line-marker nil)) ) ) (defun downcase-tag ()(interactive) "downcases HTML tags to make them more like xhtml wants. (<HTML> and </HTML> are downcased. Bad Side Effects to be corrected: Permanently changes case-fold-search and starts by downcasing the word at the point." (setq case-fold-search nil) (downcase-word 1) (search-forward-regexp "<[/]*[A-Z]") (forward-char -1) (setq case-fold-search t) ) (defun display-file-info() "trival function to show find-file-hooks functionality" (message (concat "the filename is " buffer-file-name " and it is " (if buffer-read-only "read only." "writable"))) ) (defun insert-numbers (arg) "insert number into a file, starting with 1 -mdf" (interactive "NHow many numbers to insert: ") (setq i 0) (while (< i arg) (setq i (1+ i)) (insert (int-to-string i)) (backward-char 1) (next-line 2) (beginning-of-line 0) ) ) (defun bubble-line-up(click) ;; by mitch fincher (interactive "@e") (beginning-of-line)(transpose-lines 1) (previous-line 2)) (defun insert-date-stamp () "Insert current date at current position." (interactive "*") (message "starting to date stamp the line...") (beginning-of-line) (insert "<!------------------------------------------------->\n") (insert (format-time-string "%D %a" (current-time))) (insert "\n<!------------------------------------------------->\ntimetool:\n\n") (forward-char -1) (message "starting to date stamp the line - finished.") ) (defun sqlupcase () " upcases sql keywords in selected region" (interactive "*") (setq sqlkeywords '("add " "all " "alter " "and " "any " "as " "asc " "authorization " "backup " "begin " "between " "break " "browse " "bulk " "by " "cascade " "case " "check " "checkpoint " "close " "clustered " "coalesce " "collate " "column " "commit " "compute " "constraint " "contains " "containstable " "continue " "convert " "create " "cross " "current " "current_date " "current_time " "current_timestamp " "current_user " "cursor " "database " "dbcc " "deallocate " "declare " "default " "delete " "deny " "desc " "disk " "distinct " "distributed " "double " "drop " "dummy " "dump " "else " "end " "errlvl " "escape " "except " "exec " "execute " "exists " "exit " "fetch " "file " "fillfactor " "for " "foreign " "freetext " "freetexttable " "from " "full " "function " "goto " "grant " "group " "having " "holdlock " "identity " "identitycol " "identity_insert " "if " "in " "index " "inner " "insert " "intersect " "into " "is " "join " "key " "kill " "left " "like " "lineno " "load " "national " "nocheck " "nonclustered " "not " "null " "nullif " "of " "off " "offsets " "on " "open " "opendatasource " "openquery " "openrowset " "openxml " "option " "or " "order " "outer " "over " "percent " "plan " "precision " "primary " "print " "proc " "procedure " "public " "raiserror " "read " "readtext " "reconfigure " "references " "replication " "restore " "restrict " "return " "revoke " "right " "rollback " "rowcount " "rowguidcol " "rule " "save " "schema " "select " "session_user " "set " "setuser " "shutdown " "some " "statistics " "system_user " "table " "textsize " "then " "to " "top " "tran " "transaction " "trigger " "truncate " "tsequal " "union " "unique " "update " "updatetext " "use " "user " "values " "varying " "view " "waitfor " "when " "where " "while " "with " "writetext ")) (narrow-to-region (point) (mark)) (while sqlkeywords (goto-char (point-min)) (setq keyword (car sqlkeywords)) (replace-string keyword (upcase keyword)) (setq sqlkeywords (cdr sqlkeywords)) (message keyword)(sit-for 0 1) ) (widen) (message "sqlupcase complete.") ) Or the mapcar function can be used to make it more elegant: (defun sqlupcase () " upcases sql keywords in selected region" (interactive "*") (setq sqlkeywords '("add " "all " "alter " "and " "any " "as " "asc " "authorization " "backup " "begin " "between " "break " "browse " "bulk " "by " "cascade " "case " "check " "checkpoint " "close " "clustered " "coalesce " "collate " "column " "commit " "compute " "constraint " "contains " "containstable " "continue " "convert " "create " "cross " "current " "current_date " "current_time " "current_timestamp " "current_user " "cursor " "database " "dbcc " "deallocate " "declare " "default " "delete " "deny " "desc " "disk " "distinct " "distributed " "double " "drop " "dummy " "dump " "else " "end " "errlvl " "escape " "except " "exec " "execute " "exists " "exit " "fetch " "file " "fillfactor " "for " "foreign " "freetext " "freetexttable " "from " "full " "function " "goto " "grant " "group " "having " "holdlock " "identity " "identitycol " "identity_insert " "if " "in " "index " "inner " "insert " "intersect " "into " "is " "join " "key " "kill " "left " "like " "lineno " "load " "national " "nocheck " "nonclustered " "not " "null " "nullif " "of " "off " "offsets " "on " "open " "opendatasource " "openquery " "openrowset " "openxml " "option " "or " "order " "outer " "over " "percent " "plan " "precision " "primary " "print " "proc " "procedure " "public " "raiserror " "read " "readtext " "reconfigure " "references " "replication " "restore " "restrict " "return " "revoke " "right " "rollback " "rowcount " "rowguidcol " "rule " "save " "schema " "select " "session_user " "set " "setuser " "shutdown " "some " "statistics " "system_user " "table " "textsize " "then " "to " "top " "tran " "transaction " "trigger " "truncate " "tsequal " "union " "unique " "update " "updatetext " "use " "user " "values " "varying " "view " "waitfor " "when " "where " "while " "with " "writetext ")) (narrow-to-region (point) (mark)) (mapcar (lambda (keyword) (goto-char (point-min)) (replace-string keyword (upcase keyword) ) (message keyword)(sit-for 0 5) ) sqlkeywords) (widen) (message "sqlupcase complete.") )
- How to have the recently used files show up as an option under "File"
Put this in your init.el file
(recentf-mode 1)
- How to reverse sort selected lines
reverse-region
C-u1M-x sort-lines
- How to run an external command and get the results in an Emacs
buffer
E-!"your command goes here"
- How to use Abbreviation mode.
Abbreviation allows you to enter a few characters and emacs expands those to a longer string.
in your init.el file place the following lines:
(setq-default abbrev-mode t)
(read-abbrev-file "~/.abbrev_defs")
(setq save-abbrevs t)
To define an abbreviation, enter the shortened form and then "C-x aig" for "add-inverse-global"
- Using Bookmarks.
Bookmarks are a quick index into your frequently visited files.
Keystroke Command Description C-xrm bookmark-set saves this file and position as a bookmark C-x rb bookmark-jump goes to the defined bookmark - Using gnus, the emacs news reader
Add the following code if you are reading from a remote news server:
Helpful keystrokes:(add-hook 'nntp-server-opened-hook 'nntp-send-authinfo) (setq gnus-nntp-server "news.your.isp")
j - jump to newsgroup space - open this newsgroup
- W3 a web browser inside emacs.
This is located at https://www.cs.indiana.edu/elisp/w3/docs.html My favorite keystrokes:
Key Meaning C-xC-b Show index of visited links F Move forward a page B Move backward a page s Show HTML source for current page C-o Open a user-supplied URL - Macros are our friends.
Keystroke Command Description C-x( start-kbd-macro Starts recording keystrokes C-x) end-kbd-macro Stop recording keystrokes C-xe call-last-kbd-macro C-g keyboard-quit quit defining C-uC-x( Executes current macro and opens it for appending new commands name-last-kbd-macro Gives the macro a name insert-kbd-macro inserts the textual definition at the current point
usually done into the init.el fileset-local-key key macroname binds the key name to the macro for this session C-uC-xq Insert a pause into a macro definition ESC C-c Continue in the macro after a pause - Defining the meaning of keys
function syntax Notes define-key (define-key keymap "keystroke" 'command-name) gloabal-set-key (gloabal-set-key "keystroke" 'command-name) like a define-key, but uses "global-map" local-set-key (local-set-key "keystroke" 'command-name) like a define-key, but uses the local keymap (setq ctlf1-map (make-sparse-keymap))
(global-set-key [(control f1)] ctlf1-map)
(define-key ctlf1-map "t" 'simple-html-insert-table)
Now if you enter "C-f1 t" it will run the command simple-html-insert-table. - To add info files to directories other than default:
Example to add a new info directory. I think this is better than adding to the default one, since we I upgrade emacs the old one (and my additions go away).
(setq Info-directory-list (cons "c:/fincher/emacs/info/make" Info-directory-list))
The c:/emacs/lisp/dir file will still need the top level pointers added. e.g.,
* Make: (make). gnu make documents.
- Using Registers to save scraps of text
Registers allow you to have multiple copy/paste buffers. You need two commands, Register-Save (C-x r s) and Register-Insert(C-x r i) To use this, hilight an area of text and enter "C-x r s"; emacs will ask you for which buffer to save it in. Pick a letter a-z.. Use "C-x r i" to insert the saved text.
- Regular Expressions
// replaces all instances of xxx.JAVA with xxx.java M-x replace-regexp "\(^[a-z]+\)\.c" "\1.C"
myfile.c other.c twice.c
results in:
myfile.C other.C twice.C
- Completion.
You can tell emacs to ignore certain file extensions when giving you possible completions:
(setq completion-ignored-extensions '(".o" ".elc" "~" ".bin" ".bak" ".obj" ".map" ".a" ".ln" ".class"))
- Command Line args
- "--no-site-file" tells emacs not to look for a site-init.el file.
- "--no-init-file" do not run the ~/.emacs.d/init.el or default.el
- "-q" start emacs without running ~/.emacs.d/init.el
- "--debug-init" allows debugging of the ~/.emacs.d/init.el file
- etags:
To create the TAG file
etags -o c:\fincher\TAGS c:/emacs/lisp/*.el
- Output
(print OBJECT & optional PRINTCHARFUN) (print "howdy") => "howdy" (directory-files "c:" ) ("AUTOEXEC.BAT" "Adobe" "CONFIG.SYS" ...) (directory-files "c:" t "\\.BAT$") ("AUTOEXEC.BAT") prin1 the Lisp Printer, used to show printed representation of a Lisp object - Arguments:
(defun forward-paragraph (&optional arg) "docuemtation string" (interactive "p") (or arg (setq arg 1)) ...
- How to change the background and foreground colors
(set-face-background 'default "black") (set-face-foreground 'default "white")
- How to set the editor to autocopy the selected text to the kill-ring.
(setq mouse-drag-copy-region 't)
- How to set the column number, line number and encoding type in your title
(setq column-number-mode t line-number-mode t buffers-menu-sort-function nil frame-title-format (concat "TheOneTrueEditor: \"%f\" -- " (format "'%s'" buffer-file-coding-system)) )
- Misc:
> I seems like most of the commands only operate on the current buffer. Is > there way to apply a command (query-replace, for exapmple) on all loaded > bufferes, other than by creating etags ? M-x load-library RET dired-x RET Then, you can mark files in dired and use `A' to search in all of them and `Q' to do query-replace in all of them. -kai
- My favorite Emacs references:
- https://tiny-tools.sourceforge.net/emacs-keys.html
- emacswiki.org
- windows info
- Emacs and Unicode
- FAQ in text
- Manual in HTML
- an emacs email reader
- Emacs and Java (JDE)
- gnu's nt page
- GNU Emacs Lisp Reference Manual Excellent source of info on the language
- Programming in Emacs Lisp An Introduction
- Writing GNU Emacs Extensions A real carbon based book. Excellent intro into emacs and lisp
- My .vm and simple-html-mode.el files.
The name "Emacs" is derived from "Editor Macros", but I like "Escape Meta Alt Control Shift" better.
- "Lambda: The Ultimate Imperative" by Guy Lewis Steele, Jr. and Gerald Jay Sussman.