vim

Open URL under cursor in Vim with browser


I'm using Twitvim for the first time. Seeing all the URLs in there made me wonder, is there any way to open the URL under the cursor in your favorite browser or a specified one?


Solution

  • 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