Google Autocomplete Fun
Vim 8 + unimpaired.vim, 93 89 85 70 73 71 bytes
- -4 bytes thanks to tsh
- -2 bytes thanks to Ian Emnace
- -2 bytes thanks to FatalMerlin
- -1 byte thanks to tsh/ckjbgames
:s/ /+/g
D:e http://google.us/complete/search?client=gma&q="
d3f";D]yy
As a bonus, the last bytes look like they're winking at you ;D
Since this contains non-printing characters, the explanation contains substitutions (and I've replaced the pre-querystring part of the url with [url]
, for brevity):
:s/ /+/g<CR>D:e [url]?client=gma&q=<C-R>"<CR>d3f";D]yy
:s/ /+/g<CR> " Replace spaces with +
D " Delete and yank result
:e " Type :e ('edit' command) and a space
[url]?client=gma&q= " Type the url, except the query
<C-R>" " Paste from the default register
<CR> " Execute the command, which opens the response
" in a new buffer
d3f" " Delete through the first three quotation marks
" This deletes everything before the suggestion
; " Repeat motion; this jumps to the next \"
D " Delete everything from there, leaving only the suggestion
]yy " unimpaired.vim C string unescape to handle escaped '
As far as running goes, it works fine if you save it to a file named script
and run with vim -s script input.txt
on macOS, at least. It doesn't work if you add -u NONE
, but it works fine if my .vimrc
is empty. I assume it is using something from the system .vimrc
to make the URL stuff work. This means, however, that it doesn't work in V, so no TIO link.
Some more test cases:
'what' => 'whataburger'
'what ' => 'what time is it' (Seriously? People Google that?)
What I really need is a way to open a URL with spaces in it. Replacing them with +
first is just too many bytes!
Zsh + coreutils + w3m, 88 71 68 bytes
echo `w3m "google.com/complete/search?client=gma&q=$*"|cut -d\" -f4`
Switching from Bash to Zsh saved 3 bytes.
Thanks to @FatalMerlin for the shorter URL, saving 17 bytes!
Sample run
$ zsh complete.sh how to
how to make slime
$ zsh complete.sh dont you
don't you forget about me
$ zsh complete.sh don\'t you
don't you worry child
Python + requests 121 117 103 bytes
from requests import*
lambda s:get("http://google.com/complete/search?client=gma&q="+s).json()[1][0][0]