3

I have a file in Mediawiki format, containing internal links (in a Mediawiki installation)

* [[Topic A]]
* [[Topic B]]
* [[Vim]]

(The asterisks are how MediaWiki lists are formatted, they are not mandatory, as the solution should work on [[Topic A]] [[Topic B]] [[Vim]] as well.)

I want to open these links with their full URL, e.g. https://en.wikipedia.org/wiki/Vim.

  1. Can I e.g. use something like gx (which works on complete URLs) for an individual list entry, but having it complete the rest of the URL and opening it the browser?
  2. Is there a solution to open all listed elements at once?

Thank you.

  • 1
    We’d need to understand the URL construction scheme. Is the format guaranteed for the links? Is that a list of links (i.e., are the asterisks always present)? – D. Ben Knoble Nov 13 '19 at 12:53
2

gx is normally processed by netrw plugin. You can try to plug into it like this:

function g:ExpandWiki(expr)
    " perform standard expand() for netrw
    let l:url = expand(a:expr)
    " check if it looks like "wiki" URL
    let l:wiki = matchstr(l:url, '\[\[\zs.*\ze\]\]')
    " root should be patched to match your installation
    return empty(l:wiki) ? l:url : 'https://en.wikipedia.org/wiki/' . l:wiki
endfunction

" redefine <Plug>NetrwBrowseX to use our custom expansion
let rhs = maparg('<Plug>NetrwBrowseX', 'n')
if !empty(rhs)
    exe 'nn <silent><Plug>NetrwBrowseX' substitute(rhs, 'expand(', 'ExpandWiki(', '')
else
    echoerr 'Netrw is not found!'
    echoerr 'Perhaps, you may want to run this code from ~/.vim/after/plugin'
endif

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.