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= |
+ | |created=2005 |
|complexity=basic |
|complexity=basic |
||
− | |author= |
+ | |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. |
||
⚫ | |||
− | This is especially useful to lookup spellings of proper nouns, phrases, |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | <pre> |
||
− | function prototypes and man pages on the web, while working in vim. |
||
⚫ | |||
⚫ | |||
⚫ | |||
+ | </pre> |
||
⚫ | |||
+ | 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. |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
− | Notes and explanation: |
||
− | |||
− | |||
− | |||
− | The vmap takes the visually selected 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. |
||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
− | to query for <word under cursor>. |
||
− | |||
− | |||
− | |||
− | - MA |
||
− | |||
− | http://www.cs.albany.edu/~mosh |
||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | 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. |
||
⚫ | |||
− | |||
− | 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 |
|
− | 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 & |
+ | if &shell =~ "cmd" |
− | vmap ?? |
+ | 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( |
+ | \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g") |
− | \ . "\"" |
+ | \ . "\""<CR><CR> |
− | else |
+ | else |
− | :vmap ?? |
+ | :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( |
+ | \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g") |
− | \ . "\\\" " |
+ | \ . "\\\" "<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= |
+ | 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 |
+ | 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 ?? |
+ | 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( |
+ | \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g") |
− | \ . "\"" |
+ | \ . "\""<CR><CR> |
+ | </pre> |
||
− | MA |
||
− | , May 24, 2005 17:15 |
||
---- |
---- |
||
− | + | You can use single-quote to lessen the backslash: |
|
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | <pre> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | </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 |
+ | vmap ?? y<Esc>:silent exec |
− | \ ":!/usr/bin/opera http://www.google.com/search?q='"; |
+ | \ ":!/usr/bin/opera http://www.google.com/search?q='"; |
− | \ . substitute( |
+ | \ . substitute(@","\\W\\+\\\\|\\<\\w\\>",'\\%20',"g") |
− | \ . "' & |
+ | \ . "' &"<CR><CR> |
− | else |
+ | else |
− | vmap ?? |
+ | 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( |
+ | \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g") |
− | \ . "\"" |
+ | \ . "\""<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 |
||
---- |
---- |
||
⚫ | |||
− | Hi |
||
⚫ | |||
+ | <pre> |
||
− | vmap ,g "zy:let |
+ | 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= |
+ | \ [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 |
+ | "zy # yank visual area to register z |
− | let..substitute |
+ | let..substitute # turn spaces into + (wot Google wants) |
− | silent..opera.exe |
+ | silent..opera.exe # where my opera is (BTW Opera is really kool!) |
− | + | <C-R>z # recall contents of z |
|
+ | </pre> |
||
− | zzapper |
||
− | , May 26, 2005 10:39 |
||
---- |
---- |
||
⚫ | |||
− | Hi |
||
+ | |||
⚫ | |||
+ | <pre> |
||
let $opera="c:\progra~1\opera75\opera.exe" |
let $opera="c:\progra~1\opera75\opera.exe" |
||
+ | </pre> |
||
− | zzapper |
||
− | , May 26, 2005 10:47 |
||
---- |
---- |
||
⚫ | |||
− | Thanks for this, a very useful tip. |
||
⚫ | |||
+ | <pre> |
||
− | vmap ?w |
+ | vmap ?w <Esc>:exec |
− | \ ':!/usr/bin/mozilla http://dictionary.reference.com/search?q=" |
+ | \ ':!/usr/bin/mozilla http://dictionary.reference.com/search?q="' |
− | \ . substitute( |
+ | \ . substitute(@*,'\W\+\\|\<\w\>'," ","g") |
− | \ . '"' |
+ | \ . '"'<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=";'. |
+ | \ :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 |
+ | 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=";'. |
+ | \ :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
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.