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

问题描述 投票:0回答:5

读完Open file via SSH and Sudo with Emacs然后我可以sudo编辑远程文件通过 /ssh:username@hostname|sudo:username@hostname:/the-file 但我不能sudo编辑本地文件,Emacs的tramp提示密码root@hostname,因为在Ubuntu上没有root密码存在(参见RootSudo)。

那么有一种方法可以在Ubuntu上编辑本地文件吗?

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


  (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))

有关更多信息,请参阅Simple tramp with sudo hangs on 'Sending password'projectile-global-mode prevents new processes from working over TRAMP #523

linux ubuntu emacs
5个回答
1
投票

如果在Ubuntu下运行sudo命令,则必须使用自己的密码而不是(不存在的)root密码。


4
投票

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

C-x C-f /sudo::/path/to/file RET

0
投票

我想你要做的是:

ssh username@hostname
sudo emacs /the-file

我想不出在一条线上做到这一点的方法。您首先需要通过ssh,常规密码连接远程主机,然后sudo编辑该文件。根据客户操作系统,它将提示您输入适当的sudo密码。


0
投票

我打开要编辑的文件(作为普通文件),然后使用此博客文章中的代码(选项A)进行M-x sudo-edit:

http://emacsredux.com/blog/2013/04/21/edit-files-as-root/

在这里粘贴代码,以防邮件将来消失。

 (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))))

-3
投票

要用emacs作为root编辑一些本地(在你自己的计算机上)文件/some/path/local.txt刚刚运行

  sudo emacs /some/path/local.txt

一旦emacs启动,你就不能让它写一个拥有root权限的文件(除非它是组或世界可写的,这通常是一件坏事)。

如果你有一个正确配置的sshd守护进程,你可以ssh root@localhost(也许通过Emacs Tramp ...)并使用emacs Tramp等...我不建议这样做。在Ubuntu上,您需要创建root用户(或使用uid 0创建myroot用户并使用myroot@localhost)。

您也可以编辑(作为普通用户)/tmp/中的某些文件,如/tmp/foobar,然后将其复制为root用户:sudo cp -v /tmp/foobar /some/path/for/root/foobar

Emacs Tramp有一些sudosu specific syntax;也许你想要那个......

© www.soinside.com 2019 - 2024. All rights reserved.