sudo 如何在 Ubuntu 上使用 Emacs 编辑本地文件?

南山竹*_*南山竹 0 linux emacs ubuntu

使用 Emacs 通过 SSH 和 Sudo读取打开文件后,我可以通过 sudo 编辑远程文件, /ssh:username@hostname|sudo:username@hostname:/the-file 但我不能 sudo 编辑本地文件,Emacs 的tramp 提示密码为root@hostname,因为在 Ubuntu 上不存在 root 的密码(请参阅RootSudo)。

那么有没有办法在 Ubuntu 上 sudo 编辑本地文件?

总结:如果你想用 Emacs 编辑远程/本地文件,@ philsOpen file via SSH 和 Sudo with Emacs 中有一个很好的答案 ;如果您使用了弹丸(版本<=0.12.0)并且无法 sudo 编辑本地文件(例如Tamp: sending password或挂起),您可以尝试以下代码来解决我的问题:


  (when (package-installed-p 'projectile)
  (defun projectile-project-root ()
    "Retrieves the root directory of a project if available.
    The current directory is assumed to be the project's root otherwise."
    (let ((dir default-directory))
      (or (--reduce-from
           (or acc
               (let* ((cache-key (format "%s-%s" it dir))
                      (cache-value (gethash cache-key projectile-project-root-cache)))
                 (if cache-value
                     (if (eq cache-value 'no-project-root)
                         nil
                       cache-value)
                   (let ((value (funcall it (file-truename dir))))
                     (puthash cache-key (or value 'no-project-root) projectile-project-root-cache)
                     value))))
           nil
           projectile-project-root-files-functions)
          (if projectile-require-project-root
              (error "You're not in a project")
            default-directory))))
  (projectile-global-mode))
Run Code Online (Sandbox Code Playgroud)

要查看带有 sudo 的简单流浪汉挂在“发送密码”上,并且projectile-global-mode 阻止新进程通过 TRAMP #523 工作以获取更多信息。

Chr*_*ris 5

只需user@system完全跳过该部分:

C-x C-f /sudo::/path/to/file RET
Run Code Online (Sandbox Code Playgroud)