As I do most of my daily works on
emacs, it's the only program always running on my system. So from time to time I try to create small recipes to make my life easier while using it.
Recently I thought of an idea to invoke the search engine on browser with search term directly from
emacs; so essentially I don't want to go to browser and search for a term, it will do that for me.
(defun search-on-browser (term) "Search TERM on preferred engine on browser. If no search TERM is entered interactively, the current buffer selection is used as the TERM." (interactive "sSearch term (default to selection): ") (when (eq term "") (setq term (buffer-substring (region-beginning) (region-end)))) (setq term (replace-regexp-in-string " +" "+" term)) (unless (boundp 'search-engine-query-url) (setq search-engine-query-url "https://duckduckgo.com/?q=")) (unless (boundp 'browser-command) (setq browser-command "firefox")) (let ((full_query_url (concat search-engine-query-url "'" term "'"))) (shell-command (concat browser-command " '" full_query_url "'") nil nil)))
The docstring says it all.
I added a key binding for this as well:
(global-set-key "\C-c\C-s" 'search-on-browser)
firefox is my preferred browser so the default is set as such. To use a different browser, the variable
browser-command can be set e.g. to use
(setq browser-command "chromium")
Absolute path to the browser binary can also be given.
I use DuckDuckGo as my search engine. To use a different one, the variable
search-engine-query-url can be set to give the full query URL prefix of that search engine. For example, to use Google:
(setq search-engine-query-url "https://google.com/search?q=")