Remove cookies when exiting w3m

I don’t like being tracked more than necessary. Therefore I setup my browsers to delete all cookies when I exit them. Unfortunately w3m doesn’t have an option to do so.

Solution is to use a wrapper script to call w3m.

In order to do so we must understand some basics on what happens when we start a program from the command line.

When you log in, your shell will automatically setup some environment for you. One part of this environment is your PATH. It is saved in a variable called $PATH.

To see your current $PATH open a shell and execute

echo $PATH

Mines looks like this:

/home/kt/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Now, when you type a command your shell will take the PATH and look into each directory if it contains an executable which matches the name. If an executable is found, the search stops and the executable is called.

As you can see in my path the first directory is /home/kt/.local/bin. We can use this to our advantage and write a so called wrapper script.

If your $PATH doesn’t contain a directory in your home directory you need to set it up in your configuration. I’m not going to explain how to do this here as searching the web will give a enough tutorials.

A wrapper script is a little shell script with which replaces an executable. This means it has the same name as the executable and is placed in a directory which comes first in the path.

So, to remove cookies when exiting I created a shell script in /home/kt/.local/bin with the name w3m. As it is in the first directory in my path it will always be found first.

The script looks like this:

#!/bin/sh

/usr/bin/w3m "$@"
rm -f .w3m/cookie .w3m/w3mcookie*

Not going deep into shell scripting, but here is what the script does: The first line defines the interpreter to execute the script (so called shebang). Then we call the original w3m executable and pass all the arguments that were passed to the script. This means if I call w3m -B to start w3m with my bookmarks open, the "$@" will be replaced with -B. At this point we are inside w3m as we used to, as a user you don’t recognize that you’ve called a wrapper script.

When we exit w3m the next line will be executed. And this line removes all cookie related stuff from the .w3m directory (the -f is to suppress errors when files are not present).

One important thing to do before writing the script is to call which w3m to see the actual location of w3m; though this will be /usr/bin/w3m on most - if not all - systems.

After creating the script you have to make it executable (and for security reasons only writable by yourself):

chmod 700 ~/.local/bin/w3m

With this approach it is also possible to do more manipulations, e.g.

tail -n 20 .w3m/history > /tmp/history && mv /tmp/history .w3m/history

will keep the last 20 entries from your browsing history.

Using sed one could delete all entries from the history which were google searches:

sed -i '/google/d' .w3m/history

(This assumes you have GNU sed. If not - you get the idea.)