Emacs w3m: Open pages in external browsers

Sometimes w3m is not enough. To make it easier to open the current page in a browser such as Mozilla Firefox, add the following to your ~/.emacs:

(defun wicked/w3m-open-current-page-in-firefox ()
  "Open the current URL in Mozilla Firefox."
  (browse-url-firefox w3m-current-url)) ;; (1)

(defun wicked/w3m-open-link-or-image-in-firefox ()
  "Open the current link or image in Firefox."
  (browse-url-firefox (or (w3m-anchor) ;; (2)
                          (w3m-image)))) ;; (3)

This defines a function that uses the current URL being browsed(1) and another function that takes the URL of the link at point(2). If no link is found, it takes the URL of the image at point(3).

You can use other browse-url functions instead of browse-url-firefox. For example, replacing browse-url-firefox with browse-url-kde will open the page, link, or image in Konqueror, KDE’s web browser.

I like binding f to the function that opens the current URL in Mozilla Firefox and F to the function that opens the current link or image in Mozilla Firefox. To do the same, add the following to your ~/.emacs:

(eval-after-load 'w3m
    (define-key w3m-mode-map "f" 'wicked/w3m-open-current-page-in-firefox)
    (define-key w3m-mode-map "F" 'wicked/w3m-open-link-or-image-in-firefox)))

This is part of the book that I’m writing about Emacs, which will be published by No Starch Press if I manage to get it together in time.

  • Just in case you didn’t know: emacs-w3m (at least the unstable version) already comes with such a function: it’s called


    , and one can actually associate different ‘browsers’ to different mime-types, by configuring



  • Just in case you didn’t know: emacs-w3m already provides such a function, called w3m-view-url-with-external-browser, which actually uses different ‘browsers’ for different mime types (customizable via w3m-content-type-alist) and opens either the current page or the URL (or link) and point.

    (I hope you finish your book in time: i’ve been eagerly wanting to buy a copy since you first announced it! :))

  • This seems very similar to functionality provided by w3m-view-url-with-external-browser, which is part of my w3m-emacs package. It’s bound to the “M” key (I remember it as “Mozilla”). I use a CVS-checkout of w3m-emacs, so maybe this isn’t a part of the “regular” w3m-emacs? Then again, I ask you — is there any other way to run w3m-emacs? I’m addicted to my CVS-checkouts.

  • … AARGH! <laugh> And I _already_ read through the w3m source code, so I must’ve briefly skimmed that function. Mrph. This is what comes from trying to write about code I’m not intimately familiar with, haven’t spent the last five years of my life tweaking and tweaking and tweaking…

    Maybe I should just cancel the plans for the big Emacs book and focus on a smaller one on running your life with Emacs… ;)

    • Paul Bernard

      Maybe I should just cancel the plans for the big Emacs book and focus on a smaller one on running your life with Emacs

      Maybe you need just a bit more focus. I’ve not done my homework, I don’t actually know what the intended purpose is for your book. I’m guessing it’s to introduce emacs to people who wouldn’t have seen it before and push people who are aware of it closer to becoming experts or at least users. It isn’t likely aimed at me. I don’t want to have your focus for it aimed at me. (I’m not a beginner.)

      I have read your emacs posts with enthusiasm, but I’m not the target audience. I still enjoy them though. I have eight of your suggested key bindings because they were just so much better than the defaults I was using.

      Personally I find books that introduce me to methods of achieving some goal to be so much more valuable than books that show me what keys to press. Even when such a book is aimed at a beginner I frequently find a different way of looking at a problem that I might not have otherwise considered.

      The postings you’ve made that have had the most impact on me are your “daily usage” comparisons for org-mode and planner.

  • I have this which checks if the link is an encoded/rednered w3m link or a text url.

    (kbd “”)
    ((setq url (w3m-url-valid (w3m-anchor)))
    (browse-url-firefox url))
    (t (command-execute ‘browse-url-firefox)))))

    Hope that helps too.

    • On the subject of external browsers, I feel the more “rounded” approach is to remove the firefox specifics


      (define-key w3m-mode-map “f” (lambda()(interactive)( w3m-external-view-current-url)))

      In this case I have the “free” (cough) version of Firefox, Icicles, set up as my default Gnome web browser.

  • deeptext

    This might be the place to ask this question.
    I want to open a javascript file (or similar) that resides on the Internet, from within Emacs.
    How do I do that? I don’t want to view a page (like a browser), I want to view the source.
    Yes, I can cut and paste and edit html lines and use the browser to “view source”, but I want Emacs to do ALL the work. Thanks.

    • Well, to view the source you need the page. Are you using w3m?

      Once you have the page then consider possibly using nxhtml.

      Andy Stewart, the author or contributor of W3m is very open to extension ideas (he recently wrote me a routine to convert a w3m buffer into org-style for cutting and pasting from w3m buffers into an org-mode file which maintains links)

  • Yet one more way to do it is to attach external browser to S-mouse-2

    (eval-after-load “w3m”
    (define-key w3m-link-map [S-mouse-2]
    (lambda (event)
    (interactive “e”)
    (mouse-set-point event)

  • Oh, nifty! Thanks for showing me even better ways to do things. =)

  • I had to modify the above with a little patch:

    -(define-key w3m-link-map [S-mouse-2]
    +'(define-key w3m-link-map [S-mouse-2]

  • dankles

    I’m getting an error in evaluating the “(eval-after-load ‘w3m ” … “define-key” code:

    Debugger entered–Lisp error: (void-variable w3m-mode-map)
    (define-key w3m-mode-map “f” (quote my-w3m-open-current-page-in-firefox))
    (progn (define-key w3m-mode-map “f” (quote my-w3m-open-current-page-in-firefox)) (define-key w3m-mode-map “F” (quote my-w3m-open-link-or-image-in-firefox)))
    (eval-after-load (quote w3m) (progn (define-key w3m-mode-map “f” …) (define-key w3m-mode-map “F” …)))
    eval((eval-after-load (quote w3m) (progn (define-key w3m-mode-map “f” …) (define-key w3m-mode-map “F” …))))
    call-interactively(eval-last-sexp nil nil)

    Though the two functions to open the current page and link in a new page work fine if i run them manual via M-x