Vim Tips Wiki
Register
Advertisement
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.


Advertisement