Open a file with su/sudo inside Emacs

前端 未结 9 920
我寻月下人不归
我寻月下人不归 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:18

    I recommend you to use advising commands. Put this function in your ~/.emacs

    (defadvice ido-find-file (after find-file-sudo activate)
      "Find file as root if necessary."
      (unless (and buffer-file-name
                   (file-writable-p buffer-file-name))
        (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
    
    0 讨论(0)
  • 2020-12-12 09:19

    The nice thing about Tramp is that you only pay for that round-trip to SSH when you open the first file. Sudo then caches your credentials, and Emacs saves a handle, so that subsequent sudo-opened files take much less time.

    I haven't found the extra time it takes to save burdening, either. It's fast enough, IMO.

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

    Not really an answer to the original question, but here's a helper function to make doing the tramp/sudo route a bit easier:

    (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)))
    
    0 讨论(0)
提交回复
热议问题