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
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."'" :redraw! endif endfunction nnoremap gx :call OpenURLUnderCursor()<CR>
If you’re on Linux, try changing
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.
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?