0

I use Emacs 26.3 on Ubuntu 20.04 and I often need to manipulate org files with Ubuntu file manager. It would save me a lot of time if I could open directories from emacs file links. For example, I would like to be able to open folder PRISM through the following link : [[./References/Deliverables/PRISM/proposal.org][PRSM Proposal]].

1

I don't know much about Ubuntu at the desktop level, but on my Gnome desktop, I can open a directory in the file manager from the command line, using the command xdg-open /path/to/directory. I assume there is something similar (or perhaps identical) to xdg-open on the Ubuntu desktop.

If so, all you have to do is write a function that parses the link at point, selects the directory part of the path (which may be the whole path, or it may be a prefix of the path, as in your example) and then calls xdg-open (or whatever the Ubuntu equivalent is) with that path as argument. Then you bind the function to a key and off you go.

Here's a function that looks at the link at point and calls xdg-open with the directory part of the path of the link.

#+begin_src emacs-lisp
  (defun ndk/desktop-open-link-at-point()
    (interactive)
    (let* ((context
            (org-element-lineage (org-element-context) '(link) t))
           (type (org-element-type context))
           (path (org-element-property :path context))
           (dirpath (if (file-directory-p path)
                       path
                      (file-name-directory path))))
      (shell-command (format "xdg-open %s" dirpath) nil nil)))
#+end_src

For convenience, bind the function to the key <M-S-f12> (i.e. Meta-Shift-FunctionKey12) or some other undefined key in your keymaps:

#+begin_src emacs-lisp
(define-key global-map (kbd "<M-S-f12>") #'ndk/desktop-open-link-at-point)
#+end_src
4
  • Thanks a lot NickD, it worked perfectly! And for the record, xdg-open works on Ubuntu. Do you by any chance know how can I list all emacs defined keys, so I can define a key which isn't already defined ? – crocefisso Sep 17 '20 at 15:26
  • See Keymaps in the Emacs Lisp doc but be warned: things are not simple. You might want to start with the more introductory sections of the Emacs manual: Keys and Key Bindings before you tackle the first link. – NickD Sep 17 '20 at 15:52
  • I found C-h b (describe-bindings) which displays a list of all the key bindings in effect. – crocefisso Sep 18 '20 at 7:28
  • Just realize that whatever is in effect in this buffer is not necessarily the same as what is in effect in the next buffer you visit: it depends on the buffer's major mode and all the minor modes enabled (at least). – NickD Sep 18 '20 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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