Vim Tips Wiki
Register
Advertisement
Tip 337 Printable Monobook Previous Next

created October 3, 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:

vim scp://remoteuser@server.tld//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.

Comments

The latest netrw.vim, available under http://mysite.verizon.net/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.


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

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.


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://m@mymachine.com: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.


Using relative paths is quite normal and well supported. Try

:r scp://m@machine/t1

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://stankonia@domainname.com:6090/public_html/index.html

I guess that would work.


As mentioned in VimTip744, one can get the latest netrw from http://mysite.verizon.net/astronaut/vim/index.html#vimlinks_scripts as "Network Oriented Reading and Writing". Although it's still under development, the netrw there currently supports both local and remote browsing; it strongly resembles the file explorer plugin.


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.

Run command:

gvim <host>//<path_2_file> ftp://<host>//<path_2_file>;

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:

gvim scp://user@server.xxx.cz/file.txt

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"

vim scp://root@example.com/~user/public_html/.htaccess

That was a lot nicer than having to bother with:

vim scp://root@example.com//home/user/public_html/.htaccess

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://root@example.com//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:

:e scp://my_user@remote_hostname//path/to/remote/file

--> 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.wordpress.com/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.

--Preceding unsigned comment added by 161.88.255.139 (talkcontribs) 20:49, 18 April 2008


Advertisement