Vim logo vim online Vim Book Ad

 Tip #306: Open a web-browser with the URL in the current line

 tip karma   Rating 6/3, Viewed by 849 

created:   August 10, 2002 21:10      complexity:   intermediate
author:   Kartik Agaram      as of Vim:   5.7

function! Browser ()
    let line = getline (".")
    let line = matchstr (line, "http[^ ]*")
    exec "!netscape ".line
endfunction

map <Leader>w :call Browser ()<CR>

 rate this tip  Life Changing Helpful Unfulfilling 

<<Best of VIM Tips (VIM's best Features) | annoying "Hit any key to close this window..." >>

Additional Notes

[email protected], August 12, 2002 2:48
http[^ ]* could be replaced with \%(http\|www\.).\{-}\>
[email protected], August 12, 2002 2:53
Hrm... The above wouldn't work. Better try: \%(http://\|www\.\)[^ ,;\t]*
[email protected], August 12, 2002 11:19
I use a similar script when editing html files to view changes made to the file.

if exists("loaded_mozilla")
  finish
endif
let loaded_mozilla=1

"Setup commands to run mozilla.

":Mozilla - open current file in mozilla.


if !exists(':Mozilla')
  command Mozilla :call s:StartMozilla()
endif

function! s:StartMozilla()

" let s:myfile = getcwd() . "/" . bufname("%")
  let s:myfile = expand("%:p")
  let s:a = "mozilla -remote 'openurl(file://"; . s:myfile . ")'"
  let s:r =system(s:a)

  "Mozilla is not running so start it."
  if s:r =~"No running window found."
    unlet s:a
    let s:a = "mozilla " . s:myfile . "&"
    let s:r =system(s:a)
  endif

endfunction

Both Netscape and Mozilla accept the remote argument which reloads an open browser with the supplied url.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo