Open URL under cursor in Vim with browser

If you are using Vim 7.4 or later, in normal mode, put your cursor below the URL, then click gx, the URL will be opened in browser automatic demo operate


Updated: from tpope's tweet today

Press gx. You can customize the browser. On Gnome and Mac OS X it's already use gnome-open/open. Generally you can set g:netrw_browsex_viewer to anything you want.


Original answer:

Don't remember where I get this function. There is a bug with hash (#) in the url, but the function works well enough that I won't bother fixing it.

function! HandleURL()
  let s:uri = matchstr(getline("."), '[a-z]*:\/\/[^ >,;]*')
  echo s:uri
  if s:uri != ""
    silent exec "!open '".s:uri."'"
  else
    echo "No URI found in line."
  endif
endfunction
map <leader>u :call HandleURL()<cr>

Note: If you are not on the Mac, use gnome-open/xdg-open for Linux, or 'path to your web browser' for Windows

Tags:

Vim