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?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
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>
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.
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.
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.
nmap gy :silent execute "!google-chrome http://google.com/search?q=" . shellescape("<cWORD>") . " &"<CR> vmap gy <Esc>:silent execute "!google-chrome http://google.com/search?q=" . shellescape("<C-r>*") . " &"<CR>
The first command allows you press
gy to search for a word under your cursor. The second command allows you to visually search for a term, also using
gy but in visual mode. Replace
google-chrome with your browser command or use
vim-shell as in the other answer.