So I asked this question about directory shortcuts in a shell. The idea is I'd like to be able to open a file in /home/Documents/foo
, say, by typing C-x C-f ~foo/file
.
So, is there a way to emulate zsh's "named directories" in the emacs buffer?
Incidentally, the shortcut definitions I want to use are already in my .zshrc, if there's a solution that reads them directly, that gets bonus points.
Best Answer
The Emacs component that's responsible for expanding
~
in file names isexpand-file-name
. Unfortunately, it's written in C, and deep inside its bowels is code that assumes that what comes after~
is a user name. Fortunately, Emacs has a generic way of adding a wrapper around functions, so you can do what you want if you don't mind repeating some of the logic in the built-in function.Here's some completely untested code that should get you going. Look up “Advising Emacs Lisp Functions” in the Emacs Lisp manual for more information; the basic idea is that
defadvice
adds some code to run before the actual code ofexpand-file-name
. Please signal the mistakes I've inevitably made in comments (whether you know how to fix them or not).I'll leave parsing the shortcuts in
.zshrc
to fillexpand-file-name-custom-tilde-alist
(or whatever technique you choose to keep the aliases in synch) as an exercise.