Eww, a web browser inside a text editor

Emacs text editor is not immediately accessible to beginners, such as myself. However, it contains so many additional features that one may end up using them, thus ending up using Emacs indirectly. Such additional features include Org mode, emailer, and webbrowser.

Get on the web and zoom

After launching Emacs, press Alt+x, which brings up a prompt, type “eww”, which in turn prompts for a web address. This is the first entrance to the Emacs web browser.

Emacs has two appearances: Graphical and text-mode. To keep it inside the terminal emulator, type emacs -nw in the terminal. In the graphical mode, Eww attempts to show images while the overall layout is left-aligned, as is usual in text editors. In the terminal emulator, colours, mouse actions, etc. depend a lot on the specific emulator in use. Colours additionally depend on the theming of Emacs.

Text size can be increased with Ctrl+x and Ctrl+plus – that is, press Ctrl+x and then, still holding Ctrl, the plus key , this is C-x C-+ in Emacs notation – in the graphical mode. Decrease of text or zooming out is correspondingly C-x C–. In the terminal emulator, zooming depends on the terminal emulator. In XTerm, it is Shift with numpad’s plus or minus.

Unfortunately, zooming messes up line rendering in Eww. Rendering is readjusted by Shift+r. Rerendering in turn brings you to the beginning of the webpage. To return wherever you were earlier, use Ctrl+s for incremental search, hopefully remembering some distinctive word you were looking at before.

Move and scroll by keyboard

The Tab key jumps from link to link. Shift+Tab does the same in reverse direction.

I do not bother to discuss very basic movements per character back and forth, and per line up and down, because they work simply by arrow keys and this is not how we browse the web anyway. We browse the web more by chunks such as Space which scrolls down a screen’s height.

In Emacs (as in word processors), Ctrl+up arrow and Ctrl+down arrow move up and down by paragraph (text block), which is perhaps the best basic movement when browsing the web for reading purposes.

Then there is a somewhat important distinction between moving and scrolling. Scrolling means that the text lines shift up or down on the screen. Moving means that the cursor is moving left or right or up or down. The arrow movements both with and without Ctrl primarily move the cursor. They scroll only when the cursor hits the edge of the visible area. But for example PgUp and PgDn represent pure scrolling.

Ctrl+l: Scroll by the cursor position

My favourite default Emacs keybind is Ctrl+l (or C-l in Emacs notation). This scrolls the cursor position to the centre of the visible area. That is, this is not a cursor movement, but a scroll of the text: The line where the cursor is, this line gets window-centred along with the cursor.

What makes it my favourite is its behaviour when repeated. The first issuance of Ctrl+l centres the line with the cursor. The second issuance moves the line with the cursor topmost in the window and the third to the bottom of the window.

The way I like to browse with Eww is to navigate to the start of the paragraph I am about to read with Ctrl+down arrow. Arriving there, I press Ctrl+l twice to bring the beginning of the paragraph to the top of the window. This is the best feature in Eww.

The “move” equivalent of Ctrl+l, i.e. move the cursor (as opposed to scroll) to the center line, on command repeat to the first visual line, and on next repeat to the last visual line, is Alt+r.

Scroll by line

For pure scrolling by line (as opposed to moving the cursor by line which is done simply by up and down arrow keys), Emacs has functions called scroll-up-line and scroll-down-line but these lack convenient keybinds. To access these (as any other) functions, one is supposed to press Alt+x to get a prompt for function and then type the function, which is tedious. However, the function is there and therefore can also be configured to keybinds. I have configured these useful scroll functions to Alt+up arrow and Alt+down arrow in my .emacs file.

(global-set-key (kbd "M-<up>") 'scroll-down-line)
(global-set-key (kbd "M-<down>") 'scroll-up-line)

However, Alt+arrows have other functions in other parts of Emacs (notably in Org and in markdown-mode), so some other keybinds could be better, if one is looking for consistency (which is frankly impossible to find and to achieve in Emacs).

Navigate by numerical prefix

Navigating by line is precise, but tedious when you need to navigate more than a few lines. To navigate more efficiently, but still precisely, the keybinds can be prefixed with numbers.

The function of the numerical prefix is to make the command repeat the given number of times. For example, typing 5 and down arrow in Eww moves the cursor down five lines. Typing 5 and then Ctrl+down arrow moves down five paragraphs!

With a numerical prefix, Ctrl+l has special behaviour: It scrolls to the indicated visual line. The numerical prefixes in arrow keys indicate relative lines, meaning line calculated from the current position of the cursor, while visual line means line calculated from the top of the visible area. Thus, typing 5 and Ctrl+l scrolls the current line to become the fifth line in the window.

There are also negative numerical prefixes. For example, typing -5 and up arrow moves the cursor down instead. Typing -5 and Ctrl+l scrolls the current line to become the fifth line from the bottom of the window.

(As an extra tip, numerical prefixes also work in Emacs text editor, but in the editor the numerical prefix must in turn be prefixed by C-u “the universal prefix” to indicate that what follows is not to be typed into the text buffer.)

With some commands, however, numerical prefixes do not work. For example, it would be nice for 5+PgDn to scroll five screens down, but it does not.

Tabbed browsing

Emacs does not do tabs. It does buffers and windows. When on a link, you can press Alt+Enter for “Follow URL in new buffer”, but this conflicts with XTerm’s Alt+Enter for fullscreen.

Moreover, navigating buffers and windows in Emacs is such an alchemy that I have not figured it out. The best I know is that C-x b brings up the list of buffers.

Summary and continuation

So this post covers launching Emacs, opening the prompt (with Alt+x) for eww which in turn opens the prompt for web address, and then re-rendering (with Shift+r) and navigating the loaded web page. This is enough for a start.

To advance to an intermediate level, one may want to configure a neutral user agent string in .emacs file.

(require 'url-http)
(setq browse-url-browser-function 'eww-browse-url)
(setq url-user-agent "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3739.0 Safari/537.36\n")

However, I still find w3m a more convenient text-mode web browser even with the lack of scrolling feature like C-l in Emacs. There is a w3m plugin/extension/mode for Emacs, but it is a whole new installation in addition to the ordinary w3m and has its own incompatible poorly documented manner of configuring.

Leave a Reply

Your email address will not be published. Required fields are marked *