Search code examples
vimftp.netrc

Using .netrc-file with Vim (as an alternative to DreamWeavers FTP-client)


I'm trying to use Vim as a substitute for DreamWeavers nice FTP-client sidebar. But I would like for Vim to 'remember' my logins, - so I don't need to get the FTP-address, the username and the password, every time I need to connect to a remote server, to make some changes. I've made it as far as to figure out, that one can setup a .netrc-file, where you can save login-credentials (and possibly encrypt it using GPG, which I will look into, after I've goten this to work). I can find endless manuals and tutorials about how to setup the .netrc-file, - but no information on how to actually use this .netrc-file.

So if my .netrc-file looks like this:

machine <hostname1>
login <login1>
password <password1>
machine <hostname2>
login <login2>
password <password2>

Then how to I for instance connect to <hostname2>? Do I write vim <hostname2> - and if so, then what if I have several different remote servers on the same host (where the user and password-credentials vary)?

Ideally, I would like to just be able to write something along the lines of

vim ftp://MY_OWN_NICKNAME_FOR_A_REMOTE_SERVER

Whereafter Vim will find the credentials in the .netrc-file and connect me to the given server. How do I do this?


Solution

  • The .netrc file supplies vim with usernames and passwords of remote servers. On the command line, when vim is supplied with a hostname that matches a machine name defined in .netrc, it plugs in the username and password into the url. For example, if I wanted to connect to ftp.server1.com and automatically login I would need to do set up the following in my .netrc file:

    $ < .netrc 
    machine ftp.server1.com login username password "secretpassword"
    $ vim ftp://ftp.server1.com/ 
    

    If I wanted multiple hostnames setup in my .netrc, I would just add a new entry:

    $ < .netrc 
    machine ftp.server1.com login username password "secret password"
    machine ftp.server2.com login username password "secretpassword1"
    machine ftp.server3.com login different_username password "secretpassword2"
    

    What may be confusing is that you can not alias the hostname. When I first starting using this feature, I thought machine was a variable I could supply -- but this is not the case. That said, a shell aliases will fill-in for vim's lack:

    $alias server1=ftp://ftp.server1.com/
    $vim $server1
    

    Note: you need that trailing / on the urls to tell vim you're browsing. Also note there are further complications when using a windows os as detailed in :help netrw.