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.
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.
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
.
zy
but "zy
. "z
selects the z register for yanking your text, which is then available by either the variable @z
or getreg('z')
.
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>
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.
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.
Thanks for all the help everybody. I put this together based on everything here (and fixed a bunch of bugs). It works in nvim, but I don't see why it wouldn't work in regular vim.
I use these keymaps:
" google the word under the cursor
nmap <silent> <leader>gg "gyiw:call GoogleSearch()<CR>
" google the visual selection
vmap <silent> <leader>gg "gy:call GoogleSearch()<CR>
with this code:
" http://www.danielbigham.ca/cgi-bin/document.pl?mode=Display&DocumentID=1053
" URL encode a string. ie. Percent-encode characters as necessary.
function! UrlEncode(string)
let result = ""
let characters = split(a:string, '.\zs')
for character in characters
if character == " "
let result = result . "+"
elseif CharacterRequiresUrlEncoding(character)
let i = 0
while i < strlen(character)
let byte = strpart(character, i, 1)
let decimal = char2nr(byte)
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! CharacterRequiresUrlEncoding(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
function! GoogleSearch()
let searchterm = getreg("g")
let searchterm = substitute(searchterm, "\n", " ", "g")
let searchterm = UrlEncode(searchterm)
let searchterm = shellescape(searchterm, 1)
" echo "searchterm: " . searchterm
silent! exec "!open 'https://google.com/search?q=" . searchterm . "'"
" exec "!echo 'https://google.com/search?q=" . searchterm . "'"
" redraw!
endfunction