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.
The previous two answers didn't work for me:
(setq process-connection-type nil)broke
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...
setsid xdg-open 1.pdf works for me. The problem seems to be that
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.
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.
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
sleep 1 ;
anyway, shell will still wait background process
Inspired by https://depp.brause.cc/dotemacs/#orgd97f08c here is a solution for
(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))
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.
(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.