1

Sometimes I want to edit a file in Emacs as root. So I found this useful function:

(defun sudo-edit (&optional arg)
  "Edit currently visited file as root.
With a prefix ARG prompt for a file to visit.
Will also prompt for a file to visit if current
buffer is not visiting a file."
       (interactive "P")
       (if (or arg (not buffer-file-name))
           (find-file (concat "/sudo:root@localhost:"
                              (ido-read-file-name "Find file(as root): ")))
         (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))

But I'm not able to save the file, I got the following error message:

Suspicious state from syntax checker emacs-lisp: Flycheck checker emacs-lisp returned non-zero exit code 255, but its output contained no errors: Opening input file: No such file or directory, /sudo:root@localhost:/etc/mysql/flycheck_my.cnf

This problem is understandable. In order to get flycheck-package (an alternative to Flymake) working, it tries to create the file flycheck_my.cnf in directory mysql, but it don't have any write access in order to create the file.

So I modified the function as following:

(defun sudo-edit (&optional arg)
  "Edit currently visited file as root.

With a prefix ARG prompt for a file to visit.
Will also prompt for a file to visit if current
buffer is not visiting a file."
       (interactive "P")
       (if (or arg (not buffer-file-name))
           (find-file (concat "/sudo:root@localhost:"
                              (ido-read-file-name "Find file(as root): ")
                              (flycheck-mode nil)
                              ))
         (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))
         (flycheck-mode nil)
         ))

But I keep the same error message, while the flycheck-mode is disabled. So somewhere I went wrong. But where? And why?

  • I don't think the flycheck-mode calls are right - the docs say "When called from Lisp, enable ‘flycheck-mode’ if ARG is omitted, nil or positive." so those calls will be turning flycheck mode on. I don't have flycheck mode being enabled by default - are you using global-flycheck-mode? – stevoooo May 14 '17 at 17:15
  • Thanks for your reply. So if I understand you right, my function enables the flycheck mode on. I checked, and global-flycheck-mode is disabled. I still got the same error. – ReneFroger May 14 '17 at 18:52
  • Yeah, I think you need to use (flycheck-mode -1) to turn flycheck off, but if you've not got global-flycheck-mode enabled I don't see why it should be on at all. I also suspect that flycheck is being enabled (by whom we don't know) when the file is visited before it even gets to your lines for turning it off... – stevoooo May 14 '17 at 19:12
  • You're right. Disabling (flycheck-mode -1) and (global-flycheck-mode -1) helped me in this case. – ReneFroger May 14 '17 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.