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?

up vote 57 down vote accepted

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.

  • Thank you. I tried it. Unfortunately, I have no /dev/fusermount on my computer. I have /bin/fusermount. Does it the same? – Loom May 12 '15 at 9:59
  • Without /dev/fusermount all works perfect. Thank you – Loom May 12 '15 at 10:09
  • 6
    The referenced information from HowToGeek was from 2006 and heavily outdated. I've updated the answer to the much simpler and up-to-date way of invoking SSHFS. – Benjamin B. May 12 '15 at 10:10
  • 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
  • 2
    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

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.

  • 5
    Yes, the file is retrieved to a temporary file, that is uploaded to the server on save. – FloHimself May 12 '15 at 9:17
  • 1
    Thank you. However, I have got an error, when tried to save file: E382: Cannot write, 'buftype' option is set – Loom May 12 '15 at 9:17
  • 1
    @Loom, I had the same problem, because I omitted the double slash //. Perhaps you made the same mistake? – Benjamin B. May 12 '15 at 9:18
  • 1
    You can try :set buftype=""​ in vim. – FloHimself May 12 '15 at 9:20
  • 1
    Furthermore, vim will read ~/.ssh/config and resolve remote hosts with vim scp://[host]/path. – Jangari Apr 3 at 4:05

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.

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.

  • 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 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 at 18:20

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.

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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