In Emacs, how do I use directory local variables for remote projects?

孤者浪人 提交于 2019-12-04 05:28:58

For Emacs 24.3 or later, see legoscia's answer.

For earlier versions, you can use the following workaround from EmacsWiki:

We can advise ‘hack-dir-local-variables’ to work around this, if you are willing to incur the associated performance cost (which can be relatively minimal in some situations).

;; Enable directory local variables with remote files. This facilitates both
;; the (dir-locals-set-class-variables ...)(dir-locals-set-directory-class ...)
;; and the .dir-locals.el approaches.
(defadvice hack-dir-local-variables (around my-remote-dir-local-variables)
  "Allow directory local variables with remote files, by temporarily redefining
`file-remote-p' to return nil unconditionally."
  (flet ((file-remote-p (&rest) nil))
  ad-do-it))
(ad-activate 'hack-dir-local-variables)

If using directory classes instead of a .dir-locals.el file, you would set the class using a normal tramp path. e.g.:

 (dir-locals-set-class-variables
  'read-only
  '((nil . ((buffer-read-only . t)))))

 (dir-locals-set-directory-class
  "/ssh:(user)@(host):/path/to/dir" 'read-only)

If you're using Emacs 24.3 or later, set enable-remote-dir-locals to t.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!