vim - 远程编程

标签 vim emacs cross-platform

我在 Windows 计算机上进行开发工作,但在远程 Linux 计算机上进行编译。我目前所做的就是在 Windows 上启动 X 服务器,通过 ssh 进入 Linux 机器,然后进行远程开发。

我想要做的是在 Windows 计算机上编辑源代码,并在保存时自动将文件复制到 Linux 系统。我还希望我的内置编译命令能够在远程系统上执行构建。

如果有区别的话,源代码全部是 C 语言,使用 GCC。按照偏好的降序排列,我的桌面上有 Emacs、Vi 和 Netbeans,并且愿意安装另一个 IDE 作为最后的手段。

最佳答案

这在 vim 中当然是可行的。您可以在 vim 中使用 scp://协议(protocol)来编辑远程文件,并设置写入本地副本的命令。您还可以更改 vim 用于 :make 的程序,以在您的服务器上执行 ssh make。

您需要设置 ssh key 来轻松完成此操作(否则您将一直输入密码),但这相当简单。

另一种选择是作为 make 命令的一部分推送到远程存储库,而不是远程编辑。

<小时/>

编辑:

首先,使用scp:// vim 内的协议(protocol)。来自 :help netrw-start (或从:help scp向下翻页)

Netrw supports "transparent" editing of files on other machines using urls (see |netrw-transparent|). As an example of this, let's assume you have an account on some other machine; if you can use scp, try:

vim scp://hostname/path/to/file

Want to make ssh/scp easier to use? Check out |netrw-ssh-hack|!

您还可以使用scp:// :edit 中的路径命令,或者任何可以使用正常路径的地方。

并且,从提到的:help netrw-ssh-hack ,有关如何设置 ssh key 的说明:

IMPROVING BROWSING *netrw-listhack* *netrw-ssh-hack* {{{2

Especially with the remote directory browser, constantly entering the password is tedious.

For Linux/Unix systems, the book "Linux Server Hacks - 100 industrial strength tips & tools" by Rob Flickenger (O'Reilly, ISBN 0-596-00461-3) gives a tip for setting up no-password ssh and scp and discusses associated security issues. It used to be available at http://hacks.oreilly.com/pub/h/66 , but apparently that address is now being redirected to some "hackzine". I'll attempt a summary based on that article and on a communication from Ben Schmidt:

(1) Generate a public/private key pair on the local machine (ssh client):

  ssh-keygen -t rsa

(saving the file in ~/.ssh/id_rsa as prompted)

(2) Just hit the when asked for passphrase (twice) for no passphrase. If you do use a passphrase, you will also need to use ssh-agent so you only have to type the passphrase once per session. If you don't use a passphrase, simply logging onto your local computer or getting access to the keyfile in any way will suffice to access any ssh servers which have that key authorized for login.

(3) This creates two files:

  ~/.ssh/id\_rsa
  ~/.ssh/id\_rsa.pub

(4) On the target machine (ssh server):

 cd
 mkdir -p .ssh
 chmod 0700 .ssh

(5) On your local machine (ssh client): (one line)

 ssh {serverhostname} cat '>>' '~/.ssh/authorized\_keys2' < ~/.ssh/id_rsa.pub

or, for OpenSSH, (one line)

 ssh {serverhostname} cat '>>' '~/.ssh/authorized\_keys' < ~/.ssh/id_rsa.pub

You can test it out with

 ssh {serverhostname}

and you should be log onto the server machine without further need to type anything.

If you decided to use a passphrase, do:

 ssh-agent $SHELL
 ssh-add
 ssh {serverhostname}

You will be prompted for your key passphrase when you use ssh-add, but not subsequently when you use ssh. For use with vim, you can use

 ssh-agent vim

and, when next within vim, use

 :!ssh-add

Alternatively, you can apply ssh-agent to the terminal you're planning on running vim in:

 ssh-agent xterm &

and do ssh-add whenever you need.

For Windows, folks on the vim mailing list have mentioned that Pageant helps with avoiding the constant need to enter the password.

Kingston Fung wrote about another way to avoid constantly needing to enter passwords:

In order to avoid the need to type in the password for scp each time, you provide a hack in the docs to set up a non password ssh account. I found a better way to do that: I can use a regular ssh account which uses a password to access the material without the need to key-in the password each time. It's good for security and convenience. I tried ssh public key authorization + ssh-agent, implementing this, and it works! Here are two links with instructions:

要在远程系统上进行制作,您需要设置 makeprg变量为 做一个 ssh make。来自 :help makeprg

Program to use for the ":make" command. See |:make_makeprg|. This option may contain '%' and '#' characters, which are expanded to the current and alternate file name. |:_%| |:_#| Environment variables are expanded |:set_env|. See |option-backslash| about including spaces and backslashes.

Note that a '|' must be escaped twice: once for ":set" and once for the interpretation of a command. When you use a filter called "myfilter" do it like this:

 :set makeprg=gmake\ \\\|\ myfilter

The placeholder "$*" can be given (even multiple times) to specify where the arguments will be included, for example:

 :set makeprg=latex\ \\\\nonstopmode\ \\\\input\\{$*}

This option cannot be set from a |modeline| or in the |sandbox|, for security reasons.

关于vim - 远程编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/601532/

相关文章:

emacs - 在 js2-mode 中使用 flycheck 进行缩进设置

emacs - 如何禁用 Emacs 自动向右移动命令?

python - 获取我在黑莓或其他移动平台上编写的 python 程序?

mobile - 跨手机后台服务

java - Vim java折叠无法识别折叠

vim - ":ab"映射如何以及何时被触发?

vim - 如何在 vim 脚本中使用字符串设置选项

c++ - 获取 emacs 的访问标签缩进以添加缩进级别

python - 如何让 Windows 知道我用 Python 编写的服务?

vim - 将一个范围的行附加到另一行范围的末尾