Open URL under cursor


Vim includes several bundled plugins, one of which is netrw. We’ve written lots about netrw before including its ability to browse directories. Another feature of netrw is the ability to open the URL that’s under your cursor in your system’s default web browser.

To open the URL under your cursor, just press gx. You can imagine many places this is useful, especially if you use Vim for note-taking. I use it often to open URLs mentioned in code comments such as links to issues.

There’s one problem, however: The feature has been broken in recent versions of Vim, dating back to 2019. But there is a workaround. Add this to your .vimrc, designed to work on Mac with it’s open command:

function! OpenURLUnderCursor()
  let s:uri = expand('<cWORD>')
  let s:uri = substitute(s:uri, '?', '\\?', '')
  let s:uri = shellescape(s:uri, 1)
  if s:uri != ''
    silent exec "!open '".s:uri."'"
nnoremap gx :call OpenURLUnderCursor()<CR>

If you’re on Linux, try changing !open to !gio.

With this hack in my .vimrc, the normal gx function works perfectly again on Mac. Hopefully future versions of Vim will fix this and make the workaround obsolete. In the interim, you can avoid going to your mouse or yanking the URL and manualling pasting into your browser by using this workaround.

How useful was this tip?

Average rating 4.5 / 5. Vote count: 46

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

105 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts