2017-03-04 Bookmarking directories in Eshell

A friend of mine showed me a nifty trick, basically bookmarking directories in bash. While this is nice, I often use Eshell, and I wanted a similar thing there. (Notice that while Emacs bookmarks do work in Dired, they won’t let you bookmark a directory in Eshell!) Bonus points for sharing the bookmark database (which is basically a directory called ~/.marks/ containing symlinks to bookmarked directories).

Well, here it is. Notice the (a bit misleadingly named) function file-symlink-p, which returns the target of the symlink if the file with the given name is a symlink and nil otherwise. Also, the directory name is hardcoded, and I didn’t bother to add any error detection – but this would be easy.

(defun eshell/jump (mark)
  "Jump to a directory symlinked to by a file called ~/.marks/MARK."
  (eshell/cd (file-symlink-p (concat "~/.marks/" mark))))

This is nice already, but lacks one important thing: autocompletion. It turns out that this is not difficult at all:

(defun pcomplete/jump ()
  "Complete a command that wants a name of a file in ~/.marks."
  (pcomplete-here* (directory-files "~/.marks/")))

And that’s pretty much it!

On the other hand, it would be probably more Emacs-y to make Emacs bookmarks support Eshell. Still, this solution has an advantage of working both in Eshell and in bash. Moreover, one could add support also for the mark function, which wouldn’t be too difficult.

CategoryEnglish, CategoryBlog, CategoryEmacs