Emacs Eww Useful Tips and Tricks
Introduction
These are some of the tweaks I've made that improved the quality of life in EWW. I use evil mode.
Simple Styling
I ripped this from the wiki and increased the indentation value
(setq browse-url-browser-function 'eww-browse-url) ; Use eww as the default browser
(setq shr-use-fonts nil) ; No special fonts
(setq shr-use-colors nil) ; No colours
(setq shr-indentation 4) ; Left-side margin
(setq shr-width 70) ; Fold text to 70 columns
(setq eww-search-prefix "https://google.com/search?q=") ; Use another engine for searching
Force EWW to open new buffers by default
Normally EWW only runs as a single buffer, similar to Eshell. This function will make a new EWW buffer by default instead of replacing the previous.
(use-package eww
:ensure t
:config
(defun modi/force-new-eww-buffer (orig-fun &rest args)
"When prefix argument is used, a new eww buffer will be created,
regardless of whether the current buffer is in `eww-mode'."
(if current-prefix-arg
(with-temp-buffer
(apply orig-fun args))
(apply orig-fun args)))
(advice-add 'eww :around #'modi/force-new-eww-buffer)
)
Faster Searching
This function will prompt you for a string and allow you to search for it using your favorite search engine. If the string is a url it'll open it up.
(defun my/is-url (string)
"Checks if STRING is a valid URL."
(let ((url-parts (url-generic-parse-url string)))
(and url-parts (url-type url-parts))))
(defun my/eww-search (x)
"Searches stuff in eww"
(interactive "sSearch Query: ")
(switch-to-buffer (generate-new-buffer "eww"))
(eww-mode)
(if (my/is-url x)
(eww-browse-url x)
(eww (concat "https://www.google.com/search?q=" (replace-regexp-in-string " " "+" x)))
))
; Binding it using evil mode
(evil-define-key 'normal eww-mode-map "ss" 'my/eww-search)
Search highlighted region
Highlighting a url will open it using EWW. If you highlight text, it'll google the string instead.
(defun my/eww-search-selected (start end)
"Searches the selected text in EWW. If it's a URL, opens it directly. If not, searches Google."
(interactive "r")
(when (use-region-p)
(let ((region (buffer-substring-no-properties start end)))
(if (my/is-url region)
(eww-browse-url region)
(split-window-right)
(balance-windows)
(other-window 1)
(let ((query (replace-regexp-in-string "\n" " " region)))
(eww-browse-url (concat "https://www.google.com/search?q=" (url-hexify-string query))))))))
(define-key map (kbd "C-c s") 'my/eww-search-selected)
Redirect Reddit to Reddit.old
Self explanatory. I had to compile emacs 29.2 with libxml2 support to get it to work. The URL transformers function wasn't available with my package managers emacs version.
(defun eww-reddit-redirect(url)
"Redirect reddit.com to old.reddit.com automatically."
(replace-regexp-in-string "https://www.reddit.com" "https://old.reddit.com" url))
(setq eww-url-transformers '(eww-remove-tracking eww-reddit-redirect))
Quasi Default Web Browser
Have EWW act as a default web browser source
(defalias 'gk-urls-external-browser 'browse-url-xdg-open)
(defun gk-browse-url (&rest args)
"Prompt for whether or not to browse with EWW, if no browse
with external browser."
(apply
(if (y-or-n-p "Browse with EWW? ")
'eww-browse-url
#'gk-urls-external-browser)
args))
(setq browse-url-browser-function #'gk-browse-url)