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?
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?
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.
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 . "\" &"
– WW00WW
Aug 20 '18 at 03:15
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.
: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
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
you can do it like this
x-www-browser https://google.com
then you can do a search or if you want to open a link it would be like this
x-www-broswer https://www.youtube.com/watch?v=dQw4w9WgXcQ
:! x-ww-browser ...
– D. Ben Knoble
Oct 24 '20 at 17:30
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.
Providing a snippet to resolve url encoding.
" ref: https://vi.stackexchange.com/a/1958/49235
" Call shell command silently then Ctrl+L (or :redraw!) to refresh the screen when back to Vim.
command! -nargs=1 Silent execute ':silent !'.<q-args> | execute ':redraw!'
" google search
nnoremap <silent> gG :Silent open "http://google.com/search?q="<c-r>=<sid>encode_words(expand("<cword>"))<cr><cr>
vnoremap <silent> gG y:Silent open "http://google.com/search?q="<c-r>=<sid>encode_words(@")<cr><cr>
" https://vi.stackexchange.com/questions/42717/how-do-i-turn-a-string-into-url-encoded-string
" URL encode a string. ie. Percent-encode characters as necessary.
function! s:encode_words(text) abort
" Replace \n, preserve \
let words = substitute(a:text, '\n', ' ', 'g')
let words = substitute(words, '\', '\', 'g')
let result = ""
let characters = split(words, '.\zs')
for character in characters
if character == " "
let result = result . "+"
elseif s:needs_encode(character)
let i = 0
while i < strlen(character)
let byte = strpart(character, i, 1)
let decimal = char2nr(byte)
" Avoiding command expansion
let result = result . "\\%" . printf("%02x", decimal)
let i += 1
endwhile
else
let result = result . character
endif
endfor
return result
endfunction
" Returns 1 if the given character should be percent-encoded in a URL encoded
" string.
function! s:needs_encode(character)
let ascii_code = char2nr(a:character)
if ascii_code >= 48 && ascii_code <= 57
return 0
elseif ascii_code >= 65 && ascii_code <= 90
return 0
elseif ascii_code >= 97 && ascii_code <= 122
return 0
elseif a:character == "-" || a:character == "_" || a:character == "." || a:character == "~"
return 0
endif
return 1
endfunction