Emacs and symbolic links
Say I have a symbolic link at /home/.bashrc
that points to an actual .bashrc file somewhere else: /some/other/path/.bashrc
that is under a git repository.
If I open /home/.bashrc
in Emacs, it prompts me with:
Symbolic link to Git-controlled source file; follow link? (y or n)
I usually type y
, since I want to edit the actual file. However, later on, if I do buffer-file-name
Emacs returns /some/other/path/.bashrc
, and not /home/.bashrc
).
I would like Emacs to remember that the way I accessed this file was through a symbolic link.
In other words, I would like my symbolic links to be transparent to Emacs. Part of the reason is because I have several Emacs macros that depend on the path of the current file, and these macros think that I am working with a file located in /some/other/path
and not in /home/
.
How can I do this?
When it prompts with
Symbolic link to Git-controlled source file; follow link? (y or n)
type n
. Instead of following the symlink and directly opening the file that the symlink points to, emacs will use the symlink itself, as you desire.
A warning: doing this prevents emacs' version control features from detecting and interacting with the repository at the destination, if there is one.
If you'd like to change the default behaviour, check out the documentation of the vc-follow-symlinks
customization variable. ( Ch v vc-follow-symlinks
)
Just for completeness, for someone who has a question about
Symbolic link to Git-controlled source file; follow link? (y or n)
but doesn't necessarily want what the OP wants, this is the documentation from Ch v vc-follow-symlinks
(as of my Emacs version — look up your own Emacs for details):
vc-follow-symlinks
is a variable defined in vc-hooks.el
. Its value is ask
Documentation: What to do if visiting a symbolic link to a file under version control. Editing such a file through the link bypasses the version control system, which is dangerous and probably not what you want.
If this variable is t, VC follows the link and visits the real file, telling you about it in the echo area. If it is `ask', VC asks for confirmation whether it should follow the link. If nil, the link is visited and a warning displayed.
You can customize this variable.
The upshot is that to avoid being prompted each time, you can in your .emacs
set either
(setq vc-follow-symlinks t)
to always follow the symlink (and edit the "actual" file directly), or
(setq vc-follow-symlinks nil)
to always edit the file as if it's at the symlink itself (this seems to work ok — it won't delete the symlink or anything — but it won't let you use version-control related stuff on the file). I prefer the former (unlike the OP).
如果您有像我一样的用例,我只想用热键访问某些文件而不询问,则可以使用file-truename
函数来解析file-truename
所有符号链接,因此可以在不更改缺省值的情况下防止恼人的问题一切。
(global-set-key (kbd "<f7>") (lambda () (interactive)
(find-file (file-truename user-init-file))))
链接地址: http://www.djcxy.com/p/38558.html
上一篇: Emacs创建名为##的自动保存文件
下一篇: Emacs和符号链接