My Setup for Using Emacs as Web Browser

| Comments

This post was actually a part of the last one, but the node was long enough so I broke it into two. Why? Well, why making it one post when I can make them two? To save users a click and a page load (I know some UX) of course, but I also have to convince ever self-criticizing brain that I am justifying with my blog and not slacking off.

So here’s my setup for using Emacs as a web browser.



w3m as web browser

I use emacs-w3m as for web browsing from inside emacs. It’s awesome. For using emacs-w3m from inside Emacs, it’s required to first install w3m. w3m is actually a text based browser which can be used from command line (like lynx. So we gotta install it first. 

Depending on your GNU/Linux distribution, run appropriate command to install w3m

Ubuntu
sudo apt-get install w3m
Fedora
sudo yum install w3m
I used `pacman -S w3m` (Chakra).

emacs-w3m in emacs

Then of course we need emacs-w3m install in emacs. Do `package-install w3m` from within Emacs.

My setup

Here’s my setup for customizing web browsing in Emacs. Explanation follows.


(setq browse-url-browser-function ‘w3m-goto-url-new-session)
This line sets Emacs’ default browser for visiting web pages to w3m function w3m-goto-url-new-session.

This would open a new tab (if you want to call that) for the url to be visited.

(setq w3m-user-agent “Mozilla/5.0 (Linux; U; Android 2.3.3; zh-tw; HTC_Pyramid Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.”)

This line sets the user-agent string which w3m will use. It really helps to use a mobile user-agent as many sites provide small content centric versions of their websites for mobiles. Most desktop sites in w3m come up with a huge untitled list covering the whole screen.
(defun hn ()
  (interactive)
  (browse-url “http://news.ycombinator.com”))

This creates a quick interactive function for quickly visiting hacker news with `M-x “hn”`
(defun reddit (reddit)
  “Opens the REDDIT in w3m-new-session”
  (interactive (list
                (read-string “Enter the reddit (default: psycology): ” nil nil “psychology” nil)))
  (browse-url (format “http://m.reddit.com/r/%s” reddit))
  )

Another function like above for going to reddit. It opens up mobile reddit site with provided subreddit name. This one prompts for entering a reddit name to visit, and if none is provided /r/psychology is visited by default. You can change it to any of the subreddits.
(defun wikipedia-search (search-term)
  “Search for SEARCH-TERM on wikipedia”
  (interactive
   (let ((term (if mark-active
                   (buffer-substring (region-beginning) (region-end))
                 (word-at-point))))
     (list
      (read-string
       (format “Wikipedia (%s):” term) nil nil term)))
   )
  (browse-url
   (concat
    “http://en.m.wikipedia.org/w/index.php?search=”
    search-term
    ))
  )

As I’ve told earlier, I am an information maniac. I just need to know everything feasible about everything new I meet. And wikipedia is a nice quick thing for solid information. When called it will prompt for entering a search term, with the word under cursor as default. It then makes a search on Wikipedia for the given search term.

You can set to a keyboard shortcut if you want. I use it with `M-x wik…` only.

(defun w3m-open-site (site)
  “Opens site in new w3m session with ‘http://’ appended”
  (interactive
   (list (read-string “Enter website address(default: w3m-home):” nil nil w3m-home-page nil )))
  (w3m-goto-url-new-session
   (concat “http://” site)))

This adds another extra interactive function ‘w3m-open-site. When we enter a url in w3m-goto-url or w3m-goto-url-new-session, it expects full url with ‘http://’ in front. So this one just appends ‘http://’ in front of a website url you provide.

Some tips

Here are some extra tips for using emacs-w3m as web browser.

Different keyboard shortcuts

W3m has its own keyboard shortcuts. Here are some I use most

Navigation
We can use standard Emacs controls for moving cursor (C-n, C-p, C-f, C-b) as well as Vim’s (g,h,j,k). Vim’s controls are really handy when you need to move cursor with one hand.

Jumping to links
Tab will go you to next link. `Shift-Tab` to previous

Visiting different tabs
w3m has tabs like system. You can easily change tabs (buffers) with Emacs’ standard `C-x b` or use `C-n` for next tab and `C-p` for previous tab.

Open link in new tab
Just entering `G` will do the job. It will prompt for a url with the url under cursor as default.

Open link in same tab/buffer
`g` works similar to `G` but opens url in current tab only.

Go to previous page
`B` will act as ‘Back’ button of standard browsers

Toggle visibility of image under cursor
`t` shows or hides an image under cursor

Toggle visibility of all images on page
`T`

Download url under point
`d`

Move to next form field
`]`
Move to previous form field
`[`

There are so many more I don’t use. You can check all special shortcuts provided by w3m in the mode help menu. In a w3m buffer, press `C-h m` to access the help buffer.

Submitting forms

We sometime need to submit forms, like for google search, or making comment somewhere. You can step in a form by pressing ‘Return’ and enter text in minibuffer. The form can then be submitted with ‘C-c C-c’.

Google search

You can simply use ‘w3m-goto-url’ or ‘w3m-goto-url-new-session’ for Google search. That’s the default action. When we enter something which is not a standard url, it will open the Google search for that string with ‘feeling lucky’ set.

w3m-lnum-mode

Thanks to Ben Chen (from G+) for this tip. I didn’t know about it. Activating w3m-lnum-mode minor mode when web browsing with w3m adds ace-jump-mode like navigation to the buffer. We can just press ‘f’ and the all the links and forms on the page become accessible by entering numbers. This can be really handy for quick navigation. Although I think I’ll stick with ace-jump-mode.

That’s all I can recall at the moment. Please share your views (good or bad) in comments. I’ll highly appreciate every word from you.

Comments