100

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?

76

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

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.

| improve this answer | |
  • 4
    Unsurprisingly, the installation step will fail on distributions that are not deb-based and hence don't have apt-get. (In which case, yum or zypper might be present.) They also require root on the local machine, whcih may or may not be available. (This is not to say it's a bad solution, but it comes with a big prerequisite.) – Ulrich Schwarz May 12 '15 at 11:26
  • 5
    Okay. Figured it out. All of this is done on the local machine. In the case of OS X one has to install osxfuse and sshfs. These can be installed with homebrew (brew install sshfs and brew cask install osxfuse , or from packages obtained from osxfuse.github.io – inspirednz Jun 26 '17 at 3:22
118

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.

| improve this answer | |
  • 7
    Yes, the file is retrieved to a temporary file, that is uploaded to the server on save. – FloHimself May 12 '15 at 9:17
  • 2
    You can try :set buftype=""​ in vim. – FloHimself May 12 '15 at 9:20
  • 3
    Furthermore, vim will read ~/.ssh/config and resolve remote hosts with vim scp://[host]/path. – Jangari Apr 3 '18 at 4:05
  • 2
    Include an entry in your ssh config with an alias for the host, the hostname, the username and path to the identity file, and then you can open a file from vim directly using vim scp://[alias]/path. If the path is relative to your login home on the remote, you'll have one slash. If absolute in the filesystem, two: vim scp://host//etc/ssh/ssshd_config. – Jangari May 30 '18 at 21:31
  • 4
    @stillanoob: :e scp://... – FloHimself Apr 2 '19 at 11:06
6

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.

| improve this answer | |
2

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.

| improve this answer | |
  • Are you suggesting the usage of something like commiting the changes to git, pushing it to something like github, and pulling the changes from github to your local machine, to make change there? – alpha_989 May 30 '18 at 18:19
  • That is feasible for complex code changes.. but for simple code changes perhaps.. what @FloHimself or shivams mentioned is easier to implement.. – alpha_989 May 30 '18 at 18:20
1

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.

| improve this answer | |
1

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

| improve this answer | |
  • Just a note that this suppresses the Press Enter to Continue for the initial loading, but not for any subsequent :w – Sparhawk Oct 9 '18 at 4:45
0

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

| improve this answer | |
0

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" didnt 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 splitted:

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.