Editing remote files via scp in vim
From Vim Tips Wiki
created 2002 · complexity intermediate · author Matthew Weier O'Phinney · version 6.0
Vim 6.x has the netrw plugin installed as a standard plugin. It allows you to edit files via ftp, rcp, scp, or http. If your username differs on the remote host, however, and you're trying to use scp, things can get a little weird, particularly if you're not editing a document under your user tree.
To get around this, try opening the file as follows:
Notice two things: adding the "remoteuser@" syntax, and the use of two slashes (//) between the servername and the path. The first sets the remote user so that scp will not grab the $USERNAME environment variable, the second will appropriately set the absolute path.
The latest netrw.vim has several improvements. Later Windows ftp is handled, new protocols (rsync, cadaver, fetch), user fixup functions, etc.
How can we store the password? It prompts for password each time we save!
I just got this working on Win2k w/ PuTTY's command line scp program http://www.chiark.greenend.org.uk/~sgtatham/putty/
- copy pscp.exe into your path somewhere as scp.exe
- put "let g:netrw_cygwin= 0" in your $VIM/_vimrc
A possible gotcha:
If you don't put the path as specified (and noted) in the tip, you may get a non-intuitive error: not putting "//" between the hostname and the *absolute* path of the file you edit may cause vim to try to retrieve the file via rcp, as in
and result in an error. Also be careful that you put the absolute path of the file on the remote machine, not the path relative to the remote user's home directory.
Using relative paths is quite normal and well supported. Try
Someone was asking if you could define the port for ftp
try that...just like any other url.
I guess that would work.
There is a nice way to save your passwords: Create .netrc under you home directory and put lines in, one per ftp machine, like this one:
machine yourftp.somewhere.org login yourlogin password "yoursecret"
ftp remote edit is OK.
Then enter user name and password.
If everything seems to be setup correctly but you're still unable to access a file with ftp. Check the permissions on your .netrc file. If .netrc is readable by anyone else besides the owner then ftp auto fails.
chmod 600 .netrc
Solved for me on windows with putty, in vimrc:
let g:netrw_cygwin = 0 let g:netrw_scp_cmd = "\"C:\\Program Files\\PuTTY\\pscp.exe\" -pw mypasswd "
and now run from wincommander:
Get the latest version to fix any problems you are having.
Was getting a bit annoyed with having to type the full path a remote user's file when I'm using scp and connecting as root on the remote end to edit a local user's file on the remote site and found out that I could do this and vim did "The Right Thing"
That was a lot nicer than having to bother with:
Maybe not such a pain in that example, but if you're working with an Ensim for Linux system, you've got everything chrooted which makes you have to type a ridiculously long path such as:
vim scp://firstname.lastname@example.org//home/virtual/site2/fst/var/www/html/.htaccess (yawn)
To change the scp port, there's several options. A quick one would be while you've opened vim to type this:
:let g:netrw_scp_cmd="scp -q -P <desired_new_port>"
and then just type:
--> I think a better solution is to use ssh-mechanisms, i.e. the ~/.ssh/config file:
Host lala HostName test.machine.example.net User remoteuser IdentityFile ~/.ssh/id_for_test.machine Port 57
I have discovered how to make passive mode ftp work. See http://alecthegeek.github.io/blog/2007/02/06/handy-hack-how-to-use-vim-netrw-in-ftp-passive-mode/
Try "C:\Program Files\Vim\vim71\gvim.exe" --remote-tab !.! in WinSCP to open each file in a separate tab in the same gVim instance. I also clicked the "External Editor Opens Multiple Files in one window" checkbox. There is another option --remote-silent that will suppress the first warning that there is no gVim already running, but you cannot use it with the --remote-tab option. I prefer to ack. the one warning rather than making sure I'm in a new tab before opening anything.----
Use "C:\Program Files\Vim\vim71\gvim.exe" --remote-tab-silent !..! in WinSCP as External Editor works just fine.
For windows I would suggest adding PuTTY to the system path (or not) and simply set the g:netrw_cygwin=0, and g:netrw_scp_cmd=<PATH TO PSCP> and use Pageant to manage your private keys; that way you don't have to keep your password in your vimrc file. Pageant provides the key to PSCP when it tries to connect.
bcvi is a utility that works with SSH/SCP and the Vim NetRW plugin described above. When you log into a remote server with SSH+bcvi, you can
cd into any directory then type
vi filename and the gvim command will get launched back on your workstation, with the correct SCP URL to point to the file on the server. If that sounds confusing, the bcvi article clarifies things with examples and pictures.