Running Doom Emacs 2.0.9 on Emacs 27.1.

My current settings are:

(setq org-directory "~/Documents/organize/org-mode")
(setq org-agenda-files (directory-files-recursively "~/Documents/organize/org-mode/agenda"  "^[[:alnum:]].*\\.org\\'"))

org-refile-targets is set to:

((nil :maxlevel . 3)
 (org-agenda-files :maxlevel . 3))

Refile targets seem to come only from agenda files. I would like to be able to refile to any file in org directory.

What changes do I need to make?


The doc string for the variable org-refile-targets says:

  • a specification of the files to be considered, either a list of files, or a symbol whose function or variable value will be used to retrieve a file name or a list of file names. If you use ‘org-agenda-files’ for that, all agenda files will be scanned for targets. Nil means consider headings in the current buffer.

So you can define a variable whose value is a list of the filenames of interest or a function that returns that list and use it in defining a new entry for org-refile-targets.

Take the case of an org directory that contains a bunch of .org files (in a single level) that you want to be targets for refiling. Here's a function that returns a list of them:

(defun ndk/org-refile-candidates ()
     (directory-files "/path/to/some/directory/org" t ".*\\.org$"))

You'll have to tweak the path according to your circumstances.

If the set of files is static, it might be easier to define a variable instead. If you want a recursive traversal, you'll need to modify the function accordingly.

Then you can add this to org-refile-targets with something like this:

(add-to-list 'org-refile-targets '(ndk/org-refile-candidates :maxlevel . 3))

I should note that this is mostly untested, but it should work (ahem).

  • Thanks. Your solution did work, perfectly. The only change I did was to change the function to recursively traverse the directory. Here is how it looks now: (defun ndk/org-refile-candidates () (directory-files-recursively "~/Documents/organize/org-mode/" "^[[:alnum:]].*\\.org\\'")) – deshmukh Apr 7 at 2:30
  • Are the agenda files now included twice then? I don't know if that causes trouble, but if I were you, I'd try to avoid it. – NickD Apr 7 at 11:53
  • I did not understand agenda files included twice. The documentation says: '(directory-files-recursively DIR REGEXP &optional INCLUDE-DIRECTORIES PREDICATE FOLLOW-SYMLINKS)'. I thought we were specifying only the DIR and the REGEXP. – deshmukh Apr 7 at 14:48
  • I meant your setting of org-refile-targets: if you include both the new function and the old org-agenda-files entry, you'll get duplicate entries, because the recursive traversal in the new function will find the agenda files as well. – NickD Apr 7 at 14:59
  • 1
    Set org-refile-targets to include just the nil and the new function entries - leave out the old org-agenda-files entry. But if it doesn't cause difficulties, you might want to leave well enough alone. – NickD Apr 7 at 15:58

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.