Vim Tips Wiki
No edit summary
 
m (Reverted edits by 115.240.249.133 (talk | block) to last version by JohnBeckett)
 
(17 intermediate revisions by 12 users not shown)
Line 1: Line 1:
  +
{{duplicate|628|732}}
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=306
 
|id=306
  +
|previous=305
|title=Open a web-browser with the URL in the current line
 
  +
|next=308
|created=August 10, 2002 21:10
 
  +
|created=August 10, 2002
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Kartik Agaram
 
|author=Kartik Agaram
 
|version=5.7
 
|version=5.7
 
|rating=2041/515
 
|rating=2041/515
  +
|category1=Integration
|text=
 
  +
|category2=
function! Browser ()
 
 
let line = getline (".")
 
 
let line = matchstr (line, "http[^ ]*")
 
 
exec "!netscape ".line
 
 
endfunction
 
 
 
 
map <Leader>w :call Browser ()<CR>
 
 
}}
 
}}
  +
<pre>
  +
function! Browser ()
  +
let line = getline (".")
  +
let line = matchstr (line, "\%(http://\|www\.\)[^ ,;\t]*")
  +
exec "!netscape ".line
  +
endfunction
  +
map <Leader>w :call Browser ()<CR>
  +
</pre>
   
== Comments ==
+
==Comments==
  +
I use a similar script when editing html files to view changes made to the file.
http[^ ]* could be replaced with \%(http\|www\.).\{-}\&gt;
 
   
  +
<pre>
littledragon--AT--altern.org
 
  +
if exists("loaded_mozilla")
, August 12, 2002 2:48
 
  +
finish
----
 
  +
endif
Hrm... The above wouldn't work. Better try: \%(http://\|www\.\)[^ ,;\t]*
 
  +
let loaded_mozilla=1
   
  +
"Setup commands to run mozilla.
littledragon--AT--altern.org
 
  +
":Mozilla - open current file in mozilla.
, August 12, 2002 2:53
 
  +
if !exists(':Mozilla')
----
 
  +
command Mozilla :call s:StartMozilla()
I use a similar script when editing html files to view changes made to the file.
 
  +
endif
   
  +
function! s:StartMozilla()
if exists("loaded_mozilla")
 
  +
" let s:myfile = getcwd() . "/" . bufname("%")
finish
 
  +
let s:myfile = expand("%:p")
endif
 
  +
let s:a = "mozilla -remote 'openurl(file://"; . s:myfile . ")'"
let loaded_mozilla=1
 
  +
let s:r =system(s:a)
 
  +
"Mozilla is not running so start it."
"Setup commands to run mozilla.
 
  +
if s:r =~"No running window found."
 
  +
unlet s:a
":Mozilla - open current file in mozilla.
 
  +
let s:a = "mozilla " . s:myfile . "&"
 
  +
let s:r =system(s:a)
 
  +
endif
if !exists(':Mozilla')
 
  +
endfunction
command Mozilla :call s:StartMozilla()
 
  +
</pre>
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 . "&amp;"
 
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.
 
Both Netscape and Mozilla accept the remote argument which reloads an open browser with the supplied url.
   
ksteen--AT--users.sourceforge.net
 
, August 12, 2002 11:19
 
 
----
 
----
Here is a more generic way to execute a URL (windows only):
+
Here is a more generic way to execute a URL (Windows only):
vnoremap &lt;silent&gt; &lt;C-F5&gt; :&lt;C-U&gt;let old_reg=--AT--"&lt;cr&gt;gvy:silent!!cmd /cstart &lt;C-R&gt;&lt;C-R&gt;"&lt;CR&gt;&lt;CR&gt;:let --AT--"=old_reg&lt;cr&gt;
+
vnoremap &lt;silent&gt; &lt;C-F5&gt; :&lt;C-U&gt;let old_reg=@"&lt;CR&gt;gvy:silent!!cmd /cstart &lt;C-R&gt;&lt;C-R&gt;"&lt;CR&gt;&lt;CR&gt;:let @"=old_reg&lt;CR&gt;
   
If you visually highlight something, then hit CTRL-F5, it will tell windows to start the default associated application.
+
If you visually highlight something, then hit CTRL-F5, it will tell Windows to start the default associated application. {{script|id=306}}
  +
- On my machine this will launch Mozilla (since that is my default browser).
http://vim.sourceforge.net/tips/add_tip_note.php?tip_id=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.
dave.txt
 
- On my machine this will launch gvim, on default windows machines this would launch notepad.exe.
 
   
 
David Fishburn
 
, June 25, 2003 8:08
 
 
----
 
----
This is my modification. It works for http:, ftp: and file:
+
This is my modification. It works for http:, ftp: and file:
   
  +
<pre>
===============
 
function! Browser ()
+
function! Browser ()
let line0 = getline (".")
+
let line0 = getline (".")
let line = matchstr (line0, "http[^ ]*")
+
let line = matchstr (line0, "http[^ ]*")
:if line==""
+
:if line==""
let line = matchstr (line0, "ftp[^ ]*")
+
let line = matchstr (line0, "ftp[^ ]*")
:endif
+
:endif
:if line==""
+
:if line==""
let line = matchstr (line0, "file[^ ]*")
+
let line = matchstr (line0, "file[^ ]*")
:endif
+
:endif
" echo line
+
" echo line
exec ":silent !mozilla ".line
+
exec ":silent !mozilla ".line
endfunction
+
endfunction
  +
map \w :call Browser ()<CR>
  +
</pre>
   
  +
----
map \w :call Browser ()&lt;CR&gt;
 
  +
Further refinement: (For URL with #?&|%, such as one from a google search)
=================
 
   
  +
<pre>
  +
" 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>
  +
</pre>
   
Benyang Tang
 
, January 22, 2004 20:42
 
 
----
 
----
  +
Combining a couple previous scripts, here's what I came up with:
Further refinement: (For URL with &#35;?&amp;|%, such as one from a google search)
 
   
  +
<pre>
"=== evoke a web browser
 
  +
let $PATH = $PATH . ';c:\Program Files\Mozilla FireFox'
function! Browser ()
 
  +
"=== evoke a web browser
let line0 = getline (".")
 
  +
function! Browser ()
let line = matchstr (line0, "http[^ ]*")
 
  +
let line0 = getline (".")
:if line==""
 
let line = matchstr (line0, "ftp[^ ]*")
+
let line = matchstr (line0, "http[^ ]*")
  +
:if line==""
:endif
 
  +
let line = matchstr (line0, "ftp[^ ]*")
:if line==""
 
  +
:endif
let line = matchstr (line0, "file[^ ]*")
 
  +
:if line==""
:endif
 
let line = escape (line, "&#35;?&amp;;|%")
+
let line = matchstr (line0, "file[^ ]*")
  +
:endif
" echo line
 
  +
let line = escape (line, "#?&;|%")
exec ":silent !mozilla ".line
 
  +
:if line==""
endfunction
 
  +
let line = "\"" . (expand("%:p")) . "\""
 
  +
:endif
map \w :call Browser ()&lt;CR&gt;
 
  +
exec ':silent !firefox.exe ' . line
  +
endfunction
  +
map \w :call Browser ()<CR>
  +
</pre>
   
  +
----
  +
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
   
Benyang Tang
 
, January 23, 2004 0:28
 
 
----
 
----
  +
A workaround for this behaviour is to add ":redraw!<CR>" to the end of the mapping so that it looks like this:
in c how we can access pgm which is working currently at remote system by knowing its address and some other parameters
 
  +
<pre>
  +
map \w :call Browser ()<CR>:redraw!<CR>
  +
</pre>
  +
It will still change the buffer for a moment, though.
   
 
sanjeev.pawar--AT--gmail.com
 
, February 4, 2005 18:50
 
 
----
 
----
  +
I modified it so that URL that is passed to firefox is protected by quotes. The changed line is:
Combining a couple previous scripts, here's what I came up with:
 
  +
exec ':silent !firefox.exe ' . "\"" . line . "\""
 
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, "&#35;?&amp;;|%")
 
:if line==""
 
let line = "\"" . (expand("%:p")) . "\""
 
:endif
 
exec ':silent !firefox.exe ' . line
 
endfunction
 
 
map \w :call Browser ()&lt;CR&gt;
 
   
  +
The complete script now is:
   
  +
<pre>
'''Anonymous'''
 
  +
let $PATH = $PATH . ';c:\Programs\FireFox1.5'
, November 30, 2005 19:36
 
  +
" 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>
  +
</pre>
 
----
 
----
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
 
   
  +
Under Mac OS X, the '''open''' command can handle any URI:
kszabo--AT--gmx.de
 
  +
<pre>
, April 27, 2006 11:33
 
  +
function! HandleURI()
  +
let s:uri = matchstr(getline("."), '[a-z]*:\/\/[^ >,;:]*')
  +
echo s:uri
  +
if s:uri != ""
  +
exec "!open \"" . s:uri . "\""
  +
else
  +
echo "No URI found in line."
  +
endif
  +
endfunction
  +
map <Leader>w :call HandleURI()<CR>
  +
</pre>
 
----
 
----
I took Anonymous, November 30, 2005 19:36's script above
 
   
  +
OS X version that uses [http://daringfireball.net/2010/07/improved_regex_for_matching_urls John Gruber's URL regexp] and Ruby ([https://github.com/henrik/vim-open-url as a plugin]):
and modified it so that URL that is passed to firefox is protected by quotes:
 
   
  +
<pre>
The changed line is
 
  +
ruby << EOF
  +
def open_uri
  +
re = %r{(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))}
   
  +
line = VIM::Buffer.current.line
exec ':silent !firefox.exe ' . "\"" . line . "\""
 
   
  +
if url = line[re]
The complete script now is:
 
  +
system("open", url)
  +
VIM::message(url)
  +
else
  +
VIM::message("No URI found in line.")
  +
end
  +
end
  +
EOF
   
  +
if !exists("*OpenURI")
let $PATH = $PATH . ';c:\Programs\FireFox1.5'
 
  +
function! OpenURI()
"=== evoke a web browser
 
  +
:ruby open_uri
function! Browser ()
 
  +
endfunction
let line0 = getline (".")
 
  +
endif
let line = matchstr (line0, "http[^ ]*")
 
  +
map <Leader>w :call OpenURI()<CR>
:if line==""
 
  +
</pre>
let line = matchstr (line0, "ftp[^ ]*")
 
:endif
 
:if line==""
 
let line = matchstr (line0, "file[^ ]*")
 
:endif
 
let line = escape (line, "&#35;?&amp;;|%")
 
":if line==""
 
" let line = "\"" . (expand("%:p")) . "\""
 
":endif
 
exec ':silent !firefox.exe ' . "\"" . line . "\""
 
endfunction
 
 
map ,w :call Browser ()&lt;CR&gt;
 
 
TechCrazy
 
, October 13, 2006 1:01
 
 
----
 
----
Because of link spam trouble it's no longer possible to add a note to this tip. Sorry!
 
   
  +
Under Linux, this one-liner opens the URL under the cursor:
Bram--AT--vim.org
 
  +
<pre>
, January 11, 2007 11:25
 
  +
nnoremap <leader>w :silent !xdg-open <C-R>=escape("<C-R><C-F>", "#?&;\|%")<CR><CR>
----
 
  +
</pre>
<!-- parsed by vimtips.py in 0.503150 seconds-->
 

Latest revision as of 02:40, 25 November 2013

Duplicate tip

This tip is very similar to the following:

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

Tip 306 Printable Monobook 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>

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


A workaround for this behaviour is to add ":redraw!<CR>" to the end of the mapping so that it looks like this:

map \w :call Browser ()<CR>:redraw!<CR>

It will still change the buffer for a moment, though.


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>

Under Mac OS X, the open command can handle any URI:

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

OS X version that uses John Gruber's URL regexp and Ruby (as a plugin):

ruby << EOF
  def open_uri
    re = %r{(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))}

    line = VIM::Buffer.current.line

    if url = line[re]
      system("open", url)
      VIM::message(url)
    else
      VIM::message("No URI found in line.")
    end
  end
EOF

if !exists("*OpenURI")
  function! OpenURI()
    :ruby open_uri
  endfunction
endif
map <Leader>w :call OpenURI()<CR>

Under Linux, this one-liner opens the URL under the cursor:

nnoremap <leader>w :silent !xdg-open <C-R>=escape("<C-R><C-F>", "#?&;\|%")<CR><CR>