9

I want to use emacs auto-save feature with tramp, but only for file that I don't open using sudo. My current configuration, based on the tramp documentation, looks like this:

;;;;;;;; BAKUCP ;;;;;;;;
;; Backup remote files locally to stop autosave pain
(setq tramp-backup-directory "~/.emacs-backup")
(unless (file-directory-p tramp-backup-directory)
  (make-directory tramp-backup-directory))
(if (file-accessible-directory-p tramp-backup-directory)
    (setq tramp-auto-save-directory tramp-backup-directory)
  (error "Cannot write to ~/.emacs-backup"))
;; Don't backup su and sudo files
(setq backup-enable-predicate
      (lambda (name)
        (and (normal-backup-enable-predicate name)
             (not
              (let ((method (file-remote-p name 'method)))
                (when (stringp method)
                  (member method '("su" "sudo"))))))))

It is forbidding creating remote backups of files edited as superuser but auto-saved files are still created on my local machine. Is there any way to disable that without disabling auto-save at all?

1 Answer 1

2

Backup and auto-save are different operations, controlled by different variables.

If you want to discard auto-save for files opened as root, you might change the buffer-local variable auto-save-file-name-transforms. Something like this (untested):

(add-hook
 'find-file-hook
 (lambda ()
   (when (and (stringp buffer-file-name)
          (string-equal (file-remote-p buffer-file-name 'user) "root"))
     (setq buffer-auto-save-file-name nil))))
5
  • If you put this in the users' .emacs file, it will not be read if emacs is opened with sudo though correct? So when would this code be in effect?
    – Startec
    Aug 16, 2016 at 19:34
  • I don't understand your question. Everything in ~/.emacs is evaluated at start of Emacs, unless you start Emacs with the -Q or similar option. Aug 17, 2016 at 7:25
  • Sorry, I meant that opening emacs with sudo emacs would not read the config file in the users' home directory. I realize now this is probably a good answer for when something is opened as sudo from an instance of emacs opened as a user.
    – Startec
    Aug 17, 2016 at 7:32
  • What variable would check if the file were opened as root? (i.e. opened with the /sudo::/ tramp method)?
    – Startec
    Aug 17, 2016 at 7:33
  • default-directory is a buffer-local variable, which tells you the current directory of that buffer. (file-remote-p default-directory) returns non-nil, when you are remote. (file-remote-p default-directory 'user) gives you the remote user name, "root" for example. Aug 17, 2016 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.