158

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?

2
  • 3
    vim scp://$USER@$HOSTNAME//etc/passwd
    – Tinmarino
    Jan 9, 2021 at 16:29
  • 3
    @Tinmarino - if you intended to make me laugh with your choice of file, you succeeded. Aug 10, 2022 at 20:51

9 Answers 9

185

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.

28
  • 8
    Yes, the file is retrieved to a temporary file, that is uploaded to the server on save.
    – FloHimself
    May 12, 2015 at 9:17
  • 2
    You can try :set buftype=""​ in vim.
    – FloHimself
    May 12, 2015 at 9:20
  • 6
    Furthermore, vim will read ~/.ssh/config and resolve remote hosts with vim scp://[host]/path.
    – Jangari
    Apr 3, 2018 at 4:05
  • 3
    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, 2018 at 21:31
  • 9
    @stillanoob: :e scp://...
    – FloHimself
    Apr 2, 2019 at 11:06
103

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.

5
  • 6
    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.) May 12, 2015 at 11:26
  • 8
    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, 2017 at 3:22
  • 1
    what the idea with this command? sudo adduser <username> fuse did you mean to add a user to a group or something? It fails because adding a user in ubuntu doesn't allow another 'fuse' argument, and also because of course the current user already exists. Feb 5, 2021 at 14:21
  • 1
    @TamaMcGlinn: I think you don't need that command anymore for new Ubuntu versions. Simply skip that command and let me know if it worked. I will update the answer accordingly.
    – shivams
    Feb 5, 2021 at 15:03
  • yes, skipping that worked on ubuntu Focal Fossa. I also changed the last command to just sshfs <username>@<ipaddress>:/remotepath ~/remoteserv without the idmap, because I couldn't figure out from the docs why you would need that generally. Feb 5, 2021 at 15:23
7

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

3
  • Note that {01,02,03} is from csh in the 70s, not bash. {01..03} itself from zsh in the 90s. Apr 17 at 13:46
  • @StéphaneChazelas I'm not exactly sure what you mean. If I execute echo thing{1,2,3} then it prints thing1 thing2 thing3 (at least for bash version 5.1, see man bash -> "Brace Expansion"). But I also like zsh
    – MacMartin
    Apr 18 at 6:59
  • I wanted to clarify that the syntax was not bash-specific nor invented by bash contrary to what using bash-extension would imply. Apr 18 at 7:01
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.

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

2
  • 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, 2018 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, 2018 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.

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

1
  • Just a note that this suppresses the Press Enter to Continue for the initial loading, but not for any subsequent :w
    – Sparhawk
    Oct 9, 2018 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...

-1

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
1
  • 3
    This command won't execute vim locally as the OP explicitly asked. Dec 1, 2020 at 18:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .