Which is better - Emacs or Vim? (Google fight)

Ruby, 203 180 bytes

->*s{s.map{|x|[(open("http://www.google.com/search?nord=1&q=#{URI.escape x}&nfpr=1").read.match(/s">(About )?([\d,]+) result/)||[?0])[-1].split(?,).join.to_i,x]}.max[1]}

+11 bytes for the -ropen-uri flag (plus space).

Input is taken as two arguments.

URL explanation:

  • ?nord=1: prevent auto-redirect from HTTP to HTTPS
  • &q=#{URI.escape x}: escape the query so "s work
  • &nfpr=1: no autocorrect

The whole map turns ['emacs','vim'] into [[2150000, 'emacs'], [14900000, 'vim']]. (You can see this by changing the .max[1] at the end to .inspect.) Then, the max is taken, which will grab the one with the most results, and [1] is used to get the search term.


Of the text editors mentioned in the problem, only one of them can solve this on its own...

Emacs: 137 keystrokes

<C-o><C-u><C-k>(if(<<C-y><C-y><M-<><C-n><F3><C-a>http://google.com/search?nfpr=1&q=<C-S-e><M-x>r-st<RET> <RET>+<RET><C-a><C-k><M-x>b-em<RET><C-y><RET><C-x><C-q><C-s><RET>ts"><RET><M-z><<C-x>0<C-y>0 r<C-a><C-M-s><RET>[0-9]<RET><C-b><M-z> <C-a><C-y><C-k><C-S-a><M-x><up><up><RET>,<RET><RET><F4><C-n><F4><C-e>)(next-line)())<C-x><C-e><C-n><C-a><C-k><C-x>h<DEL><C-y>

It expects the first search terms to be on the first line and the second search terms to be on the second line, with the cursor at the beginning of the buffer.

For those unfamiliar with Emacs,

  • C-s means Ctrl-S.
  • M-z means Alt-Z (Alt is likely your meta key)
  • M-< means Alt-< or Alt-Shift-,
  • C-S-e means Ctrl-Shift-e

For those familiar with Emacs,

  • Start emacs with emacs -q. That doesn't load your .emacs file, so any fancy packages won't intefere with this.

Explanation

  1. Write the beginning of the elisp statment shell

    • <C-o><C-u><C-k> Saves the arguments with a newline
    • (if(<<C-y><C-y> Write the start of the if statement and places 2 copies of the arguments
    • <M-<><C-n> Move to the second line
  2. Define the HTTP macro. This converts the argument to a Google search URL and then returns the number of search results.

    • <F3> Start defining the keyboard macro
    • <C-a> Move to the start of the search term.
    • http://google.com/search?nfpr=1&q= Prepend the search URL
    • <C-S-e><M-x>r-st<RET> <RET>+<RET> Replace all the spaces with + signs.
    • <C-a><C-k><M-x>b-em<RET><C-y><RET> Emacs retrieve the raw HTML (b-em is short for browse-url-emacs)
    • <C-x><C-q> Make the file writable (required so the macro doesn't error ou t)
    • <C-s><RET>ts"><RET> Move to the number of results div (need to do a regular search because emacs macros rely on isearch to work)
    • <M-z><Cut the text of of the diff (this is why the was required)
    • <C-x>0 Switch back to the original buffer
    • <C-y>0 r;; Put the results back on the line ('0 r' handles no results)
    • <C-a><C-M-s><RET>[0-9]<RET><C-b><M-z> <C-a><C-y><C-k>;; Extract the number from the string.
    • <C-S-a><M-x><up><up><RET>,<RET><RET> Strips commas out of the number
    • <F4> Finish the keyboard macro
  3. Move down and execute the keyboard macro on the next line.

    • <C-n> Goes to the next line
    • <F4> Repeat the macro once.
  4. Finish up the elisp statement and execute it

    • <C-e>)(next-line)()) Finish up the elisp statement
    • <C-x><C-e> Evaluate the elisp command
    • <C-n><C-a><C-k> Kill the winning argument
    • <C-x>h<DEL> Delete everything else
    • <C-y> Paste the winning argument

Running It Yourself

Luckily you don't have to type all those keystrokes in perfectly! The meat and potatoes is all in a macro that you can just copy and paste. The macro can be copy and pasted into emacs!

1.Edit the keyboard macro <C-x><C-k><C-e>
2.Paste this into the whole buffer (paste should be <C-y>)

;; Keyboard Macro Editor.  Press C-c C-c to finish; press C-x k RET to cancel.
;; Original keys: C-a http://google.com/search?nfpr= 1&q= S-C-e M-x r-st RET SPC RET + RET C-a C-k M-x b-em RET C-y RET C-x C-q C-s RET ts"> RET M-z < C-x 0 C-y 0 SPC r C-a M-C-s [0-9] 5*DEL RET [0-9] RET C-b M-z SPC C-a C-y C-k S-C-a M-x 2*<up> RET , 2*RET
Command: last-kbd-macro
Key: none
Macro:
C-a         ;; move-beginning-of-line
http://google.com/search?nfpr=  ;; self-insert-command * 30
1&q=            ;; self-insert-command * 4
S-C-e
M-x         ;; execute-extended-command
r-st            ;; self-insert-command * 4
RET         ;; newline
SPC         ;; self-insert-command
RET         ;; newline
+           ;; self-insert-command
RET         ;; newline
C-a         ;; move-beginning-of-line
C-k         ;; kill-line
M-x         ;; execute-extended-command
b-em            ;; self-insert-command * 4
RET         ;; newline
C-y         ;; yank
RET         ;; newline
C-x C-q         ;; read-only-mode
C-s         ;; isearch-forward
RET         ;; newline
ts">            ;; self-insert-command * 4
RET         ;; newline
M-z         ;; zap-to-char
<           ;; self-insert-command
C-x 0           ;; delete-window
C-y         ;; yank
0           ;; self-insert-command
SPC         ;; self-insert-command
r           ;; self-insert-command
C-a         ;; move-beginning-of-line
M-C-s           ;; isearch-forward-regexp
RET         ;; newline
[0-9]           ;; self-insert-command * 5
RET         ;; newline
C-b         ;; backward-char
M-z         ;; zap-to-char
SPC         ;; self-insert-command
C-a         ;; move-beginning-of-line
C-y         ;; yank
C-k         ;; kill-line
S-C-a
M-x         ;; execute-extended-command
2*<up>          ;; previous-line
RET         ;; newline
,           ;; self-insert-command
2*RET           ;; newline
  1. Type <C-c><C-c> to save the macro.
  2. If following the explanation steps, replace step 2 with <F4> to run the macro (or just run it on its own to try)

Caveats

  1. You can't run the macro on the same search twice without killing the buffer the HTML gets loaded into. Kill the buffers
    • <C-x><k> search<TAB>
    • Choose one of the buffers in this list to kill.
    • Repeat for all the buffers beginning with "search"
  2. If you run the macro too much, Google will think you are a robot and block access for atime
    • If the macro returns something like <input type="submit" name="submit" value="Submit"...>, then this likely occurred.
    • You can confirm it by looking at the raw HTML (<C-x><C-b> and choose the buffer with the search term in it).
    • If you see stuff about robots and captcha, Google's blocking you. It's not my fault.

Factor, 305 201 196 200 201 188 184 182 179 169 178 171 165 199 171 170 165 163 bytes

A language nearly as verbose as Java, beating Ruby... and Powershell! :D

Better regex, now. Thanks to @fede s. for 5 bytes off!

[ dup [ url-encode "google.com/search?nfpr=1&q="prepend http-get R/ s">About [\d,]+/ first-match " "split second 10 >base ] map zip [ last ] sort-with last first ]

Or 159 157 if the output can be like { "vim" 9782948234 }:

[ dup [ url-encode "google.com/search?nfpr=1&q="prepend http-get R/ s">About [\d,]+/ first-match " "split second 10 >base ] map zip [ last ] sort-with last ]

On the other hand, if we want to be unkillable, for 199 196 193 bytes:

[ dup [ url-encode "google.com/search?nfpr=1&q="prepend scrape-html nip dup "resultStats"find-by-id-between second text>> " "split second string>number ] map zip [ last ] sort-with last first ]

Unkillable because it parses HTML, so way more robust than the answers using regex.