2

I use org-mode, and have huge text files with headings (as well as subheadings at different depths). Instead of clicking open each headline to check for its content, I would like a "follow" functionality, made such that when moving the cursor down or up the collapsed headlines in one window (commands C-n or C-p, respectively), the content under the current headline is shown in a second window, splitting the frame vertically or horizontally.

How can I make or invoke such a following mode for showing headline content?

  • 1
    You can use occur to search for ^* (i.e. all headings) and navigate with M-g M-n and M-g M-p. – Juancho Sep 7 '17 at 14:50
  • Thanks @Juancho, when I do this I get an Occur window where I can navigate up and down, but the original headings in the first window does not open by this navigating. How can I make the original headings open? – myotis Sep 7 '17 at 14:55
  • 1
    This sounds a little like a customized agenda for the current file with follow mode turned on. Unfortunately C-c a < m RET F, though it will give you all the headings will not preserve the tree structure/order. – Andrew Swann Sep 8 '17 at 7:50
1

@Juancho correctly suggested to use occur to provide the functionality you desire.

Use add-hook on the occur-mode-find-occurrence-hook.

  • To see how occur-mode-find-occurrence-hook works, try this:

    1. Add the code blocks below into your large org-mode file.

      This code block adds the functionality to occur.

      #+BEGIN_SRC elisp :results none :exports none 
      (defun my/occur-org-cycle ()
        (outline-hide-sublevels 1)
        (org-cycle) 
        (org-switch-to-buffer-other-window "*Occur*")
       )
      
      (add-hook 'occur-mode-find-occurrence-hook 'my/occur-org-cycle)
      #+END_SRC
      

      This code block removes the functionality from occur

      #+BEGIN_SRC elisp :results none :exports none 
       (remove-hook 'occur-mode-find-occurrence-hook 'my/occur-org-cycle)
      #+END_SRC
      
    2. Click inside the first code block and press C-c C-c to add a hook to occur. Answer yes if prompted.

    3. Call occur using M-s o ^*

    4. Navigate your headlines from the new *Occur* buffer and they should expand and collapse as you described.

    5. To revert back to the default behavior, click inside the second code block and execute by pressing C-c C-c. Answer yes if prompted.


To add this functionality permanently, you will need modify and add the code into your emacs configuration file. Unfortunately, I'm still working that step out.

Thanks for asking a great question!


This code was tested using
GNU Emacs 25.2.1 (x86_64-unknown-cygwin, GTK+ Version 3.22.10)
Org mode version 9.0.9

  • Thanks for your work! I will test your code as soon as possible. – myotis Sep 8 '17 at 22:31
  • @myotis my pleasure! Please let me know if you have any problems. I'm going to add the code to my init file because it's a nice feature when you have long org files. – Melioratus Sep 9 '17 at 0:06
  • Hi @Melioratus. Now I have tested your code and I am not able to make it work. I have turned your code on, turned on occur, and got a second window with the occur buffer. But when navigating the headlines in the new buffer, down (C-n) or up (C-p), nothing happens, no headlines are opened, none are collapsed. What have I done wrong? I am using Emacs 25.0.93.1 and Org mode version 9.0.1 – myotis Sep 10 '17 at 23:08
  • @myotis - My guess is you did everything perfectly! Try this: after executing first code block and opening occur, use arrow keys and return key to navigate in occur instead of C-n and C-p. If this works than our key bindings may be different. If it doesn't then please remove the :results none header from block so any execution errors will be displayed and execute the first code block again. Does this help? – Melioratus Sep 11 '17 at 1:13
  • a) the arrow key approach does not help. The return key moves point from a heading in the occur window, to the corresponding heading ing the file. b) the removing of :results none does not help either, it writes out a lot of stuff under a #+RESULTS: heading, but navigating the occur file with arrow keys/return key or C-n/C-p does not automatically open the header i the file, when the corresponding header in the occur buffer is navigatet to. – myotis Sep 11 '17 at 21:30

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.