Open a file with su/sudo inside Emacs

前端 未结 9 919
我寻月下人不归
我寻月下人不归 2020-12-12 08:32

Suppose I want to open a file in an existing Emacs session using su or sudo, without dropping down to a shell and doing sudoedit or

相关标签:
9条回答
  • 2020-12-12 09:08

    Tramp does not round-trip sudo via SSH, it uses a subshell. See the manual: https://www.gnu.org/software/tramp/#Inline-methods

    Therefore, I recommend that you stick with TRAMP.

    0 讨论(0)
  • 2020-12-12 09:11

    (works only locally. Need to be updated to work correctly via tramp)

    A little bit extended Burton's answer:

    (defun sudo-find-file (file-name)
    "Like find file, but opens the file as root."
    (interactive "FSudo Find File: ")
    (let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name))))
    (find-file tramp-file-name)))
    
    
    (add-hook 'dired-mode-hook
        (lambda ()
          ;; open current file as sudo 
          (local-set-key (kbd "C-x <M-S-return>") (lambda()
            (interactive)
            (message "!!! SUDO opening %s" (dired-file-name-at-point))
            (sudo-find-file (dired-file-name-at-point))
          ))
        )
    )
    
    0 讨论(0)
  • 2020-12-12 09:12

    At least for saving, a sudo-save package was written exactly for that kind of problem.

    0 讨论(0)
  • 2020-12-12 09:13

    If you use helm, helm-find-files supports opening a file as root with C-c r.

    0 讨论(0)
  • 2020-12-12 09:14

    Ugh. Perhaps you could open a shell in Emacs and exec sudo emacs.

    The problem is that you presumably don't just want to open the file. You want to be able to save it later. Thus you need your root privs to persist, not just exist for opening the file.

    Sounds like you want Emacs to become your window manager. It's bloated enough without that. :)

    0 讨论(0)
  • 2020-12-12 09:17

    Your example doesn't start ssh at all, at least not with my version of TRAMP ("2.1.13-pre"). Both find-file and save-buffer work great.

    0 讨论(0)
提交回复
热议问题