Emacs Lisp 删除空行命令

;;;KILL-BLANK-LINE function
(defun undouble-newline ()
  "Delete doubled newlines from the current buffer."
  (interactive)
  (save-excursion
    (replace-regexp "\n\n" "\n")))

Emacs Lisp 进行编号备份

;;;Make Numbered Backups of Files
(setq version-control t)
;;;Don't ask when deleting old backups
(setq delete-old-versions t)
;;;Number of Numbered Backups to Keep
;;;Delete Oldest Version First
(setq kept-old-versions 50)
(setq kept-new-versions 50)

Emacs Lisp 在Emacs中键入换行符

C-q C-j

Emacs Lisp 带空格的Emacs缩进

(setq-default indent-tabs-mode nil)

Emacs Lisp 标题栏自定义,文件名显示

(add-hook 'window-configuration-change-hook
	  (lambda ()
	    (setq frame-title-format
		  (concat
		   invocation-name "@" system-name ": "
		   (replace-regexp-in-string
		    (concat "/home/" user-login-name) "~"
		    (or buffer-file-name "%b"))))))

Emacs Lisp 随机模式线颜色

(defun set-random-mode-line-background() (interactive)
  (setq ncolors (length (defined-colors)))
  (random t)
  (set-face-background 'mode-line (nth (random ncolors) (defined-colors))))

Emacs Lisp 自动编译的.emacs文件

(defconst dot-emacs (concat (getenv "HOME") "/" "frog.el")
    "My dot emacs file")

(require 'bytecomp)
(setq compiled-dot-emacs (byte-compile-dest-file dot-emacs))

(if (or (not (file-exists-p compiled-dot-emacs))
	(file-newer-than-file-p dot-emacs compiled-dot-emacs)
        (equal (nth 4 (file-attributes dot-emacs)) (list 0 0)))
    (load dot-emacs)
  (load compiled-dot-emacs))

(add-hook 'kill-emacs-hook
          '(lambda () (and (file-newer-than-file-p dot-emacs compiled-dot-emacs)
                           (byte-compile-file dot-emacs))))

Emacs Lisp 插入代码模板

(defvar insert-code-template-templates
  '(((c-mode c++-mode) ".h$" "/* copyright 2001 */\n#ifndef __SOMETHING_H\n#define __SOMETHING_H\n\n#endif /* # __SOMETHING_H */\n")
    ((c-mode c++-mode) nil "int\nmain(int argc, char **argv)\n{\n\n}\n")
    ((cperl-mode perl-mode) nil "#!/usr/bin/perl -w\n\nuse strict;\n"))
  "A list of triples, used for inserting code.
A triplet is composed of a symbol for the major mode (or a list of symbols),
a regular expression to match against the buffer's file name,
and the text to insert when both the major mode and regular expression match.")

(defun insert-code-template ()
  "insert a code template, based on major mode
when called interactively, always do insertion
otherwise, only do so when the buffer is empty"
  (interactive)
  (let ((l insert-code-template-templates))
    (when (or (called-interactively-p)
              (eq (point-min) (point-max)))
      (while l
        (let* ((elt (car l))
               (modes (if (listp (car elt)) (car elt) (list (car elt))))
               (re (cadr elt))
               (template (caddr elt)))
          (when (and (member major-mode modes)
                     (or (null re)
                         (string-match re (buffer-file-name))))
            (insert template)
            (setq l nil)))
        (setq l (cdr l))))))

(add-hook 'find-file-hook 'insert-code-template)

Emacs Lisp 钩取景器

(let ((L ()))
  (mapatoms
   (lambda (atom)
     (let ((name (symbol-name atom)))
       (if (and (boundp atom)
                (string-match "-\\(hooks?\\|functions\\)\\'" name)
                (not (string-match "-mode-hook" name))
                (not (string-match "\\`ange-ftp-" name))
                (not (string-match "\\`bookmark-" name))
                (not (string-match "\\`comint-" name))
                (not (string-match "\\`custom-" name))
                (not (string-match "\\`cl-" name))
                (not (string-match "\\`byte-compile-" name))
                (not (string-match "\\`Info-" name))
                (not (string-match "\\`calendar-" name))
                (not (string-match "\\`cperl-" name))
                (not (string-match "\\`eieio-" name))
                (not (string-match "\\`tramp-" name))
                (not (string-match "\\`vc-" name))
                (not (string-match "\\`speedbar-" name))
                (not (string-match "\\`semanticdb-" name))
                (not (string-match "\\`semantic-" name))
                (not (string-match "\\`elscreen-" name))
                (not (string-match "\\`org-" name))
                (not (string-match "\\`ediff-" name))
                (not (string-match "\\`w3m-" name))
                (not (string-match "\\`skk-" name))
                (not (string-match "\\`anything-" name))
                (not (string-match "\\`rmail-" name))
                (not (string-match "\\`dired-" name))
                (not (string-match "\\`url-" name))
                (not (string-match "\\`column-number-mode-" name))
                )
           (setq L (cons atom L))))))
  (dolist (a (sort L (lambda (a b) (string< (symbol-name a)(symbol-name b)))))
    (insert (format "%s\n" a)))
  (length L))

Emacs Lisp 带拖尾空白

(defun strip-trailing-whitespace ()
  "remove all whitespace from the end of lines in the entire buffer"
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "\\s-+$" nil t)
      (replace-match ""))))