ansi-term won't find file under current directory?

518 views Asked by At

It's a very strange problem. I think it must caused my incorrect configuration of ansi-term, but i still can't find out where it is.

The issue is: when i in ansi-term and press M-x find-file, the prompt isn't current directory but the path i entered in my previous find file action. So when i change directory, it still display the same directory. So i have to enter the current directory every time. But it works very well in M-x shell and M-x eshell

2

There are 2 answers

2
Drew On BEST ANSWER

Does the same thing happen when you start Emacs without your init file, i.e., emacs -Q? If so, that's the designed behavior or (especially if you use a development snapshot) perhaps an Emacs bug.

If not, then bisect your init file recursively to find out which part of it causes this behavior. To do that, use, e.g., command comment-region (see prefix arg in doc) to comment and uncomment a block of text. Comment out 1/2 of your init file, then 3/4, then 7/8,..., each time testing whether the uncommented portion causes or removes the problematic behavior. You will very quickly identify what causes the behavior.

0
bin On

Because the path of emacs is different from that of term, it can only be changed by use the emacs command "cd".

So to solve this problem, I add the following code to my emacs configure file. The method is

  1. find the pid of current term
  2. find current working directory(cwd) of this pid.

I use multi-term, I think the method will be similar on ansi-term.

(defadvice term-send-input (after update-cwd)
(let* ((pid (process-id (get-buffer-process (current-buffer))))
     (cwd (shell-command-to-string
           (format "lsof -p %d -Fn | awk 'NR==2{print}' | sed \"s/n\\//\\//\" |  tr -d '\n'" pid))))
(cd cwd)
(message (concat "change emacs path to: " cwd))))
(ad-activate 'term-send-input)

Then you can bound the key of term-send-input to <enter>. When you press <enter> in term, the emacs will change to the same path with the current path of term.

BTW, I use Mac Os. If you are on Linux, you can use the following code to find cwd.

(cwd (file-truename (format "/proc/%d/cwd" pid)))