How do I remotely edit files via ssh?

I have to edit some files placed on some server I could reach via ssh.

I would prefer to edit these files in customized vim on my workstation (I have not rights to change vim settings on remote server). Sometimes I would like to edit a file with sublime text or other GUI editor.

Of course, I can download these files, edit them locally and upload them back to server. Is there more elegant solution?

Asked By: Loom

||

You could do this by mounting the remote folder as a file-system using sshfs. To do this, first some pre-requisites:

#issue all these cmds on local machine
sudo apt-get install sshfs
sudo adduser <username> fuse #Not required for new Linux versions (including Ubuntu > 18.04)

Now, do the mounting process:

mkdir ~/remoteserv    
sshfs -o idmap=user <username>@<ipaddress>:/remotepath ~/remoteserv

After this, just go into the mounted folder and use your own local customized vim.

Answered By: shivams

You can do that via scp like this:

vim scp://user@myserver[:port]//path/to/file.txt

Notice the two slashes // between server and path, which is needed to correctly resolve the absolute path. (The first slash is syntactic, while the second slash specifies the remote user’s root directory, as usual. To start at the home directory, you’d do [:port]/~/path/to/file.txt.) [:port]is optional.

This is handled by vim’s netrw.vim standard plugin. Several other protocols are supported.

Answered By: FloHimself

Depending on how many files and what kind of files you are expecting to edit, this is maybe not exactly what you want to do here, but I think it’s worth mentioning. If you have to edit files in a remote server, but want to use everything you have in your own working station, then you may want to start thinking of using some kind of Revision Control system in your machines. That way, you can modify your local copies in your own machine using your software of choice, commit the changes, and then just update the local copies in the destination machine. Besides editing the files with whatever software you feel comfortable with, you have the added value of having a history of changes related to each file, which is always good.

Here’s a list of Revision Control Software, just in case.

Answered By: jimm-cl

Depending on what you mean when you say you do not have the rights to edit the Vim settings, there may be a way of using Vim on the server in the way you want anyway. If you can’t change your user .vimrc (because you’re logging in as a shared user, for example) but you can still create files, create it as a file called, say, Loom.vimrc and then call Vim using the -u switch:

vim -u ~/Loom.vimrc file_to_edit

You can even then use an alias: alias vim='vim -u ~/Loom.vimrc' will allow you to use Vim in the usual way, and it’ll still load your custom .vimrc file. This alias won’t persist after you log out, so you don’t need to worry about anyone else accidentally using your customised Vim.

Answered By: Kara Potts

To expand on Mr. Potts answer:
You can also do the above, then put something like this in .bash_profile (or whatever your shell uses):

if [[ "$(who mom loves | awk ' { print $1 }' )" == "Loom" ]]; then 
         alias vim="vim -u ~/.Loom_vimrc"  
         fi  

where Loom is your original userID that you login in as.

If you’re logging in as a shared account (and not an individual account then sudo su – ing, then may Von Neuman have mercy on your soul for you are lost.

I would have put this in a comment, but I couldn’t get the code formatted at all.

Answered By: Petro

If you have a vim sessions running already use

:silent e scp://user@myserver[:port]//path/to/file.txt

The :silent in front will suppress the Press Enter to Continue message

and

e scp://user@myserver[:port]//path/to/file.txt is the Ex mode command to edit the remnote file.

Tested with BitVise SSHD running on Windows 10, and using VIM runing on Ubuntu 16.04

Answered By: alpha_989

If you are more GUI-oriented and use one of the more newbie-friendly Linux distros like Ubuntu or Mint, this is another option and does not require any more installations.

You should have nemo as your default file manager. It may not be called "Nemo" on the menu, so go under Help > About of your file manager ("Files" app) to see.

In nemo, go to File > Connect to server, enter your remote machine’s details (SSH’s default port is 22), and then open the files just like any file on your local machine, with whatever editor you prefer. You can even close Nemo and continue working in your editor.

From the address bar, it seems to be using the sftp protcol.

Just be aware that if your remote host has an inactivity timeout for the SSH connection, this will also prevent you from saving changes in the editor after the timeout has dropped the connection…

Answered By: frIT

Multiple options are usable, not just "scp"; see: https://www.vim.org/scripts/script.php?script_id=1075

I like using "rsync" more, because for me "diff" didn’t work correctly with scp.

Example – start vim with the "/tmp/test"-file on different remote servers (hostname01.domain.my and hostname02.domain.my and …), using bash-extension, vertically split:

vimdiff -O rsync://hostname{01,02,03}.domain.my:/tmp/test

Answered By: MacMartin

I quite often use something like the following

for i in hosta hostb hostc
do
ssh -t vim xx.txt
done

so the trick is

ssh -t vim xx.txt
Answered By: ferg

The Visual Studio Code Remote – SSH extension allows you to open a remote folder on any remote machine, virtual machine, or container with a running SSH server and take full advantage of VS Code’s feature set. Once connected to a server, you can interact with files and folders anywhere on the remote filesystem.

No source code needs to be on your local machine to gain these benefits since the extension runs commands and other extensions directly on the remote machine.

Answered By: Ahmad Ismail
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.