6

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.

1
  • Do these links have some shared characteristics (other than that you want to open them with some other browser)?
    – Stefan
    Oct 31, 2018 at 14:12

5 Answers 5

5

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.

2

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.

1
  • 3
    IIUC this specifies in general which app is used for which kind of link, whereas the OP wanted something specific to a particular link.
    – Stefan
    Oct 31, 2018 at 14:12
2

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)))
1

There is a simple way that I use myself

  1. Keep, e.g., eww as your default browser inside emacs (setq browse-url-browser-function #'eww-browse-url)

  2. 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)

0

I'm not aware of other ways, than to brute force it;)

(defun extract-link ()
  (interactive)
  (when (org-in-regexp org-bracket-link-regexp 1)
    (let ((link (org-link-unescape (org-match-string-no-properties 1))))
      link)))

(defun open-org-link-in-firefox ()
  (interactive)
  (setq url (extract-link))
  (browse-url-firefox url)
  )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.