What is a quick way to edit a remote file on Linux?

倾然丶 夕夏残阳落幕 提交于 2019-12-21 05:17:20

问题


I have a remote file that I edit regularly. I would like to edit it with a quick, simple command that would work likely via SSH. At present, my workflow is to connect to the remote computer via SSH, open the file using an editor (say vim or nano), edit, save and then close the connection.

I am aware that I can mount the remote computer filesystem using SSHFS or Nautilus capabilities, but I'm really looking for a single command to run in the terminal which shall open the file in an editor, allow me to save and then exit, closing all connections to the remote computer.

Currently, I am trying to do this by passing a command to the remote computer via SSH, but I am running into difficulties. For VIM, the command is something like the following:

ssh user1@computer1 "vim /path/laboratory_notebook_1.md"

Using this procedure, VIM does not run correctly and presents the following error:

Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal

For nano, the command is something like the following:

ssh user1@computer1 "nano /path/laboratory_notebook_1.md"

Using this procedure, nano does not run and the following error is presented:

Error opening terminal: unknown.

I'm not sure how to proceed on this line of thought. I would appreciate assistance on this method and suggestions on other ways to edit remote files briskly with a minimum amount of interaction.


回答1:


Force Pseudo-TTY Allocation

You can force pseudo-tty allocation with one or more -t flags. The SSH(1) man page says:

 -t      Force pseudo-tty allocation.  This can be used to execute arbi-
         trary screen-based programs on a remote machine, which can be
         very useful, e.g. when implementing menu services.  Multiple -t
         options force tty allocation, even if ssh has no local tty.

Example

Using your own example, slightly modified, the following would work if you have a local TTY or PTY:

ssh -t user1@computer1 'vim /path/laboratory_notebook_1.md'

It works fine for me with OpenSSH_6.2p2. Your mileage (and operating environment) may vary.




回答2:


If you are using vim. Vim comes with a plugin called netrw which will allow you to do this.

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

Will copy the file to you local machine and on save reupload it.

Take a look at netrw's documentation :h netrw



来源:https://stackoverflow.com/questions/25183674/what-is-a-quick-way-to-edit-a-remote-file-on-linux

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