2011-07-14

Emacs: unique buffer names; auto-compile elisp files

Emacs: unique buffer names; auto-compile elisp files

If you work with files of the same name often, their buffers name is hard to distinguish. e.g. {“index.html”, “index.html <2>”, “index.html <3>”}. You can make the buffer name display part of the dir name. Place the following in your emacs init file:

;; make buffer names easily identifiable
(require 'uniquify) ; bundled with GNU Emacs 23.2.1 or earlier
(setq uniquify-buffer-name-style 'forward)

auto byte-compile elisp files

If you code elisp, it's nice to have the elisp file automatically byte-compiled everytime you save it. Put the following in your emacs init file:

;; auto compile elisp files after save
(add-hook 'emacs-lisp-mode-hook (lambda () (add-hook 'after-save-hook 'emacs-lisp-byte-compile t t)) )

Note that a byte-compiled lisp file will make emacs load it faster, but also run faster. The code run at least 6 times faster. However, for small elisp files (such as your 〔.emacs〕), the speed increase may not be noticeable.

Also note: if you want emacs to load the byte compiled file if it exists, you should not include the “.el” suffix. e.g. do it like this (load "my_emacs_keys") See also: How to Install Emacs PackagesEmacs Lisp's Library System: What's require, load, load-file, autoload, feature?.

I got the above tips today from Source tsengf.blogspot.com. Check it out, he's got many other emacs tips.

2011-07-15 Addendum: Adolfo Benedetti wrote to add a code so that it'll byte-compile only if there already exist a byte-compiled file. Great improvement. Thanks.

(defun auto-recompile-el-buffer ()
  (interactive)
  (when (and (eq major-mode 'emacs-lisp-mode)
             (file-exists-p (byte-compile-dest-file buffer-file-name)))
    (byte-compile-file buffer-file-name)))

(add-hook 'after-save-hook 'auto-recompile-el-buffer)