sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #337: editing remote files via scp in vim

 tip karma   Rating 312/107, Viewed by 4741 

created:   October 3, 2002 12:56      complexity:   intermediate
author:   Matthew Weier O'Phinney      as of Vim:   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 wierd, particularly if you're not editing a document under your user tree.

To get around this, try opening the file as follows:
    vim scp://[email protected]//path/to/document
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.

 rate this tip  Life Changing Helpful Unfulfilling 

<<type the line number and press enter to get there | vim + cscope + cygwin >>

Additional Notes

nickm(at)mail.sochi.ru, October 14, 2002 13:41
Good tip!

ps RTFM ;-)
[email protected], December 24, 2002 0:25
Does the same work for FTP as well?.
When I try to do it using ftp am not able to open the file(yes, it exists on the server!), but vim returns an error saying 4 lines filtered and some information about not being able to open a file in the tmp directory

TIA,
kris
tevfik, March 20, 2003 12:56
I need ftp too...
tevfik, March 20, 2003 13:04
Hey.. this is great.. it is working for ftp.. REALLY GREAT.
[email protected], April 8, 2003 9:14
The latest netrw.vim, available under

     http://www.erols.com/astronaut/vim/index.html#vimlinks_scripts
     as "Network Oriented Reading and Writing"

has several improvements.  Later Windows' ftp is handled, new protocols
(rsync, cadaver, fetch), user fixup functions, etc.
[email protected], July 4, 2003 8:46
The tip is great. I have been looking for a way to work remotely like this for months
(well, admittedly not looking very much, but really wanting it!).
I just would need an extra feature which doesn't seem to work:
:E in the remote file is not able to navigate through the remote folder.
Anyone knows if this should work?

Thanks!
[email protected], July 5, 2003 11:45
I was wondering if anyone knew how to edit files remotely as described above but manually specifying the port to be used by scp.
I have an ssh server running on a different port and am not sure if this is supported by vim.

Any advice?

thanks
[email protected], September 2, 2003 17:44
Yes.  Change the g:netrw_scp_cmd to include a "-p ###" bit.  That should work.  (I have not tried it.)
[email protected], October 22, 2003 11:17
how can we store the password?? it prompts for password each time we save!
Anonymous, November 4, 2003 10:36
I just got this working on Win2k w/ PuTTY's command line scp program http://www.chiark.greenend.org.uk/~sgtatham/putty/
1) copy pscp.exe into your path somewhere as scp.exe
2) put "let g:netrw_cygwin= 0" in your $VIM/_vimrc
[email protected], January 20, 2004 9:31
If you don't want to bother typing in passwords, have a look at http://hacks.oreilly.com/pub/h/66 to see how to set up public keys.
[email protected], February 3, 2004 19:05
ftp works for me, but syntax highlighting went away?
Anonymous, February 20, 2004 3:29
fantabulous vim extension.  makes life much easier.  thank you
Anonymous, April 7, 2004 4:34
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

:!rcp scp://[email protected]:t1

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.
[email protected] - NOSPAM, June 7, 2004 22:38
Using relative paths is quite normal and well supported.  Try

:r scp://m@machine/t1
[email protected], June 15, 2004 12:40
This is a great tip, and it certainly works.

However, on Win98 and Win2k using gVim 6.3, if you open multiple buffers via ftp you cannot switch between them using the Buffers menu.

Is there a way around this?
greencat_98 at yahoo dot com, June 17, 2004 7:04
Someone was asking if you could define the port for ftp

vim ftp://[user@]machine[[:#]portnumber]/path

try that...just like any other url.

vim ftp://[email protected]:6090/public_html/index.html

I guess that would work.
[email protected], July 6, 2004 4:22
regarding the "can't edit a file with netrw ftp"; "can't open a file in the temp directory error message"

this error occurs when you don't specify a file name, but a directory name! gave me headaches... so it looks like when you want to edit a remote file over ftp, you must specify the file name, a directory won't suffice

BTW: i've just switched to vim from emacs and must say vim is a nice editor :), although i'm used to the emacs like interface for ftp editing (which is more conveniant i must say sofar). does anybody know a way to edit files remotely over ftp in vi with a browsing like mode? i mean, to look at a remove directory and then decide which files to edit instead of manually typing in the entire directory path and filename.
[email protected] - NOSPAM, July 6, 2004 8:31
As mentioned in vimtip#744, one can get the latest netrw from http://mysite.verizon.net/astronaut/index.html#vimlinks_scripts as "Network Oriented Reading and Writing".  Although its still under development, the netrw there currently supports both local and remote browsing; it strongly resembles the file explorer plugin.
[email protected] - NOSPAM, July 6, 2004 8:34
Sorry, that url should be: http://mysite.verizon.net/astronaut/vim/index.html#vimlinks_scripts
me[NOSPAM]@michaelbelt.com, July 14, 2004 19:17
Does anybody know how keep the dos window from popping up when using scp, on Win32?
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo