BrowseUrl

The ‘browse-url’ package included in Emacs, originally written by DenisHowe, allows you to open a URL in a browser.

It is described in the EmacsManual under Following URLs and in the extensive package documentation accesibled with C-h P.

Basic browsing

The main command to use is ‘browse-url’. The URL, which defaults to the URL around point, is read from the minibuffer.

‘browse-url-at-point’ will open the URL around the point directly.

Browsing the URL of the current buffer

‘browse-url-of-buffer’ opens the URL associated with the file in the current buffer.

Browsing the URL of the current region

‘browse-url-of-region’

Browsing from dired

‘browse-url-of-dired-file’ displays the file named on the current line.

Launching browser searches

See BrowseAproposURL

Making URLs clickable

The built-in GotoAddress highlights URLs and make them clickable

Selecting the browser to use

Setting a default browser

This is controlled by the variable ‘browse-url-browser-function’.

You can customize it by entering M-x customize-option browse-url-browser-function and following the prompts. If your browser is not among those listed, choose the entry “Specified by ‘Browse Url Generic Program’” and customize the option ‘browse-url-generic-program’. Using a generic browser offers no remote control – for every URL you click a new process will be started.

For help on getting a browser that doesn’t die when emacs is exited, see PersistentProcesses.

Varying handling by URL

See ‘browse-url-handlers’, included from Emacs 28, which allows varying how each URL is handled based on regexp-matching.

To control how file names are mapped to URLs, update the variable ‘browse-url-filename-alist’, for example:

    (add-to-list 'browse-url-filename-alist
                 '("/var/www/cgi/files/" . "http://my.website.com/cgi?"))

Using various browsers

Conkeror

There’s ‘browse-url-conkeror’ but it is marked obsolete in Emacs 28.

Add the following to .conkerorrc:

    url_remoting_fn = load_url_in_new_buffer;

Galeon

There’s ‘browse-url-galeon’ but it is marked obsolete as of Emacs 25.

IceCat

Customize

Links gui mode

Refers to the links web browser from http://links.twibright.com/ wikipedia.

Also known as “links -g”

Microsoft Edge

Add the following to .emacs

    (defun browse-url-edge (url &optional new-window)
      (shell-command
        (concat "start microsoft-edge:" " " url)))

and set the browser function

    (setq browse-url-browser-function 'browse-url-edge)

I don’t know why but to be able to use # anchors in URLs I had to define:

    (defun browse-url-edge (url &optional new-window)
      (shell-command
        (concat "start msedge" " " url)))

DieterWilhelm, 2021-08-30

Midori

Setting browse-url-generic-program to Midori just works fine :

    (setq browse-url-browser-function 'browse-url-generic
          browse-url-generic-program "midori")

Opera

From opera -h:

    Usage: opera [options] url
    -newwindow                     open url in new window
    -newpage                       open url in new page (tab)
    -backgroundpage                open url in background page (tab)

The netscapesque -remote openURL options are also supported.

Seamonkey

Customize the ‘browse-url-mozilla-*’ variables to use “seamonkey” in place of “mozilla”.

Termux (Android)

If you’re running Emacs on Termux you can use ‘am’ to pass URL to browser. ‘am’ can be installed by running ‘apt install termux-am’. You can then add this to your InitFile:

  (defun open-url-am (url &rest ignore)
    (interactive "sURL: ")
    (shell-command (concat "am start -a android.intent.action.VIEW -d '" url "'")
      (setq truncate-lines t))
  (setq browse-url-browser-function 'open-url-am)

If you haven’t set a default browser, the system service will prompted you with which browser you want to open the url with.

Note: You need to include protocol to open URL with ‘am’ (example: “https://google.com” will work, “google.com” won’t and will return error)

Xwidget Webkit

If you try the Emacs Xwidget branch you can do:

  (setq browse-url-browser-function 'xwidget-webkit-browse-url)

Then a webkit browser will show up inside an Emacs buffer.

Choosing among multiple browsers

Choose the browser each time

I want C-u to always open in emacs (e.g. emacs-w3m) and C-u C-u to always open in external (e.g. Safari).

Is there a way?

→ Have a look here: http://www.emacswiki.org/cgi-bin/wiki/JorgenSchaefersEmacsConfig I have this in my .emacs:

(require 'w3m-load)
(require 'w3m)
 (setq browse-url-browser-function 'browse-url-generic
       browse-url-generic-program "/usr/bin/conkeror")

(defun choose-browser (url &rest args)
  (interactive "sURL: ")
  (if (y-or-n-p "Use external browser? ")
      (browse-url-generic url)
    (w3m-browse-url url)))

(setq browse-url-browser-function 'choose-browser)
(global-set-key "\C-xm" 'browse-url-at-point)

hth Memnon

Choose the browser persistently

Alternatively, to use C-u to give a choice between opening in an internal or an external browser, with that choice persisting until reset:

(setq browse-url-browser-function 'eww-browse-url)

(defun choose-browser (url &rest args)
  "Ask the user to choose between an internal and external web browser, the choice then persisting."
  (interactive "sURL: ")
  (if (y-or-n-p "Use external browser? ")
      (progn
        (setq browse-url-browser-function 'browse-url-firefox)
        (browse-url-firefox url args))
    (progn
      (setq browse-url-browser-function 'eww-browse-url)
      (eww-browse-url url))))

(defun mg/browse-url (args)
  "Call choose-browser function if the universal argument is used."
  (interactive "P")
  (if (equal current-prefix-arg '(4))
      (setq browse-url-browser-function 'choose-browser)))

(advice-add 'browse-url :before #'mg/browse-url)

Choose the browser when using Org-mode's org-open-at-point (C-c C-o)

If you want to be able to easily choose between different browsers (with w3m as the default) you can advise ‘org-open-at-point’ with the following code (note, it uses choose-browser from above):

(defadvice org-open-at-point (around org-open-at-point-choose-browser activate)
  (let ((browse-url-browser-function
         (cond ((equal (ad-get-arg 0) '(4))
                'browse-url-generic)
               ((equal (ad-get-arg 0) '(16))
                'choose-browser)
               (t
                (lambda (url &optional new)
                  (w3m-browse-url url t)))
               )))
    ad-do-it))

Defaults to w3m: C-c C-o Defaults to external browser: C-u C-c C-o Choose w3m or external browser: C-u C-u C-c C-o

AaronCulich

Misc

Private browsing

Ask whether URL should open in a new window in private browsing mode (with Firefox). Unconditionally open some URLs in private browsing mode:

(defun my-browse-url-maybe-privately (url &optional new-window)
  "Ask whether URL should be browsed in a private browsing window."
  (interactive "sURL: ")
  (if (y-or-n-p "Private Browsing? ")
      (my-browse-url-firefox-privately url)
    (browse-url-default-browser url new-window)))

(defun my-browse-url-firefox-privately (url &optional new-window)
  "Make firefox open URL in private-browsing window."
  (interactive (browse-url-interactive-arg "URL: "))
  (let ((process-environment (browse-url-process-environment)))
    (apply 'start-process
           (concat "firefox " url)
           nil
           browse-url-firefox-program
           (list "-private-window" url))))

(setq browse-url-browser-function
      '(("^https?://t\\.co" . my-browse-url-firefox-privately)
        ("^https?://instagram\\.com" . my-browse-url-firefox-privately)
        ("." . my-browse-url-maybe-privately)))

Customizing macOS behavior with AppleScript

The built-in ‘browse-url-default-macosx-browser’ uses ‘open’, which uses the system’s default browser. To force using a new browser window, you can execute applescript from elisp (only in Emacs >23):

(defun browse-url-default-macosx-browser (url &optional new-window)
  (interactive (browse-url-interactive-arg "URL: "))
  (if (and new-window (>= emacs-major-version 23))
      (ns-do-applescript
       (format (concat "tell application \"Safari\" to make document with properties {URL:\"%s\"}\n"
		       "tell application \"Safari\" to activate") url))
    (start-process (concat "open " url) nil "open" url)))

On the other hand, if you want the browser to reuse an existing tab if it has one already showing the URL, the applescript is somewhat more complicated. Instructions are here.


CategoryDotEmacs CategoryHypermedia