Open a file with su/sudo inside Emacs

前端 未结 9 918
我寻月下人不归
我寻月下人不归 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: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 ") (lambda()
            (interactive)
            (message "!!! SUDO opening %s" (dired-file-name-at-point))
            (sudo-find-file (dired-file-name-at-point))
          ))
        )
    )
    

提交回复
热议问题