Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to be able to select a text and have a command to open a google search in a browser.

Right now, I"m just copying and pasting. Are there any plugins that do this? I'm using Mac OSX.

share|improve this question
add comment

5 Answers

up vote 2 down vote accepted

I wrote this in about 5 minutes when I read your question, but it works on Linux anyway. Of course, change "firefox" to the command for your favorite browser, and change <F6> to whatever key or key combination you want to do this.

function! GoogleSearch()
     let searchterm = getreg("g")
     silent! exec "silent! !firefox \"http://google.com/search?q=" . searchterm . "\" &"
endfunction
vnoremap <F6> "gy<Esc>:call GoogleSearch()<CR>

This uses the g register, so don't use the g register for anything you don't want overwritten by calling this command. I haven't tested it much either.

It's possible that for a mac, the third line would need to be changed slightly:

silent! exec "silent! !firefox \"http://google.com/search?q=" . searchterm . "\""

but without a mac to test on, I don't know.

share|improve this answer
    
Missed Benoit's command, which is more or less the same thing. –  frabjous Nov 17 '10 at 21:20
    
You have the clarity, I have the conciseness (and maybe space escaping, but I forgot to escape &'s and #'s as well. –  Benoit Nov 17 '10 at 21:22
    
Good point. How about adding let searchterm = shellescape(searchterm)" between lines 2 and 3 of mine? –  frabjous Nov 17 '10 at 21:27
add comment

Why not use:

xnoremap <f1> "zy:!firefox "http://www.google.com/search?q=<c-r>=substitute(@z,' ','%20','g')<cr>"<return>gv

?

(as you are a mac user, you might replace firefox with another browser!)

Note that if your word contains & or # you might have to escape them as well as %26 and %23.

share|improve this answer
    
what does the zy part of the text do? –  tommy chheng Nov 18 '10 at 1:01
    
It is not zy but "zy. "z selects the z register for yanking your text, which is then available by either the variable @z or getreg('z'). –  Benoit Nov 18 '10 at 5:57
add comment

Expanding on frabjous's idea because the function as it was blacked my screen and printed "Created new window in existing browser session" in the middle (which is the terminal output from running google-chrome directly).

function! GoogleSearch()
  " Now with > /dev/null to prevent writing to the screen
  silent! exec "silent! !google-chrome \"http://google.com/search?q=" . @g . "\" > /dev/null"
  " Without this, vim goes blank
  redraw!
endfunction
vnoremap <F6> "gy<Esc>:call GoogleSearch()<CR>
" Added this to google the word under the cursor
nnoremap <leader>goo b"gye:call GoogleSearch()<CR>
share|improve this answer
add comment

My first spontaneous thought was Urxvt's Perl capabilities. If you used that terminal emulator, it would be easy to make any text googleable (that's how I would have solved it if I wanted the function since I already use Urxvt). This wouldn't work with GVim, however.

My second spontaneous thought is that Vim is open source, and thus it should be possible to customize GVim to do this. More or less work craving though, probably there are better solutions.

share|improve this answer
add comment

If you switch firefox in the above commands with open this will work with whatever your default browser is set to. As long as you are on OS X.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.