Tuesday, August 19, 2008

Emacs Tip #25: Shell Dirtrack By Prompt

Note: I've since been notified of this problem already being solved by a package provided with Emacs. I looked at it and prefer the implementation in this post, only because it modifies the prompt so I don't have to see the full path in my prompt.

I'd been butting my head against this issue for a while now, but wasn't quite annoyed enough to come up with a solution. Luckily for me (and you), ccthomas wrote this little snippet which parses your shell prompt to determine the current directory.

Other than having to modify your shell prompt (no big deal), it's super clean and easy.

I modified the code slightly because I didn't see the need/purpose for/of the "%300<.<" in the prompt. So here' s my slightly modified version:

(add-hook 'shell-mode-hook
#'(lambda ()
(shell-dirtrack-mode nil)
(add-hook 'comint-preoutput-filter-functions
'shell-sync-dir-with-prompt nil t)))

(defun shell-sync-dir-with-prompt (string)
"A preoutput filter function (see `comint-preoutput-filter-functions')
which sets the shell buffer's path to the path embedded in a prompt string.
This is a more reliable way of keeping the shell buffer's path in sync
with the shell, without trying to pattern match against all
potential directory-changing commands, ala `shell-dirtrack-mode'.

In order to work, your shell must be configured to embed its current
working directory into the prompt. Here is an example .zshrc
snippet which turns this behavior on when running as an inferior Emacs shell:

if [ $EMACS ]; then
prompt='|Pr0mPT|%~|[%n@%m]%~%# '

The part that Emacs cares about is the '|Pr0mPT|%~|'
Everything past that can be tailored to your liking.
(if (string-match "|Pr0mPT|\\([^|]*\\)|" string)
(let ((cwd (match-string 1 string)))
(setq default-directory
(if (string-equal "/" (substring cwd -1))
(setq cwd (concat cwd "/"))))
(replace-match "" t t string 0))

The original can be found on the Emacs Wiki: Shell Dirtrack By Prompt

No comments: