Elisp: View URL in Web Browser: browse-url

By Xah Lee. Date: . Last updated: .

In emacs, there's a function browse-url. It sends a URL to a web browser.

Call it to view the URL under cursor in browser.

Use it in emacs lisp code like this:

(require 'browse-url)
(browse-url "http://google.com/")

browse-url is defined in browse-url.el.

browse-url-of-buffer → sends the current (local HTML) file in browser.

For example use, see: Emacs: Lookup Google, Dictionary, Documentation.

[see Emacs: Set Default Browser]

Launching Browser by Shell Command

There's also browse-url-firefox. But i recall, under Microsoft Windows, there's some problems.

You can also launch a browser by making a shell call. For example:

(defun my-browse-url-of-buffer-with-firefox ()
  "Same as `browse-url-of-buffer' but using Firefox.
You need Firefox's path in the path environment variable within emacs.
 (setenv \"PATH\" (concat \"C:/Program Files (x86)/Mozilla Firefox/\" \";\" (getenv \"PATH\") ) )
On Mac OS X, you don't need to. This command makes this shell call:
 「open -a Firefox.app http://example.com/」"
   ((string-equal system-type "windows-nt") ; Windows
    (shell-command (concat "firefox file://" buffer-file-name)))
   ((string-equal system-type "gnu/linux")
    (shell-command (concat "firefox file://" buffer-file-name)))
   ((string-equal system-type "darwin") ; Mac
    (shell-command (concat "open -a Firefox.app file://" buffer-file-name)))))

You have to make sure that the path of firefox. For example: C:/Program Files (x86)/Mozilla Firefox/ is in emacs's (getenv PATH) result. Here's a example of how to add it:

;; set env var named “path” by appending a new path
(setenv "PATH"
         "C:/Program Files (x86)/Mozilla Firefox/"
         (getenv "PATH")))

You might want to give it a key. [see Emacs: How to Define Keys]

[see Emacs in Microsoft Windows FAQ]

Browser in Emacs

Emacs: Eww Web Browser

Put $5 at https://www.patreon.com/xahlee , or goto paypal.com and pay to Xah@XahLee.org . Message me on XahLee discord, say so, ask me questions.
Or Buy Xah Emacs Tutorial