I would like to call an agenda which is restricted to the current buffer, even if it is not in the agenda files.

I tried to go one way with a custom agenda here, but it feels like maybe peeling the banana from the wrong side.

I know about the < feature in the agenda dispatcher, but maybe there is a shorter way.

How can I call an agenda, which is restricted to the current buffer, using as few keystrokes as possible?

  • 1
    You could temporarily modify org-agenda-files. So that the file corresponding to the buffer is the only thing in there. The only tricky thing is that the buffer might not have a corresponding file, but I imagine you could create a temporary file for it if necessary. Commented Aug 7, 2020 at 14:07
  • @Aquaactress could you elaborate a little more in depth how I could achieve this and how it would be less keystrokes than restricting via < in the agenda dispatcher? Commented Aug 7, 2020 at 14:27
  • 1
    Does this answer your question? in org-mode, how to view TODO items for current buffer only? Commented Aug 8, 2020 at 17:45
  • @itirazimvar - the answer of NickD explains it well, too imho. Commented Aug 8, 2020 at 18:10

1 Answer 1


Here's two implementations. The first one is along the lines that @Aquaactress suggested in a comment:

 (defun my/org-agenda-list-current-buffer ()
  (let ((org-agenda-files (list (buffer-file-name (current-buffer)))))
      (call-interactively #'org-agenda)))

Basically, rebind org-agenda-files temporarily before calling org-agenda. You get the dispatcher, type a and get the daily/weekly/whatever agenda but with just the current buffer contributing. The code is missing error handling: in particular, it should check that the current buffer is associated with a file and the file is in Org mode.

The second implementation bypasses the dispatcher, so it is closer to your "few keystrokes" desideratum:

(defun my/org-agenda-list-current-buffer (&optional arg)
  (interactive "P")
  (org-agenda arg "a" t))

This pre-chooses the daily/weekly/whatever agenda (the "a" argument) and sets the restriction (the t argument) when calling org-agenda. It uses the same "P" interactive spec that org-agenda uses in order to deal with a prefix argument. The error handling comments above apply to this case too.

You can then bind whichever function you prefer to a key that will allow you to call it. It's probably best to use the Org mode map for that, since it does not make sense to call either of the functions above in a non-Org mode buffer. That ameliorates a bit the need for error handling: the key is only bound in Org mode buffers - but you will get an error if the buffer is not associated with a file in either implementation. If you are using GUI emacs, you probably have access to function keys with modifiers, so I'm using the C-<f12> key, since it was undefined in my case - which key to use is up to you though: I just wanted to provide an example:

(define-key org-mode-map (kbd "<C-f12>") #'my/org-agenda-list-current-buffer)

EDIT: In reply to how to restrict to subtree, all you need to do is to change the t argument in the second implementation. That argument can be a symbol with any of the following values: buffer, subtree and region. The call

(org-agenda arg "a" t)

is equivalent to

(org-agenda arg "a" 'buffer)

so you can restrict to e.g. the current subtree with

(org-agenda arg "a" 'subtree)

PS. I hope this works, but I have not tested it, so there may be problems. I'll do some testing at some point to verify.

  • 2
    You beat me to the punch. I was about to post an answer. :) I like the dispatcher bypassing idea. Commented Aug 7, 2020 at 14:45
  • Nice :-) Can one pass a value to org-agenda to restrict to the current subtree, too? I tried to look at the code in org-agenda.el but its over my power level to understand how it works. Commented Aug 7, 2020 at 15:03
  • I just saw the edit. This will propably keep me busy for at least the next weeek. A true emacs-moment. Thanks! Commented Sep 22, 2020 at 16:01
  • Ah sorry: I thought the OP gets notified when an edit happens to an answer to one of their questions (although I may be wrong in that expectation) - did that not happen?
    – NickD
    Commented Sep 22, 2020 at 18:49

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.