Is there a way in the org mode syntax to create a link and to define which browser to open the link in? I have some links that I want to open in a different browser than my default one.
4 Answers
You can define a function that grabs the URL at point and calls one of browse-url-<browser name>
functions. Taking the example of Chromium:
(defun browse-url-at-point-chromium (&optional ARG)
(interactive)
(let ((url (browse-url-url-at-point)))
(if url
(browse-url-chromium url ARG)
(error "No URL found"))))
And bind that function to a key:
(define-key org-mode-map (kbd "C-RET") 'browse-url-at-point-chromium)
This also works in other modes, such as mu4e
(email in Emacs):
(define-key mu4e-view-mode-map (kbd "C-RET") 'browse-url-at-point-chromium)
macOS pitfalls
On macOS, the browsers with a Graphical User Interface (GUI) are not linked to shell executables, so Emacs can't see them in the default browse-url-chromium
function, for example. So you need the additional step of giving Emacs the path to the executable:
(setq browse-url-chromium-program "/Applications/Chromium.app/Contents/MacOS/Chromium")
macOS and Safari
If you want Safari on macOS, it has the additional problem that the executable does not take arguments (see this thread). One solution from there is to call AppleScript to tell Safari to open the URL. The shell command is:
/Applications/Safari.app/Contents/MacOS/Safari & sleep 1 && osascript -e 'tell application "Safari" to open location "http://www.google.com"'
Another is to use open -a Safari <URL>
in bash and start-process
in ELisp:
(defun browse-url-at-point-safari ()
(interactive)
(let ((url (browse-url-url-at-point)))
(if url
(start-process (concat "mozilla " url) nil "mozilla" url)
(error "No URL found"))))
I was unable to make either of these work and found it easier to use another browser, such as Chromium.
You can set which browser to use for all http(s) links with this:
(setq browse-url-browser-function 'browse-url-firefox)
You can also open different links with different browsers. As an example, the following setting opens links to Google docs with Chrome, and all other links with Firefox:
- Emacs >=28:
browse-url-handlers
.
(setq browse-url-handlers
'(("https://docs.google.com/\.*" . browse-url-chrome)
("." . browse-url-firefox)))
- Emacs < 28:
browse-url-browser-function
:
(setq browse-url-browser-function
'(("https://docs.google.com/\.*" . browse-url-chrome)
("." . browse-url-firefox)))
More than two years later, and possibly not exactly what you were looking for, but still:
M-x customize-option RET org-file-apps RET
Feel free to customise.
-
2IIUC this specifies in general which app is used for which kind of link, whereas the OP wanted something specific to a particular link.– StefanOct 31, 2018 at 14:12
There is a simple way that I use myself
Keep, e.g., eww as your default browser inside emacs
(setq browse-url-browser-function #'eww-browse-url)
Click a formated link and when you're visiting a webpage in eww you can hit & (
eww-browse-with-external-browser
) to open the page in an external browser, e.g. firefox (your system default browser)