Emacs: Lookup Google, Dictionary, Documentation

By Xah Lee. Date: . Last updated: .

Here's how to write a command to lookup the web of the word under cursor.

Here's the code:

(require 'browse-url) ; part of gnu emacs

(defun my-lookup-wikipedia ()
  "Look up the word under cursor in Wikipedia.
If there is a text selection (a phrase), use that.

This command switches to browser."
  (interactive)
  (let (word)
    (setq word
          (if (use-region-p)
              (buffer-substring-no-properties (region-beginning) (region-end))
            (current-word)))
    (setq word (replace-regexp-in-string " " "_" word))
    (browse-url (concat "http://en.wikipedia.org/wiki/" word))
    ;; (eww myUrl) ; emacs's own browser
    ))

You can setup a key such as F8 for easy access. See: Emacs: How to Define Keys.

You can change the URL in the above definition to other reference sites.

Here's the URL syntax for some popular language or reference sites. The “WORD” should be your search string.

Reference sites URL
SourceURL Format
Perl refhttp://perldoc.perl.org/search.html?q=WORD
PHP refhttp://us.php.net/WORD
AutoHotkey refhttp://www.autohotkey.com/docs/commands/WORD.htm
Wikipediahttp://en.wikipedia.org/wiki/WORD
Googlehttp://www.google.com/search?q=WORD
binghttp://www.bing.com/search?q=WORD
Wolfram|Alphahttp://www.wolframalpha.com/input/?i=WORD

Here's a list of online dictionary URLs: Online English Dictionary Tools.

Emacs: xah-lookup.el, Lookup the Web

For ready to use package, see emacs package: xah-lookup.el, Lookup the Web

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from my keyboard store.