6078 comments
2357 subscribers
6202 on Twitter
Subscribe! Feed reader E-mail

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."
  (interactive)
  (browse-url-firefox w3m-current-url)) ;; (1)

(defun wicked/w3m-open-link-or-image-in-firefox ()
  "Open the current link or image in Firefox."
  (interactive)
  (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
  (progn 
    (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.

Short URL: http://sachachua.com/blog/p/5138
  • http://emacs.wordpress.com Jose A Ortega

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

    w3m-view-url-with-external-browser

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

    w3m-content-type-alist

    .

  • http://emacs.wordpress.com Jose Antonio Ortega Ruiz

    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! :))

  • http://walker.pendleton.name/ Walker Pendleton

    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.

  • http://sachachua.com Sacha Chua

    … 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.

  • http://richardriley.net Richard Riley

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

    (global-set-key
    (kbd “”)
    (lambda()
    (interactive)
    (cond
    ((setq url (w3m-url-valid (w3m-anchor)))
    (browse-url-firefox url))
    (t (command-execute ‘browse-url-firefox)))))

    Hope that helps too.

    • http://richardriley.net Richard Riley

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

      e.g

      (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.

    • http://richardriley.net Richard Riley

      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)

  • http://steelman.jogger.pl steelman

    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)
    (w3m-external-view-this-url))))

  • http://sachachua.com Sacha Chua

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

  • http://steelman.jogger.pl steelman

    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” …))))
    eval-last-sexp-1(nil)
    eval-last-sexp(nil)
    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

On This Day...

  • 2011: Deliberate practice, typing faster, and Emacs — I type at about 90-95 wpm. I wonder: Would it be worth getting even faster? How would I go about [...]
  • 2010: Week ending September 12, 2010 — SCHEDULED: 2010-09-13 Mon 08:00 From last week’s plans Work [X] Classroom to Client: Create community and structure online resources [X] Connections Toolkit: [...]
  • 2008: First impressions in an e-mail world — I really wish I could’ve spent more time in Washington–not only to spend a month or two in the Smithsonian [...]
  • 2006: What makes a good life? — Ian Garmaise introduced Lawrence Miller, who was a wonderful addition the conversation. Talk turned to wisdom. I asked him, “How do you [...]
  • 2005: ACM training again — I saw a poster for the ACM programming tryouts. I’m really rusty, but it would be good for me to find [...]
  • 2005: Presentation insights from science competitions — On community.lifehack.org: presentation tips from a science fair competitor. Makes me wish I’d gotten into the science fair circuit! I’ve given [...]
  • 2003: Nifty Assignments — http://nifty.stanford.edu/ I should develop things like this, and maybe see about using some of these assignments in my class.
  • 2003: Naive tools for studying compilation history — Full text available at http://www.cs.kent.ac.uk/pubs/2003/1588/index.html First paragraph: We are interested in assessing the impact of pedagogic programming environments in the teaching of programming [...]
  • 2003: CS education dissertations — At the University of Texas at Austin, Roger Louis Priebe earned a doctorate in Science Education in 1997 with a [...]
  • 2003: Blogging as an educator — From http://www.cs-ed.org/blogs/mjadud/archives/000137.html#137: I haven’t ever blogged as an educator, I must admit. This is, I think, a failing in my blogging [...]
  • 2003: CS education weblog — http://www.cs-ed.org/blogs/mjadud/ proposed an interesting question - how do you compile? I notice that students tend to write lots of (buggy) code and [...]
  • 2003: mount –bind — Whoa, funky. mount —bind olddir newdir allows us to make contents available elsewhere.
  • 2002: Fri Sep 13:27 2002 — http://www.imc.org/pdi
  • 2002: Fri Sep 13:45 2002 — Research and Development – CompSAT Paolo Venegas We learn so that we may learn more in this committee. I believe that we should [...]
  • 2002: Fri Sep 13:27 2002 — http://www-106.ibm.com/developerworks/java/library/j-xp0910.html?loc=j
  • 2002: tasks in a database, guids, whatever Fri Sep 13:37 2002 — If I store all tasks in a database, then I need to be able to quickly refer to it. Actually, [...]
  • 2002: Fri Sep 13:42 2002
  • 2002: Fri Sep 13:42 2002 — make remember use emacspeak. learn enough to become a major contributor ito Emacspeak if i store it in a database, then I [...]
  • 2002: Fri Sep 13:59 2002 — now we’re talking about relationships
  • 2002: Fri Sep 13:24 2002 — Maybe I should store the tasks in a MySQL database. MySQL allows us to do a simple relevance query. Hmmm. [...]
  • 2002: Fri Sep 13:21 2002 — Not fully formed Existentialism: no “nature” but we have history. The moment we are born, we cannot say that that is [...]
  • 2002: Fri Sep 13:58 2002 — decode-time is a built-in function. (decode-time &optional SPECIFIED-TIME)
  • 2002: Fri Sep 13:48 2002 — get a wish list of hardware – charmit thinkgeek com minimum thin client, using the xscale chip automatic media transfer automatic networ [...]

Get the highlights as a PDF!

Stories from my Twenties: Highlights from a Decade of Blogging