4

I would like to be able to search google from within any vim file. A nice command might be :goo while in normal mode.

Then I type what I want to search and bam it opens my default browser with the search.

How would I do this?

  • 1
    I think you can have a look at github.com/szw/vim-g – nobe4 Jul 28 '16 at 14:47
  • 2
    I dunno, type then open browser, or open browser then type. Seems a bit pointless me. Plus if you type in the browser you get search completion. What's the use case? – Antony Jul 28 '16 at 16:00
  • The use case is not having to switch focus to browser, also being able to paste confusing code from any of the registers into the search. Sometimes system register is a little choppy and it would be and extra step to copy to that register. Plus come on -->"Googling from vim"<-- – Jason Basanese Jul 28 '16 at 18:16
  • 1
    If you want to google from your text editor you should have a look at emacs ;-) More seriously, I understand that it is fun to see but IMO that's not an efficient way to use Vim. – statox Jul 29 '16 at 8:05
  • You will switch focus, at least mentally, so using the "right" tool for what you want is a plus. I feel like "Googling from vim" is like "gdb from vim", it can be done, but it's not the purpose of each tool (aka vim is only a text editor, nothing more). – nobe4 Jul 29 '16 at 8:06
10

You have a couple of options here:

Using a plugin:

Or, if you prefer a lightweight solution, you can try the following:

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

(source)

Using the vim-shell plugin you can rewrite this to:

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

You can also have a look at those links:

And I highly recommend this video by Drew Niel.

  • The vim-shell plugin could be used to make your function to be OS/browser agnostic. – mMontu Jul 28 '16 at 17:00
  • "silent! !firefox \"http://google.com/search?q=" . searchterm . "\" &" should be "silent! !firefox \"http://google.com/search\\?q\\=" . searchterm . "\" &" – Vold Notz Aug 20 '18 at 3:15
2

As others have pointed out, Searching from Vim is not something one would want to do every time, but I do understand that there are some situations where you just want to search for a particular word in Vim. In those situations this plugin might be useful.

Mind you, this is something I wrote only for the purpose of searching for the word under the cursor, nothing more.

Vim-Scour

The Readme file has info about how to use this. And even if you don't want to use the plugin itself, look at the plugin file. Its really small and you will get an idea about how you can do system calls.

  • 1
    It's nice to see you are starting to write Vim plugins. I suggest you have a look at :h :execute, which let you insert variable name in your commands. This could simplify largely your scour#Search function. – nobe4 Jul 29 '16 at 12:07
  • 1
    I agree with @nobe4: your function could be one line actually: execute "silent !" . g:scour_browser . " www." . g:scour_search_engine . ".com/search?q=<cword>". execute will execute the string provided as argument, in the string you simply concatenate the browser and search engine variables with . and you're good to go :-) – statox Jul 29 '16 at 12:13
  • 1
    @statox Thanks for the suggestion. It was a while ago I wrote that. I was just learning Vim scripting then. Though I am still a beginner in scripting I haven't bothered to update the plugin. – Durga Swaroop Jul 29 '16 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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