3

Is it possible to somehow sort of save a view in org-mode?;)

Many times, if the org file is large, I work on several sections of the file and feel I would want to jump between views, like where the cursor is and what headings are folded or unfolded;)

1
  • I am not aware of such a thing. But, between saving and restoring there might be a lot of editing, i.e., deleting, inserting or moving of sections, deleting and moving the region where point was in. This rises the question what to do in such cases. Nevertheless, this would be a nice feature. It just requires some careful planning before implementation. ...And some prediction what the user would expect in the exceptional cases.
    – Tobias
    Feb 24, 2023 at 6:55

1 Answer 1

4

When it comes to saving and restoring specific folding states across all entries in an Org file across Emacs sessions, I am not aware of much, but think this could be a useful feature. Visibility of entries in Org files is controlled by org-fold-core.el, which is well documented. Digging around there might provide all that is needed to implement this.

Org-fold-core uses aliases for the 'invisible text property to hide text. Text properties in general are added ad hoc and not stored in files (Org files would be far from plain text if they were); they are hence not saved across Emacs sessions. Still, they can be copied along with the text of a buffer.

Interestingly, indirect Org buffers do have independent folding states. This means that to have different views (with both different cursor positions and folding states) within one Emacs session you can use indirect buffers. If you are only interested in one tree you can use org-tree-to-indirect-buffer, else you can use clone-indirect-buffer or clone-indirect-buffer-other-window. With a universal prefix argument (C-u) to the latter two you'll be prompted for name. To switch between the 'views' one can now use switch-to-buffer and similar commands.

Moreover, for saving and restoring different cursor positions there are already a lot of tools in Emacs. Start with the most obvious ones:

  • The cursor position in a buffer is called point in Emacs. The mark allows one to keep track of one further position and can be set using set-mark-command (C-SPC). Use C-SPC C-SPC (i.e. the command twice in a row) to set the mark and deactivate it (so that no active region is shown). To move point to mark call the command with a universal prefix (C-u C-SPC). exchange-point-and-mark (C-x C-x) is also useful.
  • The mark-ring (see manual) keeps track of past mark positions. org-mark-ring-push puts the current position into the mark ring and org-mark-ring-goto (which takes a numeric prefix argument) can be used to go to any position in the mark ring.
  • Registers can also be used to store positions. point-to-register and register-to-point are especially useful here.
  • Similary, there are bookmarks, which unlike registers persist automatically between sessions. Commands include bookmark-set and bookmark-jump.
  • Then there are tons of navigation commands like imenu, org-goto, etc. which can quickly jump to specific buffer positions without saving them before. I'd also recommend consult here (it provides nice commands for marks, registers, and bookmarks too).

You can use org-fold-show-context-detail to configure what parts of an Org buffer are shown when you jump to an invisible location or use org-fold-reveal after jumping.

Your Answer

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

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