3

I use an org file for storing bookmarks. In each org-headline I have a short description of and a link to the file or location I want to open, like this :

* ...
* my notes [[file:c:/users/myname/Dropbox/notes]]
* my projects [[file:c:/users/myname/Dropbox/projects]]
* ...
* ...

When scrolling up and down this list (C-n and C-p, respectively) I like to locate point to the far left of the window. This is visually clearest for me. However, when I want to open the link in a particular headline, I then have to move the point to the right (C-f or M-f) until point overlaps with the link, and then press C-c C-o to open the link at point.

This opening process is unpractical. Therefor, I would like an interactive function that let me jump from the leftside of a headline (or from anywhere inside the headline), into the link, and opening it. How would such a function look like?

Thanks for any suggestions and help!

4

How would such a function look like?

One approach would be to add a hook to org-mode-map to remap the familiar C-n or C-p (or any other pair you want) to go to the next link, if any. Here's a suggestion:

(add-hook 'org-load-hook
     (lambda ()
          (define-key org-mode-map "\C-n" 'org-next-link)
          (define-key org-mode-map "\C-p" 'org-previous-link)))

Then once you are on that link, open as usual or write a function that calls org-next-link first, followed by org-open-at-point. You can of course combine the two functionalities into one function.

If you'd rather not go through org-mode-map, then consider the built-in:

C-u C-c C-o

on an headline anywhere will offer links under that headline as choices to select from. Look in the manual if you want to further customize this list.

The last approach is not general enough...

Your definition of general conflates the heading line with the heading. They are not the same.

it only works on folder links, not on file links.

Wrong. It works on all org link types: internal, external, or any future link types you might define. Check your installation or the key bindings to make sure you are not using something non-standard.

Where should your "add-hook" code be placed in my init file?

Before loading org-mode. This is true for all hooks, not just this one. You always load the hook before you need the hood to run.

  • Hi, thanks for your answer. The last approach is not general enough, because it only works if point is at least two places to right of the star, hence it does not work if point is in its leftmost position. n addition it only works on folder links, not on file links. The first solution is interesting if mapped to some other keys. But still it is visually distracting that point jumps back and forth somewhere inside the header line instead of followint the left margin. – myotis May 17 '16 at 13:47
  • Where should your "add-hook" code be placed in my init file? I have placed it at the end of all init code for org mode adjustments and changed C with M, but when hitting M-n og M-p I got the message that they are "undefined". What is going wrong. I am a windows user, emacs version 25.0.93.1 – myotis May 17 '16 at 14:15
  • Where should your "add-hook" code be placed in my init file? See my updated answer. – Emacs User May 21 '16 at 16:17
1

This function could just call org-next-link followed by org-open-at-point, enclosed in save-excursion so that point is not changed. In addition, it can check that the link is on the same line than point.

(defun my/follow-header-link ()
   "Follow the next link on the same line"
  (interactive)
  (save-excursion
    (let ((e (save-excursion (end-of-line) (point))))
      (org-next-link)
      (if (< (point) e)
          (org-open-at-point)
        (message "no link on current line")))))
1

This should work:

(defun open-org-link-in-heading ()
  "Open first link in the org-mode heading at current line.
Only works when at the beginning of the line."
  (interactive)
  (when (and (looking-at "^\\*")
             (re-search-forward org-any-link-re (line-end-position) t))
    (org-open-at-point)))

For an easy keybinding, you could add it to org-metareturn-hook and use it with M-RET.

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.