/ code

Two Useful Functions for Emacs

Figured I'd make a post showing off the two awesome functions that I use to make my Emacs config clean and powerful.

functions.el

functions.el is where the magic is. Two functions will make installing and configuring packages via package.el a breeze; especially on a new system.

To load personal config files that don't relate to a package.el package such as key-bindings.el or theme.el...


;; For loading personal configurations
(defun personal (library)
  (load (concat "~/.emacs.d/personal/" (symbol-name library)) 'noerror))

Now when you call (personal 'key-bindings), the file key-bindings.el in the personal directory will be loaded. Awesome!

To install and/or require packages such as what-the-commit.el via package.el and your custom configuration for that package...


;; For loading packages from the Emacs Lisp Package Archive (ELPA)
(defun package (package)
    (when (not (package-installed-p package))
      (package-install package))
    (personal package))

So when you call (package 'what-the-commit), the package what-the-commit will be installed if not already installed, required, and configured via the file what-the-commit.el in the personal directory.

init.el

Now we have to actually load functions.el and set up ELPA so we can use the functions.


(load "~/.emacs.d/personal/functions")

(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
                         ("melpa" . "http://melpa.milkbox.net/packages/")))
(package-initialize)
(when (not package-archive-contents) (package-refresh-contents))

Example files

Loaded using the personal function

;;; key-bindings.el located in ~/.emacs.d/personal/
;;; Sets up various key bindings

;; Window manipulation
(global-set-key [(control kp-6)] 'enlarge-window-horizontally)
(global-set-key [(control kp-4)] 'shrink-window-horizontally)
(global-set-key [(control kp-8)] 'enlarge-window)
(global-set-key [(control kp-2)] 'shrink-window)

;; Query Replace
(global-set-key [(f6)]         'query-replace)
(global-set-key [(control f6)] 'query-replace-regexp)

;; Version control
(global-set-key [(f9)]         'magit-status)

;; Terminal
(global-set-key (kbd "C-x t") 'eshell)

Loaded using the package function

;;; projectile-rails.el located in ~/.emacs.d/personal/
;;; Configures the package projectile-rails

(add-hook 'projectile-mode-hook 'projectile-rails-on)

There we go. Two simple functions that will clean up your init.el. You will also no longer have to worry about your packages not being installed on a new machine that you use your Emacs config with. Enjoy!