4

I use emacs 24.4.1 on ubuntu 15.04. Somehow xdg-open doesnot work if i run it from emacs. For example, if i run "xdg-open 1.pdf" or "gvfs-open 1.pdf" in eshell they do not work. At the same time, "evince 1.pdf" works perfectly.

5 Answers 5

6

The previous two answers didn't work for me:

  • (setq process-connection-type nil) broke org-plot/gnuplot
  • setsid xdg-open ... worked for a while, but then it stopped for some reason.

Now I use setsid -w, which supposedly makes setsid wait until the called program finishes.

If someone would need it, I use this with AUCTeX (and I found this question while trying to fix AUCTeX behavior on Arch linux), and my TeX-command-list is set to

(quote
 ( 
  ;; ...
  ("View" "setsid -w xdg-open %s.pdf" TeX-run-command t t :help "Run Text viewer")
  ;; ...
  )

It seems to work fine for now...

1
  • could you explain how to hook that into opening org mode links?
    – xeruf
    Feb 23, 2021 at 19:45
5

Using setsid xdg-open 1.pdf works for me. The problem seems to be that gvfs-open and xdg-open return before their children are done working. Emacs might kill their controlling terminal when this happens, killing the children, and stopping evince from opening properly. setsid sets a new controlling terminal that emacs won't close.

2
  • 1
    To make it work I had to include -w (wait): setsid -w xdg-open 1.pdf Mar 10, 2019 at 21:34
  • could you explain how to hook that into opening org mode links?
    – xeruf
    Feb 23, 2021 at 19:45
3

This may be a problem with using ptys for subprocess communication. Try setting process-connection-type to nil to use pipes instead.

In your .emacs

;; Use pipes for subprocess communication
(setq process-connection-type nil)

I use the publishing functions from org mode and without this change, none of the generated files get opened via xdg-open, despite no error messages.

4
  • Thanks, it worked! Is there some documentation containing some explanation for this?
    – Emil Vatai
    Jan 11, 2017 at 10:27
  • This method does work, but might cause problems for other applications (for me it ruined calling gnuplot from org-mode), so it's not an optimal solution! I have Michael Hoffmans solution in my .emacs now (for the view command in Auctex). We'll see how it works.
    – Emil Vatai
    Feb 16, 2017 at 5:28
  • To reproduce the error mentioned above: try running org-plot/gnuplot with the cursor/point on a table with and without process-connection-type set to nil, for more details see first subsection of this page.
    – Emil Vatai
    Feb 16, 2017 at 6:43
  • also broke ripgrep search with ivy for me :/
    – xeruf
    Feb 23, 2021 at 19:27
2

xdg-open work in sync mode: (shell-command "xdg-open .") , but not in async mode: (async-shell-command "xdg-open .") .

it seem like xdg-open spawn child process to open file then exit. before child process open file, xdg-open exit, so shell exit, too. shell exit kill all child process.

so just make shell life little more: (async-shell-command "xdg-open . ; sleep 1") (async-shell-command will append & after sleep 1 ; anyway, shell will still wait background process & finish.)

1
0

Inspired by https://depp.brause.cc/dotemacs/#orgd97f08c here is a solution for org-mode:

(setq org-file-apps '((remote . emacs)
                      (auto-mode . emacs)
                      (directory . emacs)
                      (system . "setsid -w xdg-open %s")
                      (t . system)))

This overrides the default associations and thus also prefers opening file formats such as PDF within Emacs if possible. Alternatively, you can append to the existing list, as mentioned on SO:

(setq org-file-apps-gnu
      (append '((t . "setsid -w xdg-open %s")) org-file-apps-gnu))

The t means it will handle everything not explicitly handled by something else. You can append to org-file-apps instead of org-file-apps-gnu, but since xdg-open is only commonly used on GNU systems, this should be ideal.

I used (setq process-connection-type nil) happily for a long time, but in the last days I found out that it broke integrations with ripgrep after hours of debugging. So I would heavily discourage that.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .