I'm using a command that reverts a diff chunk in a buffer (linked question), however I don't know if the buffer is newly opened - or if an existing buffer is used.

While I could make a list of all buffers, then check if that buffer is in the original list, is there a better way to know if the buffer from a function is newly created or not?

  • 1
    Yes, I would do what you describe. (buffer-list gives you a list of all buffers.)
    – Drew
    Commented Jan 2, 2020 at 5:47

1 Answer 1


"Newly opened" can only mean relative to a state when the buffer didn't exist. Every time prior to when it came into being it didn't exist.

So yes, you need to take a snapshot of the list of existing buffers at some point prior to creating the new buffer, in order to later check whether the buffer is "new" - compared to that prior snapshot moment.

Save the value of buffer-list in a variable (e.g. let-bound), and then, later, check whether the buffer in question is in that previous snapshot. If it is, then it's not new; if it isn't, then it's new.

If you're only interested in certain kinds of buffers then you can filter buffer-list, to make your lookup quicker.

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.