1

I have a document with hundreds of org headings, sorted alphabetically. I would like to find a way of jumping to the first visible heading whose initial letter(s) match the string typed so far. (Intuitively, the functionality I am trying to replicate resembles that of some file managers such as OSX Finder, where one can quickly jump to any file or directory just by typing the first few letters of it, with the difference being that the candidates here are instead visible org headings.)

For example, suppose that the first few lines of my document look like this:

* AlphaGo
AlphaGo is a computer program that plays the game Go. It should not be confused with AlphaZero.
** Gamma
* AlphaZero
* Beta
* Gamma

Then (assuming point is at the beginning of the document) typing a would move point to the first line, typing alphaz would move point to the fourth line, and typing g would move point to the final line if the first heading is folded, and to the third line otherwise.

4
  • Ctrl-s Ctrl-q Ctrl-j * alphaz...
    – choroba
    Apr 28, 2021 at 13:37
  • avy-org-goto-heading-timer kinda meets your needs, I believe you can get what you want by extending avy.
    – nichijou
    Apr 28, 2021 at 13:40
  • Please remove the UPDATE. (1) Remove the part that answers the question - move it to a new answer. (2) Remove the part that asks another question (about yanking: "but unfortunately"...) - move it to a new question. Q&A should be specific and should not evolve (moving goalpost). Thx.
    – Drew
    Apr 28, 2021 at 16:41
  • Apologies. I've now moved the relevant part to a new answer and removed the rest.
    – Pablo
    Apr 28, 2021 at 16:54

2 Answers 2

1

Ordinary interactive search (C-s) is almost exactly what you are describing: C-s * a does what you want, C-s alphaz ditto. Ordinarily, isearch searches through invisible, as well as visible text, so C-s * g would find the second level Gamma even if folded, but you can toggle that behavior: C-s M-s i * g would skip invisible text, so it would end up at the top level Gamma if the AlphaGo heading is folded.

So as long as you start your isearch with C-s M-s i and remember to search for * something, not just something, it will work - most of the time.

Where this fails to meet your expectations is that you need to specify * as part of the search string and if you had * Gamma as part of the text and not a heading, it would find it and get a false positive. In that case, another C-s will find the next instance and you can continue from there.

So not a perfect solution but, IMO, a 99% one: hope it works for you.

3
  • Thank you! I keep forgetting that org files are just plain text, and assumed the functionality I had in mind required something more complex. As you note, it's not 100% perfect, since it will mach e.g. the words immediately following boldfaced text (if that text is visible), but it's more than enough for my purposes.
    – Pablo
    Apr 28, 2021 at 14:10
  • 1
    I created a function based on your solution, which I include in my question above (I'm not sure how to paste code blocks in comments).
    – Pablo
    Apr 28, 2021 at 14:10
  • Anything more than a line or two ends up a mess, so what you did is probably better. And comments can disappear so having all the relevant information in the answer (or in the question) is better IMO.
    – NickD
    Apr 28, 2021 at 14:24
0

NickD's solution is a good enough approximation. Here's a custom function based on it:

(defun ps/isearch-forward-visible-org-heading ()
  (interactive)
  (let ((search-invisible nil))
    (isearch-forward nil 1)
    (isearch-yank-string "* ")))

Your Answer

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

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