Command-line browsers Lynx, Links, and w3m

Surfing the Shell

By Heike Jurzik

If you're working at the command line and you need to reach the Internet, or if you just want to convert an HTML file into neatly formatted ASCII text, try a text-based web browser.

Text-mode surfing may seem like a last resort, but a text-based browser is sometimes the perfect tool. In this month's Command Line, you'll learn about the no-GUI browsers Lynx [1], Links [2], and w3m [3].

Lynx

You can launch Lynx in a terminal or a virtual console by typing lynx - if you prefer, you can pass a URL or the address of a local HTML file to the browser when you launch it, e.g.:

lynx www.linux-magazine.com

By default, Lynx displays an overview of common keyboard shortcuts at the bottom of the screen. Lynx is completely keyboard controlled. Table 1 shows the most important keyboard shortcuts.

You can modify Lynx's behavior by specifying command line options when you launch the browser, or by changing the settings in the options dialog, which you can call by pressing [O]. If you prefer not to be prompted to accept Cookies, you can disable the prompt as follows:

lynx -accept_all_cookies

To make this setting permanent, navigate the options dialog with the [Down Arrow] key until you find the entry for Cookies, and then press the [Right Arrow] key. In the menu that appears, you can select from ignore, ask user (default), and accept all (Figure 1). To make sure Lynx does not forget your settings when you quit the program, you still need to save your changes permanently (before you confirm by clicking on Submit or Accept Changes). To do so, check the Save options to disk checkbox by pressing the [Enter] key. Lynx stores user settings in a hidden configuration file called ~/.lynxrc, which you can modify using a text editor.

You can use the -book option to tell Lynx to launch into a view with your own personal bookmarks.

For password protected pages, either enter your user name and password interactively, or else supply your credentials when you call the browser:

lynx -auth=username:passwordU
 www.url.de

If you use Lynx to check out local HTML pages, you can tell the browser not to follow external links when you launch (option -localhost). You can also use your favorite editor in Lynx. If you tell Lynx that

lynx -editor=vim

on launching, you can then press [E] to load the HTML page you are currently viewing in the Vim editor. After quitting the editor, you automatically return to Lynx, where you can reload the page by pressing [Ctrl-R].

To make sure Lynx remembers your favorite editor next time you launch the browser, press [O] again to access the option dialog, where you can enter the name of your favorite program in the Editor box.

From Lynx to Links

The second text browser, Links, gives you keyboard and mouse controls. The command for launching the browser is links - and again you can specify a URL or a local file. If you press [Esc] or click on the top line of the window with your mouse, Links displays a menu, which you can navigate using your mouse or keyboard. Keyboard shortcuts are assigned to most menu entries, and it makes sense to learn the shortcuts and leave your mouse on your mouse pad. Table 2 gives you an overview of the most common commands. As the mouse is used for program control, you can't simply select an area of text and send it to the clipboard in the normal way - instead you have to hold down the [Shift] key and drag your mouse over the required area of text.

Figure 1: Using the options dialog to configure Lynx.

The Setup menu allows you to configure the browser. The Setup menu is where you set up things like the language, character set, terminal options (color, cursor etc.), the cache size, and so on. Your personal settings are stored in a file called links.cfg in the hidden ~/.links folder. Again, links.cfg is a text-only file that you can change using your editor.

Figure 2: w3m gives you tabbed browsing.

Links renders tables and frames better than Lynx, and you can tell the browser how to handle specific file types. For example, to display PNG-formatted images in the external display program (which only runs on X Window), first define a file type in Setup / Associations / Add. To do so, enter a name in the Label field, e.g., PNGs. Set the Type(s) to image/png, and finally, define the Program Links will use to display the PNG files. After completing these steps, use Add in the Setup / File extensions menu to define file extensions for the file type you have just defined. Add the possible file extension to Extension(s) in a comma-separated list, for example:

png,PNG

Again type image/png as the Content-Type, confirm by selecting OK, and save your changes. Now, whenever you right-click a PNG document that is rendered as an [IMG], you can select View image or press the [Right Arrow] key to open a dialog window that prompts you to open, save, or display (the source code) of the file.

Table 1: Lynx Controls
[Shift--] or [H] Calls Lynx help.
[Q]Quits the program after asking if you really want to quit Lynx.
[Shiftt-Q]Quits Lynx without asking.
[Up Arrow], [Down Arrow]Scroll forward or backward through links.
[Pg Up], [Pg Dn]Scroll up or down one page.
[Right Arrows] or [Enter]Follow a link.
[Left Arrow]Go back to last page.
[K]Displays a list of available keyboard commands.
[M]Goes back to start page. Confirm by pressing [Y].
[G]Opens an input dialog where you can type a new URL;
Confirm by pressing [Enter].
[Shift-G]Go to last URL, allows you to modify this address.
[L]Shows all visible links in the current document.
[P]Printer options: Store the document in a local file,
send the file by email, send the document directly to a printer.
[D]Saves the file (with all images).
[A]Adds a bookmark either for the page being viewed ([D]) or
for the page accessed by the link under the cursor ([L]).
[V]Opens the bookmark manager.
[Ctrl-A]Back to top of page.
[Ctrl-E]Go to end of page.
[Ctrl-R]Reload page.
[Shift-7] (/)Start search.
[Backspace]Display history.
[O]Open the Lynx configuration dialog.

Simply w3m

The third browser on our list is called w3m. In contrast to Links and Lynx, w3m expects you to supply a Web address or local path name. If you do not supply either, w3m simply outputs a list of options on your screen.

Again, the browser supports keyboard and mouse controls (see Table 3 for an overview of keyboard shortcuts), and again you need to hold down the [Shift] key to select text passages with your mouse.

The ability to open multiple Web pages using the [Shift-T] shortcut, and to toggle between the tabs, is a really useful feature. Pressing { takes you to the next tab, and } takes you back. Pressing [Esc-T] (or [Alt-T] on some desktops) opens a tab selection dialog, and [Ctrl-Q] removes the current tab (Figure 2).

Users of w3m definitely need to open the settings dialog by pressing [O] and make any changes they need. w3m provides many configuration options, and getting them all set to your liking will enhance your browsing experience.

Table 2: Keyboard Commands for Links
[Esc]Displays and hides the menu bar.
[Q]Quits Links - with prompt.
[Shift-Q], [Ctrl-C]Quits the program without prompting.
[Up Arrow], [Down arrow]Navigates forwards and backwards through links.
[Pg Up], [Pg Dn]Scroll one page forward/back.
[Right Arrow]Follow a link.
[Left Arrow]Go one step back in history.
[G]Open an input line where you can enter a new address.
[Shift-G]Opens the same address input line but displays the last address you visited, allowing you to modify the address.
[D]Stores a local copy of the document on your computer.
[Shift-7] (/)Forwards search in current document.
[Shift--] (?)Backwards search in current document.
[N]Go to next match.
[Shift-N]Go back to previous match.
[Shift-0] (=)Open information window with details on URL, size, character set, Web server etc.
[\\] Display source code of document, pressing the key again takes you back to the browser view.
[A]Add a bookmark.
[S]Open bookmark management.
[Ctrl-R]Reload document.

Lots of Tricks

Text browsers really shine in collaboration with other programs. If your favorite editor is Vim, for example, you only need to add a single line to the ~/.vimrc configuration file to set up your favorite browser as an HTML viewer. You can assign the browser launch function to a key or define a new command. For example, you need to add the following to ~/.vimrc to launch Lynx whenever you press [F2]:

map <F2> :!lynx %<CR>

You can replace lynx with either links or w3m if you prefer one of the other browsers.

To define your own command for Vim, add the following to ~/.vimrc, for example:

command View :!lynx %

You can then launch the browser while working in Vim. To launch the browser from Vim, press [Esc] to enter command mode and type :View.

It is just as easy to integrate all three browsers into Mutt [4]. To view HTML attachments directly in your email client, add the following entry to your ~/.mailcap file:

text/html; links %s

Then add the path to your own mailcap file to the configuration file ~/.muttrc:

set mailcap_path = ~/.mailcap

The next time you receive an HTML-formatted message, you can simply press [V] (for view attachments) and select t he HTML attachment by pressing the [Enter] key.

Table 3: w3m Keyboard Controls
[Shift-H]Calls help.
[Q]Quits w3m after prompting to confirm.
[Shift-Q]Quits w3m without prompting.
[Space], [B]Scroll one page forwards / backwards through the current file.
[G], [Shift-G]Go to first / last line of document.
[Tab]Go to next link.
[Esc], [Tab] (or [Alt-Tab])Go to previous link.
[Return]Follow link.
[Shift-B]Go back to previous page.
[U]Display the URL for the current file in the bottom left-hand corner
of the window.
[Shift-U]Open a dialog where users can enter a new URL.
[Shift-7] (I)Forward search for a search key.
[Shift-?] (?)Backward search for a search key.
[V]Toggle browser between normal and source code view.
[Esc], [A] (or [Alt-A])Add document to your list of bookmarks.
[Esc], [B] (or [Alt-B])Display bookmarks.
[Shift-R]Reload page.
[S]Open a history of the recently visited pages.
[Esc], [S] (or [Alt-S])Save the file to disk.
[O]Open dialog for personal settings.
[E]Open page in editor (as defined in the configuration dialog).

Format, Format

All three of the browsers discussed in this article use the dump command line option for converting HTML pages to neatly formatted files in ASCII text format. The following commands

lynx -dump /home/huhn/<@15_Li_Pfeil_(U)>U
lynx.html | less

tell the Lynx browser to ormat the document and call the less pager to dis- play the document on the screen page by page.

INFO

[1] Lynx: http://lynx.isc.org/

[2] Links: http://links.sourceforge.net/

[3] w3m: http://w3m.sourceforge.net/

[4] Mutt: http://www.mutt.org/