6199 comments
2357 subscribers
Follow me on Twitter (@sachac)
Subscribe! Feed reader E-mail

Emacs and W3M: Toggling between work and the Web

Here’s a handy shortcut that toggles between the W3M web browser and other buffers you’re working on. I use it to quickly switch between code and documentation (or your favorite timewasting site, as it also makes a handy boss key).

Define the function by adding the following code to your ~/.emacs:

(defun wicked/toggle-w3m ()
  "Switch to a w3m buffer or return to the previous buffer."
  (interactive)
  (if (derived-mode-p 'w3m-mode)
      ;; Currently in a w3m buffer
      ;; Bury buffers until you reach a non-w3m one
      (while (derived-mode-p 'w3m-mode)
	(bury-buffer))
    ;; Not in w3m
    ;; Find the first w3m buffer
    (let ((list (buffer-list)))
      (while list
	(if (with-current-buffer (car list)
	      (derived-mode-p 'w3m-mode))
	    (progn
	      (switch-to-buffer (car list))
	      (setq list nil))
	  (setq list (cdr list))))
      (unless (derived-mode-p 'w3m-mode)
	(call-interactively 'w3m)))))

Then bind it to a shortcut key sequence (F7 F7 in this example) by adding the following code to your ~/.emacs:

(global-set-key (kbd " ") 'wicked/toggle-w3m)

You can then use F7 F7 to switch back and forth between your web browser and whatever else you’re working on.

Short URL: http://sachachua.com/blog/p/5088
  • http://blog.nethazard.net Gabriel Saldana

    Is there a way to set w3m as the default browser on Emacs? Most times i click on a link on my mail (gnus) or visit the php manual from php-mode, firefox gets launched instead.

  • http://niels.kicks-ass.org Niels Giesen

    No need to replicate built-in functionality:

    w3m-close-window buries all w3m windows
    w3m starts w3m or switches to last-visited w3m window

    so doesn’t

    (defun wicked/toggle-w3m-2 ()
    	 "Switch to a w3m buffer or return to the previous buffer."
    	 (interactive)
    	 (if (derived-mode-p 'w3m-mode)
    	     (call-interactively 'w3m-close-window)
    	   (call-interactively 'w3m)))
    

    do the same thing?

  • http://sachachua.com Sacha Chua

    <laugh> You can tell that I haven’t read the source code for w3m-el yet. I’ll do that later, then – it’s nice to know all sorts of good stuff is in there!

    Yes, that code snippet will work beautifully.

    Gabriel: Set your browse-url-browser-function to w3m-browse-url. That should do the trick… =)

  • ky

    Mhh,
    I do not see the use of this.
    Using iswitchbuffers, I just switch between windows by C-x b.
    So, I have the same behaviour in every buffer, not just
    w3m-el. -> I use it more often -> I develop some kind of muscle
    memory. What is the advantage of such a “specialized” keybinding
    to using the usual Buffer Switch?

    -ky

    P.S.: ‘Add comment’ does not seem to work in emacs-w3m :(
    Had to come back with conkeror.

  • Ning

    A dumb question: how to install w3m?
    I did a quick search on the web, it seems that there are two related packages, w3m and emacs-w3m, which one should I install?

    Thanks for all the great articles about emacs. I have certainly learned a lot!

    • http://sachachua.com Sacha Chua

      emacs-w3m is an Emacs Lisp interface for the w3m browser. Install them both. =)

On This Day...

  • 2012: Thinking about a visual process library — I had a good conversation with Craig Flynn and Ian Garmaise over bowls of ramen at Kenzo. We talked about [...]
  • 2011: Code and circuit for a six-function Arduino-based USB footswitch — I’d been thinking about footswitches for a while, but I held off on buying one because they were expensive. Turns [...]
  • 2010: On a Lenovo X61 — I’ve been saving up for a Lenovo X61 for a while. Drawing had turned out to be tons of fun, [...]
  • 2009: Improv 101: Learning more about characterizations — Improv is so hard! <laugh> It’s a good kind of hard, though. We warmed up with a game of Hotseat, but [...]
  • 2008: Work that I love: reflecting on the whats and hows — At the team-building event the other day, I got to meet a number of other people who had been with [...]
  • 2008: Mouse woes on Ubuntu Hardy — It’s really quite odd. I’ve tried two USB mice (one wireless, one wired) on my Kubuntu system. They work well… [...]
  • 2007: First day of driving lessons — ACK! One full day of lectures! What was I thinking?! Well, I wanted a crash course… I’ve *definitely* outgrown high-school-type education. In other [...]
  • 2005: Totally blown away — I attended the 74th Annual Convention of Toastmasters International, and the experience just totally blew me away. The quality of speech was [...]
  • 2003: Exim port — firewalled_smtp: driver = smtp port = 1234 firewalled_hosts: driver = lookuphost transport = firewalled_smtp domains = foo1.my.com:foo2.my.com:foo3.my.com