Search code examples
vimssheditnano

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


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.


Solution

  • 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.