Vim Tips Wiki
m (Search the web for text selected in vim. moved to Search the web for text selected in Vim: Page moved by JohnBot to improve title)
No edit summary
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=933
 
|id=933
  +
|previous=931
|title=search the web for text selected in vim.
 
  +
|next=934
|created=May 22, 2005 11:44
+
|created=2005
 
|complexity=basic
 
|complexity=basic
|author=http://bombay11.blogspot.com
+
|author=MA
 
|version=6.0
 
|version=6.0
 
|rating=21/10
 
|rating=21/10
  +
|category1=
|text=
 
  +
|category2=
This tip allows you to search the web for a selected phrase from vim.
 
 
}}
  +
This tip allows you to search the web for a selected phrase from Vim. This is especially useful to lookup spellings of proper nouns, phrases, function prototypes and man pages on the web, while working in Vim.
   
 
Usage:
This is especially useful to lookup spellings of proper nouns, phrases,
 
 
*Add the vmap ?? line given below to your vimrc.
 
*In Vim press v to begin selection.
 
*move cursor to end of selection (region will be highlighted).
 
*Press ??
 
*The search results will appear in your web browser.
   
  +
<pre>
function prototypes and man pages on the web, while working in vim.
 
 
:vmap ?? <Esc>:silent exec
 
\ ":!c:/opera/6*/opera.exe \\\"http://www.google.com/search?q=".substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g")
 
\ . "\\\" "<CR><CR>
  +
</pre>
   
 
The vmap takes the visually selected region, and removes all non word characters and single characters in region, and launches the query on the phrase.
   
  +
You should replace c:/opera/6*/opera.exe by path to your browser, or you can get the opera from http://www.opera.com for windows/linux, opera is super fast and safe in textmode with images and java turned off.
   
 
You can replace the vmap selection by <cword> above to query for <word under cursor>.
Usage:
 
   
 
==Comments==
0. Add the vmap ?? line given below to your ~/_vimrc
 
 
Do you have too many backslashes? You may consider using <code>!start</code> also.
   
1. In Vim press v to begin selection,
 
 
2. move cursor to end of selection (region will be highlighted).
 
 
3. Press ??
 
 
4. The search results will appear in your web browser.
 
 
 
 
:vmap ?? &lt;ESC&gt;:silent exec
 
 
\ ":!c:/opera/6*/opera.exe \\\"http://www.google.com/search?q=".substitute(@*,"\\W\\+\\\\|\\&lt;\\w\\&gt;"," ","g")
 
 
\ . "\\\" "&lt;CR&gt;&lt;CR&gt;
 
 
 
 
Notes and explanation:
 
 
 
 
The vmap takes the visually selected region, and
 
 
removes all non word characters and single characters in region, and
 
 
launches the query on the phrase.
 
 
 
 
Customizations:
 
 
 
 
You should replace c:/opera/6*/opera.exe by path to your browser,
 
 
or you can get the opera from http://www.opera.com for windows/linux,
 
 
opera is super fast and safe in textmode with images and java turned off.
 
 
 
 
You can replace the vmap selection by &lt;cword&gt; above
 
 
to query for &lt;word under cursor&gt;.
 
 
 
 
- MA
 
 
http://www.cs.albany.edu/~mosh
 
 
 
}}
 
 
== Comments ==
 
Hi Moshi
 
Do you have too many backslashes?, could you put a plaintext version of this mapping on your website ie google.vim or google.txt.
 
You may consider using !start also
 
 
zzapper--AT--ntlworld.com
 
, May 23, 2005 2:34
 
 
----
 
----
The backslashes are because of my shell setting in vim (I have sh.exe):
+
The backslashes are because of my shell setting in vim (I have sh.exe):
   
  +
<pre>
if has("win32")
+
if has("win32")
set shell=sh shellslash shellcmdflag=-c shellxquote=\" shellpipe=\|\ tee
+
set shell=sh shellslash shellcmdflag=-c shellxquote=\" shellpipe=\|\ tee
endif
+
endif
  +
</pre>
   
cmd.exe will need lesser backslashes.
+
cmd.exe will need lesser backslashes.
   
 
 
 
MA
 
, May 23, 2005 13:56
 
 
----
 
----
I just looked up the quoting syntax of cmd.exe on win2k,
+
I just looked up the quoting syntax of cmd.exe on win2k, the quoting syntax is very limited but this works on windows:
the quoting syntax is very limited but this works on windows:
 
   
  +
<pre>
if &amp;shell =~ "cmd"
+
if &shell =~ "cmd"
   
vmap ?? &lt;ESC&gt;:exec
+
vmap ?? <Esc>:exec
\ ":!c:/opera/61/opera.exe http://www.google.com/search?q=\""
+
\ ":!c:/opera/61/opera.exe http://www.google.com/search?q=\""
\ . substitute(--AT--*,"\\W\\+\\\\|\\&lt;\\w\\&gt;"," ","g")
+
\ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g")
\ . "\""&lt;CR&gt;&lt;CR&gt;
+
\ . "\""<CR><CR>
   
else
+
else
   
:vmap ?? &lt;ESC&gt;:silent exec
+
:vmap ?? <Esc>:silent exec
\ ":!c:/opera/61/opera.exe \\\"http://www.google.com/search?q=";
+
\ ":!c:/opera/61/opera.exe \\\"http://www.google.com/search?q=";
\ . substitute(--AT--*,"\\W\\+\\\\|\\&lt;\\w\\&gt;"," ","g")
+
\ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g")
\ . "\\\" "&lt;CR&gt;&lt;CR&gt;
+
\ . "\\\" "<CR><CR>
   
endif
+
endif
  +
</pre>
 
   
MA
 
, May 23, 2005 20:58
 
 
----
 
----
A less flexible but simpler method (search for word under the cursor)
+
A less flexible but simpler method (search for word under the cursor)
   
  +
<pre>
nmap ,g :silent !start c:\progra~1\opera75\opera.exe [http://www.google.com/search?q=<cWORD><CR> http://www.google.com/search?q=&lt;cWORD&gt;&lt;CR&gt;];
+
nmap ,g :silent !start c:\progra~1\opera75\opera.exe [http://www.google.com/search?q=<cWORD><CR> http://www.google.com/search?q=<cWORD><CR>];
  +
</pre>
   
zzapper--AT--ntlworld.com
 
, May 24, 2005 3:53
 
 
----
 
----
As zapper pointed out, a 'start' is needed to launch opera in background,
+
As pointed out, a 'start' is needed to launch opera in background. This works with sh and cmd.exe:
this works with sh and cmd.exe
 
   
  +
<pre>
vmap ?? &lt;ESC&gt;:silent exec
+
vmap ?? <Esc>:silent exec
\ ":!start c:/opera/6/opera.exe http://www.google.com/search?q=\""
+
\ ":!start c:/opera/6/opera.exe http://www.google.com/search?q=\""
\ . substitute(--AT--*,"\\W\\+\\\\|\\&lt;\\w\\&gt;"," ","g")
+
\ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g")
\ . "\""&lt;CR&gt;&lt;CR&gt;
+
\ . "\""<CR><CR>
  +
</pre>
   
MA
 
, May 24, 2005 17:15
 
 
----
 
----
you can use single-quote to lessen the backslash
+
You can use single-quote to lessen the backslash:
vmap ?? &lt;ESC&gt;:exec
 
\ ':!c:/opera/61/opera.exe http://www.google.com/search?q=";'
 
\ . substitute(--AT--*,'\W\+\\|\&lt;\w\&gt;'," ","g")
 
\ . '"'&lt;CR&gt;&lt;CR&gt;
 
   
  +
<pre>
 
vmap ?? <Esc>:exec
 
\ ':!c:/opera/61/opera.exe http://www.google.com/search?q=";'
 
\ . substitute(@*,'\W\+\\|\<\w\>'," ","g")
 
\ . '"'<CR><CR>
  +
</pre>
   
 
slimzhao9527--AT--21cn.com
 
, May 24, 2005 17:25
 
 
----
 
----
Finally this works on vim63 / Xterm / Linux with Opera 8.0 also.
+
Finally this works on vim63 / Xterm / Linux with Opera 8.0 also.
   
  +
<pre>
if $TERM =~ "xterm"
+
if $TERM =~ "xterm"
   
vmap ?? y&lt;ESC&gt;:silent exec
+
vmap ?? y<Esc>:silent exec
\ ":!/usr/bin/opera http://www.google.com/search?q='";
+
\ ":!/usr/bin/opera http://www.google.com/search?q='";
\ . substitute(--AT--","\\W\\+\\\\|\\&lt;\\w\\&gt;",'\\%20',"g")
+
\ . substitute(@","\\W\\+\\\\|\\<\\w\\>",'\\%20',"g")
\ . "' &amp;"&lt;CR&gt;&lt;CR&gt;
+
\ . "' &"<CR><CR>
   
else
+
else
   
vmap ?? &lt;ESC&gt;:silent exec
+
vmap ?? <Esc>:silent exec
\ ":!start c:/opera/6/opera.exe http://www.google.com/search?q=\""
+
\ ":!start c:/opera/6/opera.exe http://www.google.com/search?q=\""
\ . substitute(--AT--*,"\\W\\+\\\\|\\&lt;\\w\\&gt;"," ","g")
+
\ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g")
\ . "\""&lt;CR&gt;&lt;CR&gt;
+
\ . "\""<CR><CR>
   
endif
+
endif
  +
</pre>
   
Thanks for suggestions about 'start/cmd' and single quotes, I had to use
 
%20 as protected space (maybe a quirk of my bash or opera80).
 
 
- Mohsin.
 
 
http://pipal.blogspot.com
 
, May 24, 2005 18:22
 
 
----
 
----
 
I still had trouble with all that slashville, so I did another QAD solution.
Hi
 
I still had trouble with all that slashville, so I did another QAD solution.
 
   
  +
<pre>
vmap ,g "zy:let --AT--z = substitute(--AT--z,'[[:space:]]','+','g')&lt;CR&gt;
+
vmap ,g "zy:let @z = substitute(@z,'[[:space:]]','+','g')<CR>
\ :silent !start c:\progra~1\opera75\opera.exe
+
\ :silent !start c:\progra~1\opera75\opera.exe
\ [http://www.google.com/search?q=<C-R>z<CR> http://www.google.com/search?q=&lt;C-R&gt;z&lt;CR&gt;];
+
\ [http://www.google.com/search?q=<C-R>z<CR> http://www.google.com/search?q=<C-R>z<CR>];
  +
</pre>
   
  +
<pre>
explanation
 
  +
Explanation:
"zy &#35; yank visual area to register y
+
"zy # yank visual area to register z
let..substitute &#35; turn spaces into + (wot Google wants)
+
let..substitute # turn spaces into + (wot Google wants)
silent..opera.exe &#35; where my opera is (BTW Opera is really kool!)
+
silent..opera.exe # where my opera is (BTW Opera is really kool!)
&lt;C-R&gt;z &#35; recall contents of z
+
<C-R>z # recall contents of z
  +
</pre>
   
zzapper
 
, May 26, 2005 10:39
 
 
----
 
----
 
You can de-uglify your maps a little by putting your browser path into a VIM variable
Hi
 
  +
You can de-uglify your maps a little by putting your browser path into a VIM variable
 
  +
<pre>
 
let $opera="c:\progra~1\opera75\opera.exe"
 
let $opera="c:\progra~1\opera75\opera.exe"
  +
</pre>
   
zzapper
 
, May 26, 2005 10:47
 
 
----
 
----
 
As an extension to this for definitions and spellings, I included this as well as the mapping for a google search:
Thanks for this, a very useful tip.
 
As an extension to this for definitions and spellings, I included this as well as the mapping for a google search:
 
   
  +
<pre>
vmap ?w &lt;ESC&gt;:exec
+
vmap ?w <Esc>:exec
\ ':!/usr/bin/mozilla http://dictionary.reference.com/search?q=";'
+
\ ':!/usr/bin/mozilla http://dictionary.reference.com/search?q="'
\ . substitute(--AT--*,'\W\+\\|\&lt;\w\&gt;'," ","g")
+
\ . substitute(@*,'\W\+\\|\<\w\>'," ","g")
\ . '"'&lt;CR&gt;&lt;CR&gt;
+
\ . '"'<CR><CR>
 
  +
</pre>
I find this useful, hope someone else will.
 
Rob.
 
 
Anonymous
 
, June 30, 2005 1:18
 
----
 
Don't know where that semicolon after: search?q=" appeared from, but you don't need it, unless you want all your word searches to start with a semicolon :-).
 
Rob.
 
   
Anonymous
 
, June 30, 2005 1:20
 
 
----
 
----
I modified this a bit. Instead of entering Visual mode, selecting the word, and then launching the search, I wanted to just search directly from Normal mode. Additionally, I wanted to launch the default system browser, regardless of product or path. Note: This probably only works under Windows. Once I have a chance to test under Linux, I'll see if I can hack out something cross-platform.
+
I modified this a bit. Instead of entering Visual mode, selecting the word, and then launching the search, I wanted to just search directly from Normal mode. Additionally, I wanted to launch the default system browser, regardless of product or path. Note: This probably only works under Windows. Once I have a chance to test under Linux, I'll see if I can hack out something cross-platform.
   
  +
<pre>
map ?g "zyiw
+
map ?g "zyiw
\ :exec ':silent ! start http://www.google.com/search?q=";'.--AT--z.'"'&lt;CR&gt;
+
\ :exec ':silent ! start http://www.google.com/search?q=";'.@z.'"'<CR>
  +
</pre>
   
This takes the current word under the cursor, saves it to "z, then launches google silently in the default browser passing --AT--z as the search parameter. Very clean, very simple. :-)
+
This takes the current word under the cursor, saves it to "z, then launches google silently in the default browser passing @z as the search parameter. Very clean, very simple.
   
  +
<pre>
map ?m "zyiw
+
map ?m "zyiw
\ :exec ':silent ! start http://www.m-w.com/cgi-bin/dictionary?va=";'.--AT--z.'"'&lt;CR&gt;
+
\ :exec ':silent ! start http://www.m-w.com/cgi-bin/dictionary?va=";'.@z.'"'<CR>
  +
</pre>
   
 
This does essentially the same, but looks up the word in the Merriam-Webster dictionary instead of Google.
 
This does essentially the same, but looks up the word in the Merriam-Webster dictionary instead of Google.
   
'''Anonymous'''
 
, October 6, 2005 20:48
 
 
----
 
----
<!-- parsed by vimtips.py in 0.723317 seconds-->
 

Latest revision as of 16:49, 8 July 2013

Tip 933 Printable Monobook Previous Next

created 2005 · complexity basic · author MA · version 6.0


This tip allows you to search the web for a selected phrase from Vim. This is especially useful to lookup spellings of proper nouns, phrases, function prototypes and man pages on the web, while working in Vim.

Usage:

  • Add the vmap ?? line given below to your vimrc.
  • In Vim press v to begin selection.
  • move cursor to end of selection (region will be highlighted).
  • Press ??
  • The search results will appear in your web browser.
:vmap ?? <Esc>:silent exec
 \ ":!c:/opera/6*/opera.exe \\\"http://www.google.com/search?q=".substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g")
 \ . "\\\" "<CR><CR>

The vmap takes the visually selected region, and removes all non word characters and single characters in region, and launches the query on the phrase.

You should replace c:/opera/6*/opera.exe by path to your browser, or you can get the opera from http://www.opera.com for windows/linux, opera is super fast and safe in textmode with images and java turned off.

You can replace the vmap selection by <cword> above to query for <word under cursor>.

Comments[]

Do you have too many backslashes? You may consider using !start also.


The backslashes are because of my shell setting in vim (I have sh.exe):

if has("win32")
  set shell=sh shellslash shellcmdflag=-c shellxquote=\" shellpipe=\|\ tee
endif

cmd.exe will need lesser backslashes.


I just looked up the quoting syntax of cmd.exe on win2k, the quoting syntax is very limited but this works on windows:

if &shell =~ "cmd"

vmap ?? <Esc>:exec
 \ ":!c:/opera/61/opera.exe http://www.google.com/search?q=\""
 \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g")
 \ . "\""<CR><CR>

else

:vmap ?? <Esc>:silent exec
 \ ":!c:/opera/61/opera.exe \\\"http://www.google.com/search?q=";
 \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g")
 \ . "\\\" "<CR><CR>

endif

A less flexible but simpler method (search for word under the cursor)

nmap ,g :silent !start c:\progra~1\opera75\opera.exe [http://www.google.com/search?q=<cWORD><CR> http://www.google.com/search?q=<cWORD><CR>];

As pointed out, a 'start' is needed to launch opera in background. This works with sh and cmd.exe:

vmap ?? <Esc>:silent exec
 \ ":!start c:/opera/6/opera.exe http://www.google.com/search?q=\""
 \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g")
 \ . "\""<CR><CR>

You can use single-quote to lessen the backslash:

vmap ?? <Esc>:exec
 \ ':!c:/opera/61/opera.exe http://www.google.com/search?q=";'
 \ . substitute(@*,'\W\+\\|\<\w\>'," ","g")
 \ . '"'<CR><CR>

Finally this works on vim63 / Xterm / Linux with Opera 8.0 also.

if $TERM =~ "xterm"

vmap ?? y<Esc>:silent exec
 \ ":!/usr/bin/opera http://www.google.com/search?q='";
 \ . substitute(@","\\W\\+\\\\|\\<\\w\\>",'\\%20',"g")
 \ . "' &"<CR><CR>

else

vmap ?? <Esc>:silent exec
 \ ":!start c:/opera/6/opera.exe http://www.google.com/search?q=\""
 \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g")
 \ . "\""<CR><CR>

endif

I still had trouble with all that slashville, so I did another QAD solution.

vmap ,g "zy:let @z = substitute(@z,'[[:space:]]','+','g')<CR>
 \ :silent !start c:\progra~1\opera75\opera.exe
 \ [http://www.google.com/search?q=<C-R>z<CR> http://www.google.com/search?q=<C-R>z<CR>];
Explanation:
"zy # yank visual area to register z
let..substitute # turn spaces into + (wot Google wants)
silent..opera.exe # where my opera is (BTW Opera is really kool!)
<C-R>z # recall contents of z

You can de-uglify your maps a little by putting your browser path into a VIM variable

let $opera="c:\progra~1\opera75\opera.exe"

As an extension to this for definitions and spellings, I included this as well as the mapping for a google search:

vmap ?w <Esc>:exec
 \ ':!/usr/bin/mozilla http://dictionary.reference.com/search?q="'
 \ . substitute(@*,'\W\+\\|\<\w\>'," ","g")
 \ . '"'<CR><CR>

I modified this a bit. Instead of entering Visual mode, selecting the word, and then launching the search, I wanted to just search directly from Normal mode. Additionally, I wanted to launch the default system browser, regardless of product or path. Note: This probably only works under Windows. Once I have a chance to test under Linux, I'll see if I can hack out something cross-platform.

map ?g "zyiw
 \ :exec ':silent ! start http://www.google.com/search?q=";'.@z.'"'<CR>

This takes the current word under the cursor, saves it to "z, then launches google silently in the default browser passing @z as the search parameter. Very clean, very simple.

map ?m "zyiw
 \ :exec ':silent ! start http://www.m-w.com/cgi-bin/dictionary?va=";'.@z.'"'<CR>

This does essentially the same, but looks up the word in the Merriam-Webster dictionary instead of Google.