Wednesday, January 2, 2008

Emacs Tip #7: resolve-sym-link

It's often nice to find the true path to a file or directory. Since I work in Emacs, it's often easier to use find-file to discover the path, and then invoke this little diddy. I like having it bound to C-r

(defun resolve-sym-link ()
"Replace the string at the point with the true path."
(interactive)
(beginning-of-line)
(let* ((file (buffer-substring (point)
(save-excursion (end-of-line) (point))))
(file-dir (file-name-directory file))
(file-true-dir (file-truename file-dir))
(file-name (file-name-nondirectory file)))
(delete-region (point) (save-excursion (end-of-line) (point)))
(insert (concat file-true-dir file-name))))

(define-key minibuffer-local-completion-map (kbd "C-r") 'resolve-sym-link)

No comments: