Directory Aliases is one of my favourite emacs packages, well, out of the ones I have written at least. It provides a way to go from an alias to a directory location using ido and dired respectively. On Linux, I can use shell aliases and the emacs daemon but for my sins, a lot of my time is spent developing on Windows and there it is invaluable. As I find the various Windows shells are lacking it is nice to an alternative interface to the OS – emacs.
alias realias='$EDITOR ~/.aliases; source ~/.aliases'
Inspired by chromatic’s post mentioning realias (attributed to Damian Conway) I added a couple of features to directory aliases. The first is the ability to open a regular file in addition to the directory. The second is the ability to reload the aliases so you have access to them immediately after adding them.
Future plans include a facility to add an alias with a keystroke while visiting a file (maybe I should be using bookmarks instead… but I like ido too much).
(require 'ido) (require 'dired) (defconst *home* "c:/home/jared") (defconst *packages* "c:/packages") (defconst *file:dir-aliases* (concat *elisp-dir* "dir-aliases.el")) (defsubst home (path) (concat *home* path)) (defsubst packages (path) (concat *packages* path))
The preamble has some convenience functions for referring to commonly used areas.
(defconst *file-aliases* (list (cons "dir-aliases" *file:dir-aliases*) ...)) (defconst *dired-aliases* (list (cons "project" (home "/project")) (cons "curious" (home "/websites/curious")) (cons "plack" (home "/plack-tests")) (cons "site-lisp" (packages "/emacs/site-lisp")) ...))
I main separate alists for file aliases and dired aliases.
(defun my-open-alias (aliases fn &optional alias) (interactive) (unless alias (setq alias (ido-completing-read "Alias: " (mapcar (lambda (e) (car e)) aliases) nil t))) (if (and (stringp alias) (> (length alias) 0 )) (let ((pair (assoc alias aliases))) (if pair (funcall fn (cdr pair)) (error "Invalid alias %s" alias))) (error "Invalid alias %s" alias)))
The main my-open-alias
function reads in the alias with ido
and then applies the passed in function.
(defun file-open-alias (&optional alias) (interactive) (my-open-alias *file-aliases* #'find-file alias)) (defun dired-open-alias (&optional alias) (interactive) (my-open-alias *dired-aliases* #'dired alias)) (defun reload-aliases () (interactive) (load *file:dir-aliases*))
More convenience functions using #'find-file
and #'dired
and the alias reloader I mentioned earlier.
(defvar f2-prefix-map nil) (setq f2-prefix-map (make-sparse-keymap)) (global-set-key [f2] f2-prefix-map) (global-set-key (kbd " d") 'dired-open-alias) (global-set-key (kbd " f") 'file-open-alias) (provide 'dir-aliases)
Maybe dir-aliases
isn’t the best name anymore.
Leave a comment