HomeMathComputingArtsWordsLiteratureMusictwitter facebook webfeed

Emacs: Perl PHP Dictionary Wikipedia Google … Reference lookup

Advertise Here For Profit

Xah Lee, 2008-11, 2010-11, 2011-02-14

This page shows you how to setup emacs so that you can press a button and emacs will switch to your web browser and show the documentation for the word under cursor.

The documentation can be the word's english definition, programing language documentation, Google search, Wikipedia lookup, emacswiki, or any online reference site.

Perl, PHP, Ruby, Google, Wikipedia, …

The following is a example of looking up Wikipedia:

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

This command switches you to your browser."
 (interactive)
 (let (myword myurl)
   (setq myword
         (if (and transient-mark-mode mark-active)
             (buffer-substring-no-properties (region-beginning) (region-end))
           (thing-at-point 'symbol)))

  (setq myword (replace-regexp-in-string " " "_" myword))
  (setq myurl (concat "http://en.wikipedia.org/wiki/" myword))
  (browse-url myurl)
   ))

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

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 “XYZ” is your search word.

SourceURL Format
Perlhttp://perldoc.perl.org/search.html?q=XYZ
PHPhttp://us.php.net/XYZ
LSLhttp://wiki.secondlife.com/wiki/XYZ
AutoHotkeyhttp://www.autohotkey.com/docs/commands/XYZ.htm
Wikipediahttp://en.wikipedia.org/wiki/XYZ
Googlehttp://www.google.com/search?q=XYZ
binghttp://www.bing.com/search?q=XYZ
Wolfram|Alphahttp://www.wolframalpha.com/input/?i=XYZ

If you know a url syntax for Ruby, Python, lisp, or other langs, please add at the comment below. Thanks.

Looking Up Dictionary

Place the following in your emacs init file:

(defun lookup-word-definition ()
  "Look up the current word's definition in a browser.
If a region is active (a phrase), lookup that phrase."
 (interactive)
 (let (myword myurl)
   (setq myword
         (if (and transient-mark-mode mark-active)
             (buffer-substring-no-properties (region-beginning) (region-end))
           (thing-at-point 'symbol)))

  (setq myword (replace-regexp-in-string " " "%20" myword))
  (setq myurl (concat "http://www.answers.com/main/ntquery?s=" myword))

  (browse-url myurl)
  ;; (w3m-browse-url myurl) ;; if you want to browse using w3m
   ))

(global-set-key (kbd "<f6>") 'lookup-word-definition)

With the above, pressing F6 will launch your browser and lookup definition of the word under cursor. Or you can define your own key.

You can change the url to a different online dictionary reference website.

Here are some popular dictionary sites and their url search syntax, using sample word “curlicue”. (AHD = American Heritage Dictionary)

When choosing a site, you want one that's fast loading, not many ads, not cluttered, has voice recording, but most of all, contains the dictionary you like.

If you prefer to lookup definitions within emacs, see: Emacs Dictionary Lookup.

Launching a Specific Browser

See: Emacs Lisp: browse-url Function to View URL in a Web Browser.

blog comments powered by Disqus