Following a link into a git-repo without lengthy d

2020-08-09 10:11发布

问题:

In a directory I have symbolic links into a git-administered directory (all under Linux). Every time I want to e-dit such a link I get a dialog question:

Symbolic link to Git-controlled source file; follow link? (yes or no)

And I have to type y e s to get to the file. Is there a somewhat simpler way?

Ideally, something like declaring that a directory needs no dialog.

回答1:

Set vc-follow-symlinks. You probably want it to be nil (open link), but be sure to read the docs because t (open target) is also sensible.

(setq vc-follow-symlinks nil)

You can make this a dir local variable if you don't want it set globally.



回答2:

An interesting question. I store all my dotfiles in the repository and have a bunch of symlinks scattered all over the filesystem so this is a known problem to me.

First, a half-solution:

(defalias 'yes-or-no-p 'y-or-n-p)

Now instead of typing a full yes + Enter you can just type a single y or n letter in all situations where yes-or-no is asked in Emacs.

Now, a real solution:

(defun vc-mode-hook ()
  (message buffer-file-name)
  (when
      (and
       (file-exists-p (buffer-file-name))
       (stringp buffer-file-name)
       (or (string-equal "/home/ja/.fluxbox/keys" buffer-file-name)
       (string-equal "<PATH_TO_ANOTHER_FILE>" buffer-file-name))
       (setq-local vc-follow-symlinks t)
       )))

(add-hook 'find-file-hook 'vc-mode-hook)

Here we create a new hook that is called every time find-file is called, for example with C-x C-f or e in Dired. It first checks if a visited file really exists on the filesystem using file-exists-p because it doesn't make sense to run it on files that haven't been created yet. Next it checks if a file name is known using stringp - it will return t when a regular file is opened but nil in a Dired buffer for example. And finally it checks if the file name is equal to one of strings provided using string-equal. If it is, it locally sets vc-follow-symlinks to t. You can add as many string-equal lines as you wish. In the example above I added /home/ja/.fluxbox/keys and an placeholder for an another file name in the next line.