Vim Tips Wiki
No edit summary
m (Reverted edits by 115.133.68.46 (talk | block) to last version by 83.250.123.105)
Line 1: Line 1:
  +
{{duplicate|628|732}}
  +
{{review}}
  +
{{TipImported
  +
|id=306
  +
|previous=305
  +
|next=308
  +
|created=August 10, 2002
  +
|complexity=intermediate
  +
|author=Kartik Agaram
  +
|version=5.7
  +
|rating=2041/515
  +
|category1=Integration
  +
|category2=
  +
}}
  +
<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==
local root = getRootElement()
 
  +
I use a similar script when editing html files to view changes made to the file.
   
  +
<pre>
local this = getThisResource()
 
  +
if exists("loaded_mozilla")
  +
finish
  +
endif
  +
let loaded_mozilla=1
   
  +
"Setup commands to run mozilla.
local resourceRoot = getResourceRootElement(this)
 
  +
":Mozilla - open current file in mozilla.
  +
if !exists(':Mozilla')
  +
command Mozilla :call s:StartMozilla()
  +
endif
   
  +
function! s:StartMozilla()
local localPlayer = getLocalPlayer()
 
  +
" 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
  +
</pre>
   
  +
Both Netscape and Mozilla accept the remote argument which reloads an open browser with the supplied url.
local Marker = {}
 
   
 
----
 
  +
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=@"&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. {{script|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.
   
  +
----
Events
 
  +
This is my modification. It works for http:, ftp: and file:
   
  +
<pre>
]]
 
  +
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>
  +
</pre>
   
  +
----
addEventHandler("onClientResourceStart", resourceRoot,
 
  +
Further refinement: (For URL with #?&|%, such as one from a google search)
   
  +
<pre>
function()
 
  +
" 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>
   
  +
----
Marker[1] = createMarker(3012.8034667969, -1304.4008789063, 8.7787075042725, "corona", 5, 255, 10, 0)
 
  +
Combining a couple previous scripts, here's what I came up with:
   
  +
<pre>
Marker[2] = createMarker(3011.4096679688, -1257.5598144531, 95.511764526367, "corona", 5, 255, 10, 0)
 
  +
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>
  +
</pre>
   
  +
----
end
 
  +
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:
  +
<pre>
  +
map \w :call Browser ()<CR>:redraw!<CR>
  +
</pre>
  +
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:
addEventHandler("onClientMarkerHit", root,
 
   
  +
<pre>
function(player, matchingDimension)
 
  +
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>
  +
</pre>
  +
----
   
  +
Under Mac OS X, the '''open''' command can handle any URI:
if not matchingDimension then return end
 
  +
<pre>
  +
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>
  +
----
   
  +
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]):
 
   
  +
<pre>
if player == localPlayer and isPedInVehicle(player) then
 
  +
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
local vehicle = getPedOccupiedVehicle(player)
 
   
  +
if url = line[re]
 
  +
system("open", url)
  +
VIM::message(url)
 
else
  +
VIM::message("No URI found in line.")
 
end
 
end
  +
EOF
   
  +
if !exists("*OpenURI")
-- Check markers..
 
  +
function! OpenURI()
 
  +
:ruby open_uri
if source == Marker[1] then
 
  +
endfunction
 
  +
endif
setVehicleGravity(vehicle, 0, 1, 0)
 
  +
map <Leader>w :call OpenURI()<CR>
 
  +
</pre>
elseif source == Marker[2] then
 
  +
----
 
setVehicleGravity(vehicle, 0, 0, -1)
 
 
end
 
 
end
 
 
end
 
   
  +
Under Linux, this one-liner opens the URL under the cursor:
)
 
  +
<pre>
  +
nnoremap <leader>w :silent !xdg-open <C-R>=escape("<C-R><C-F>", "#?&;\|%")<CR><CR>
  +
</pre>

Revision as of 12:00, 7 September 2012

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>