Question

In emacs' shell I am using dirtrack-mode (M-x dirtrack-mode). I get errors in the *Directory Tracking Log* buffer:

Input `user@machine:~/modules/mydir> ' failed to match `dirtrack-list'

In order to solve it I am trying to set the dirtrack-list variable to tell emacs the form of my prompt with no luck. This is mainly because I don't understand this variable's documentation:

dirtrack-list's value is ("^emacs ^.\\{1,\\}@.\\{1,\\}:.+> " nil)

Automatically becomes buffer-local when set in any fashion.

Documentation: List for directory tracking. First item is a regexp that describes where to find the path in a prompt. Second is a number, the regexp group to match. Optional third item is whether the prompt is multi-line. If nil or omitted, prompt is assumed to be on a single line.

Can anyone explain to me which is the first, the second and the third item? How can I tell emacs which part of the prompt is the path?

Was it helpful?

Solution

The value of your dirtrack-list variable is a list:

("^emacs ^.\\{1,\\}@.\\{1,\\}:.+> " nil)

This list consists of two items: a string and the symbol nil. The first item, as per the documentation, is a regular expression that is matched against your prompt to extract the path.

So, let's say your shell prompt looked like this:

toni@host [/home/toni/stuff] >

then you want the regular expression to match the part of the prompt that contains /home/toni/stuff. Obviously, the default regular expression for dirtrack-list does not do that, so you have to replace it. For the example above, something like this should do:

"^[^@]*@[^ ]* \\[\\([^]]*\\)\\] > "

In that regexp, the part that is the actual path is captured by the "\\(...\\)" group. Since it is the first (and only) group in your regexp, you want to put the number 1 as the second item of your dirtrack-list variable.

In order to use your own regexp like the one from the example, type M-x customize-variable RET dirtrack-list RET. Then edit the value to:

("^[^@]*@[^ ]* \\[\\([^]]*\\)\\] > " 1)

and set it for the current session. Of course, you will have to use a regular expression that matches your actual prompt, not the one from my example.

OTHER TIPS

Thomas' answer was useful for me but the regular expression didn't work until I put in double backslashes.

For a prompt like:

user@place:~/work$

I set the dirtrack-list variable to

("^[^:]+:\\([^\\$]+\\)\\$" 1)

always using double backslash instead of single backslash.

Backslashes must be entered as \\.

EDIT: Thanks to @phils for editing the original question and chosen answer to include doubled backslashes.

Below is the snippet added to my .emacs file to enable emacs 'dirtrack' mode - a mode where shell monitors the current directory by watching the prompt (assuming the pwd is printed in the prompt). The default mode 'shell-dirtrack' keeps track by parsing input commands for cd, pushd, and, popd.

I add it here as it is likely some readers of this page are pursuing that option.

(require 'dirtrack) ;change the method used to determine the current directory in a shell
(add-hook 'shell-mode-hook
          (lambda ()
            (shell-dirtrack-mode 0) ;stop the usual shell-dirtrack mode
            (setq dirtrack-list '("^[^:]+:\\([^\\$#]+\\)[\\$#]" 1)) 
            (dirtrack-debug-mode) ;this shows any change in directory that dirtrack mode sees
            (dirtrack-mode))) ;enable the more powerful dirtrack mode

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(dirtrack-list (quote ("^[^:]+:\\([^\\$#]+\\)[\\$#]" 1))))

You can see that dirtrack-list is set twice. The first is locally in the shell mode hook. The second is globally in a section automatically inserted when using the interactive UI command M-x customize-variable [RET] dirtack-list [RET]. Probably only one is needed.

Note: the final regex covers two prompt cases:

user@place:~/work$

user@place:~/work#

Note: Emacs has an interactive function M-x re-builder which makes testing for the right regexp very easy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top