Search code examples
emacsprojecttramp

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


I'm using TRAMP to connect to a remote server and I wanted to use some directory local variables. What are my options? Should I use emacs-server and do it that way, or should I add the directory local variables to my .emacs file? Is there a way to force TRAMP to look for the .dir-locals.el file?


Solution

  • 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)