I'm interested in hiding comments in code or structured text, but displaying them in another window; i.e. same buffer, two windows side-by-side, one showing the main text (code, or content), but hiding or minimizing comments, and the other window showing comments, but hiding or minimizing everything else.

Importantly these should track each other, similar to how a git merge application appears and functions. This is similar to, but not precisely the same as collapsible sections that you commonly see in an IDE. I think this could be useful when writing code, though in this case the problem that prompted me to originally post this question as follows:

I'm writing fiction, loosely in markdown, though the exact markup is not so important. The important thing is that in addition to my fiction text, I have notes formatted as comments, and "commented out" passages of fiction that I'd like to retain in place, at least temporarily, for one reason or another. The problem is that these can interfere with my reading of the main text. What I'd like is something akin to Word's margin comments, but presented much like a git merge application, but being a single file buffer.

Before going through the trouble of writing an emacs mode for this, I'm wondering if there are any existing packages that might do something like this.


1 Answer 1


(I'll post this answer because it's related, even though it doesn't fully respond to what you want. Hope it helps.)

You can use library hide-comnt.el to hide all of the comments in a buffer or the active region.

  • Command hide/show-comments hides comments. With a prefix arg it shows comments again.

  • Command hide/show-comments-toggle toggles hiding/showing comments.

  • Non-nil option hide-whitespace-before-comment-flag hides also any whitespace preceding a comment.

I believe it's impossible to show the same buffer, or parts of it, in separate windows, but with different things visible for the same stretch of text. You can use indirect buffers to show different parts of a buffer in different windows, but, for example, if you hide the comments in one window then they're hidden in all. (That is, assuming that "hiding" means using the visibility property.)

To do something like what you request, you could perhaps create a separate buffer or buffers for the comments. But if you want any identity sync (what you called "tracking") between the two (comment and noncomment) then you'd have to implement that explicitly. Truly separate buffers means that a change in one isn't reflected in the other.

  • 1
    Interesting. I accepted your answer, despite the limitations you note. In particular, the information about the difficulty/possibility of doing what I wanted is valuable, since I'm unfamiliar with the nuts and bolts of Emacs internals.
    – Jacob Lee
    Commented Feb 17, 2022 at 1:18

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.