How can I use Emacs tramp to ssh to a remote host and edit a file as another user on an ad-hoc basis?

余生颓废 提交于 2019-11-30 13:28:00
Chris Withers

As of this commit, TRAMP supports ad-hoc multiple hops again.

Roughly speaking, you use it like this:

/ssh:transituser@remotehost|sudo:user@remotehost:/some/file

I haven't got it to work reliably with ido-mode yet, which is a shame, but it's a lot better than nothing! :-)

The following code may help:

  (defun find-file-as-root ()
    "Find a file as root."
    (interactive)
    (let* ((parsed (when (tramp-tramp-file-p default-directory)
                     (coerce (tramp-dissect-file-name default-directory)
                             'list)))
           (default-directory
             (if parsed
                 (apply 'tramp-make-tramp-file-name
                        (append '("sudo" "root") (cddr parsed)))
               (tramp-make-tramp-file-name "sudo" "root" "localhost"
                                           default-directory))))
      (call-interactively 'find-file)))

I had it in my .emacs file, and it seems to come from here: http://atomized.org/2011/01/toggle-between-root-non-root-in-emacs-with-tramp/

I haven't used it extensively but it seems like that is a step in the right direction.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!