简体   繁体   English

使用emacs和ssh打开远程文件的最佳方法是什么?

[英]What is the best way to open remote files with emacs and ssh

I connect to the remote machine with ssh user@192.168.1.5 . 我用ssh user@192.168.1.5连接到远程机器。 When I need to open a file in the remote machine I do, eg, 当我需要在远程机器中打开文件时,例如,

emacs /usr/share/nginx/html/index.html

and that opens the index.html file in the shell. 并在shell中打开index.html文件。 I noticed that some emacs commands work but others do not work. 我注意到一些emacs命令工作,但其他命令不起作用。 For instance, Cw does not work; 例如, Cw不起作用; M-< does not work. M-<不起作用。 How can I fix this, and what is the best way to work with emacs and ssh? 我该如何解决这个问题,使用emacs和ssh的最佳方法是什么?

I found this question but it made me more confused. 我发现了这个问题,但它让我更加困惑。

I generally prefer opening remote files from a local Emacs instance. 我通常更喜欢从本地Emacs实例打开远程文件。

While running Emacs on your local machine , opening a remote file over ssh is not much different than opening any other file besides a slightly different syntax . 本地计算机上运行Emacs时,通过ssh打开远程文件与打开除了略有不同的语法之外的任何其他文件没有太大区别。

For ssh, you can type Cx Cf . 对于ssh,您可以键入Cx Cf Now, in the minubuffer you want to type /ssh:user@host:/path/to/file (Note that tab completion will work once you start typing a path. Also note the leading / character). 现在,在minubuffer中你要输入/ssh:user@host:/path/to/file (注意一旦开始输入路径,选项卡完成就会起作用。另请注意前导/字符)。 See the full docs here . 请在此处查看完整文档

In your example, that would be: 在您的示例中,那将是:

C-x C-f /ssh:user@192.168.1.5:/usr/share/nginx/html/index.html

Now you can edit remote files over ssh in Emacs while using your local configuration and any installed packages, etc... 现在,您可以使用本地配置和任何已安装的软件包等在Emacs中通过ssh编辑远程文件...

Just to add to the answer above, you can write shortcuts for machines that you use frequently: 只是为了添加上面的答案,您可以为经常使用的机器编写快捷方式:

(defun connect-remote ()
  (interactive)
  (dired "/user@192.168.1.5:/"))

This will open a dired buffer on a remote machine. 这将在远程计算机上打开一个dired缓冲区。 You can navigate this buffer as you would a local one. 您可以像在本地缓冲区一样导航此缓冲区。

If you have set up ssh keys for the remote machine, you don't even have to enter the password. 如果您已为远程计算机设置了ssh密钥,则甚至无需输入密码。

If you have a bunch of remote machines, you can give some recognizable name to each function, eg connect-cupcake , connect-kitkat and use smex package for completion. 如果你有一堆的远程机器,你可以给一些识别的名称每个功能,如connect-cupcakeconnect-kitkat ,用smex包完成。

And to add to @abo-abo's post about "shortcuts" -- 并添加到@ abo-abo关于“快捷方式”的帖子 -

Use Emacs bookmarks. 使用Emacs书签。 Just create bookmarks normally, when you visit a remote file or directory. 只需在访问远程文件或目录时正常创建书签。 Then just use Cx rb to jump to a remote bookmark, whose name you provide (with completion). 然后只需使用Cx rb跳转到您提供其名称的远程书签(完成)。

If you use Bookmark+ then remote bookmarks are highlighted specially in the *Bookmark List* , so you can recognize them more easily. 如果您使用Bookmark +,则会在*Bookmark List*特别突出显示远程书签,以便您更轻松地识别它们。 And remote bookmarks that must be accessed by su or sudo ( root ) are highlighted differently. 必须由susudoroot )访问的远程书签以不同方式突出显示。

If you use Dired+ then you can also quickly bookmark multiple remote files or directories, by visiting their containing remote directory in Dired, marking them, and hitting Cx b . 如果您使用Dired +,那么您还可以通过访问Dired中包含的远程目录,标记它们并点击Cx b来快速为多个远程文件或目录添加书签。 No need to give the bookmarks names; 无需给出书签名称; they are named after the files. 它们以文件命名。 Even if you never use those bookmarks for navigating to the remote files, you can use them with Bookmark+ tags to organize the files and thus operate on subsets of them. 即使您从未使用这些书签导航到远程文件,您也可以将它们与Bookmark + 标签一起使用来组织文件,从而对它们的子集进行操作。

If you use Icicles then whenever you use a command to jump to a bookmark, you can narrow the completion candidates to those that are remote by hitting CM-@ during completion. 如果您使用Icicles,那么每当您使用命令跳转到书签时,您可以通过在完成期间按CM-@来将完成候选者缩小到远程的候选者。

The original poster expressed interest in opening remote files as the root user. 原始海报表示有兴趣以root用户身份打开远程文件。 This can be done with the command: 这可以使用以下命令完成:

Cx Cf /ssh:you@remotehost|sudo:remotehost:/path/to/file RET

More documentation can be found here: https://www.emacswiki.org/emacs/TrampMode#toc14 可在此处找到更多文档: https//www.emacswiki.org/emacs/TrampMode#toc14

A Simple Answer that focuses on the remote machine: 一个关注远程机器的简单答案:

If I plan to do all my emacs work on the remote machine, I use 如果我计划在远程计算机上运行所有emacs,我会使用

ssh -X username@hostname

and then run emacs in the remote session, displaying back on my local machine. 然后在远程会话中运行emacs,在我的本地计算机上显示。 It's an old question but I wanted to throw this in for completeness. 这是一个老问题,但我想把它完全放入。 Granted there are some xhost / X config issues but in many networks this will work right off the bat! 虽然有一些xhost / X配置问题,但在许多网络中,这将立即起作用!

SSH mode for emacs is what you're looking for. 您正在寻找emacs的SSH模式

Once you have it set up you just run 一旦你完成设置,你就跑了

M-x ssh RET hostname RET

Then it prompts you for your password twice (once for the command line, once for loading files). 然后它会提示您输入两次密码(一次用于命令行,一次用于加载文件)。

For the most part you can treat it like any other shell (non-interactive and a few minor differences, but that's it). 在大多数情况下,你可以像任何其他shell一样对待它(非交互式和一些细微差别,但就是这样)。

It keeps track of which directory you're in, so when you want to open a file from the directory you're looking at it automatically starts in the right directory and you just need to enter in the file name. 它会跟踪您所在的目录,因此当您想要从您正在查看的目录中打开文件时,它会自动从正确的目录开始,您只需输入文件名即可。

Emacs Wiki has more info too. Emacs Wiki也有更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM