Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

ssh

edit

nano

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.

like image 966
d3pd Avatar asked Aug 07 '14 13:08

d3pd


People also ask

Which command is used to edit in Linux?

There are two command-line text editors in Linux®: vim and nano. You can use one of these two available options should you ever need to write a script, edit a configuration file, create a virtual host, or jot down a quick note for yourself.


2 Answers

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.

like image 198
Todd A. Jacobs Avatar answered Sep 18 '22 14:09

Todd A. Jacobs


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

like image 26
FDinoff Avatar answered Sep 19 '22 14:09

FDinoff