History
Article Edit this page Discussion

Open a web-browser with the URL in the current line

From Vim Tips Wiki

Jump to: navigation, search

[edit] Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 306 Previous Next Created: August 10, 2002 Complexity: intermediate Author: Kartik Agaram Version: 5.7


function! Browser ()
  let line = getline (".")
  let line = matchstr (line, "\%(http://\|www\.\)[^ ,;\t]*")
  exec "!netscape ".line
endfunction
map <Leader>w :call Browser ()<CR>

[edit] Comments

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.


Here is a more generic way to execute a URL (Windows only):

vnoremap <silent> <C-F5> :<C-U>let old_reg=@"<CR>gvy:silent!!cmd /cstart <C-R><C-R>"<CR><CR>:let @"=old_reg<CR>

If you visually highlight something, then hit CTRL-F5, it will tell Windows to start the default associated application. script#306 - On my machine this will launch Mozilla (since that is my default browser). dave.txt - On my machine this will launch gvim, on default windows machines this would launch notepad.exe.


This is my modification. It works for http:, ftp: and file:

function! Browser ()
  let line0 = getline (".")
  let line = matchstr (line0, "http[^ ]*")
  :if line==""
  let line = matchstr (line0, "ftp[^ ]*")
  :endif
  :if line==""
  let line = matchstr (line0, "file[^ ]*")
  :endif
  " echo line
  exec ":silent !mozilla ".line
endfunction
map \w :call Browser ()<CR>

Further refinement: (For URL with #?&|%, such as one from a google search)

" Evoke a web browser
function! Browser ()
  let line0 = getline (".")
  let line = matchstr (line0, "http[^ ]*")
  :if line==""
  let line = matchstr (line0, "ftp[^ ]*")
  :endif
  :if line==""
  let line = matchstr (line0, "file[^ ]*")
  :endif
  let line = escape (line, "#?&;|%")
  " echo line
  exec ":silent !mozilla ".line
endfunction
map \w :call Browser ()<CR>

Combining a couple previous scripts, here's what I came up with:

let $PATH = $PATH . ';c:\Program Files\Mozilla FireFox'
"=== evoke a web browser
function! Browser ()
  let line0 = getline (".")
  let line = matchstr (line0, "http[^ ]*")
  :if line==""
  let line = matchstr (line0, "ftp[^ ]*")
  :endif
  :if line==""
  let line = matchstr (line0, "file[^ ]*")
  :endif
  let line = escape (line, "#?&;|%")
  :if line==""
  let line = "\"" . (expand("%:p")) . "\""
  :endif
  exec ':silent !firefox.exe ' . line
endfunction
map \w :call Browser ()<CR>

ever since i used this command it bothered me that the screen messes up after calling the function. so i decided to use "urlview". well you got to hit enter quit a few times, but you also get all urls presented foud in the current buffer. you can map ":!urlsview %" to something you like


I modified it so that URL that is passed to firefox is protected by quotes. The changed line is:

exec ':silent !firefox.exe ' . "\"" . line . "\""

The complete script now is:

let $PATH = $PATH . ';c:\Programs\FireFox1.5'
" Evoke a web browser
function! Browser ()
  let line0 = getline (".")
  let line = matchstr (line0, "http[^ ]*")
  :if line==""
  let line = matchstr (line0, "ftp[^ ]*")
  :endif
  :if line==""
  let line = matchstr (line0, "file[^ ]*")
  :endif
  let line = escape (line, "#?&;|%")
  ":if line==""
  " let line = "\"" . (expand("%:p")) . "\""
  ":endif
  exec ':silent !firefox.exe ' . "\"" . line . "\""
endfunction
map ,w :call Browser ()<CR>

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto Wiki
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Open a web-browser with the URL in the current line"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation