I have a file like this:


I want to be able to open these sites with gx; this doesn't work because there's no protocol (http://) in front of these sites.

I tried to make a script to add the http:// prefix (this is trivial), but I'm not sure how I can then pass my string to gx:

fun! Goto(site)
    let l:site = a:site
    if l:site !~? '^https\?:\/\/'
        let l:site = 'https://' . l:site

    " Problem: how to I pass l:site to gx?
    exe 'normal gx'

nnoremap <Leader>g :call Goto(getline('.'))<CR>

I looked at /usr/share/vim/vim74/plugin/netrwPlugin.vim to see how it's mapped:

if !exists("g:netrw_nogx")
 if maparg('gx','n') == ""
  if !hasmapto('<Plug>NetrwBrowseX')
   nmap <unique> gx <Plug>NetrwBrowseX
  nno <silent> <Plug>NetrwBrowseX :call netrw#BrowseX(expand((exists("g:netrw_gx")? g:netrw_gx :            █'<cfile>')),netrw#CheckIfRemote())<cr>
 if maparg('gx','v') == ""
  if !hasmapto('<Plug>NetrwBrowseXVis')
   vmap <unique> gx <Plug>NetrwBrowseXVis
  vno <silent> <Plug>NetrwBrowseXVis :<c-u>call netrw#BrowseXVis()<cr>

I can't call <Plug>NetrwBrowseX (AFAIK?) ... Is there some other way to do this?

  • The hasmapto() test and <Plug>... stuff permits you to customize; instead of gx, for example, you could have vmap <unique> GX <Plug>NetrwBrowseX in your .vimrc, and GX would be the map to invoke the special-file handler.
    – user21497
    Jan 28 '16 at 19:39

The gx mapping is calling netrw#BrowseX(), so you could call that at the end of your function, passing in the l:site variable you've constructed:

call netrw#BrowseX(l:site, netrw#CheckIfRemote())

I would also suggest that instead of getline('.'), you use expand('<cfile>'), which evaluates to the filename under the cursor. This will work when the URL is on a line containing other content as well.

  • Thanks; not sure how I missed this yesterday; I was confused by the <Plug>NetrwBrowseX (I thought that was the function being called), it's fairly obvious now :-) ... Using <cfile> is good to know, but not something I can use here since this is part of a larger script. Feb 13 '15 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.