14

I use org-mode to take notes. Currently I have a root directory, say, "Notes", and there are several subdirectories inside it, for example, "Linux", "Programming", etc. The notes taken using org-mode are stored under those subdirectories.

I was wondering whether there is an available set of settings so that I can search these notes(which are all org-mode files) very quickly. I know there is deft, but it is not designed for org-mode files. Actually I also tried helm-projectile but it's far from satisfactory.

To be more precise, what I want to achieve is that I can search these files using tags, headlines, or other features which are org-mode-related, maybe using some predefined syntaxes. And if possible, I would prefer something like helm. If there is no such an extension, will it be worth to implement such an extension? I thought deft+helm-projectile might be a good start point.

UPDATE: I tried both methods, directory-local variables(as @itsjeyd suggests in the answer) and org-agenda-custom-commands, and I found the latter one seemed more convenient. So here is my setting:

(setq org-agenda-custom-commands '(("n" . "Search in notes") ("nt" "Note tags search" tags "" ((org-agenda-files (file-expand-wildcards "path/to/Notes/*/*.org")))) ("ns" "Note full text search" search "" ((org-agenda-files (file-expand-wildcards "path/to/Notes/*/*.org")))))) 
3
  • Have you tried?: M-x org-search-view Here is the link to the documentation: orgmode.org/manual/Search-view.html Commented Mar 14, 2015 at 6:51
  • Afaik you can set up Deft to use org files by default with (setq deft-extension "org") and (setq deft-text-mode 'org-mode). Commented Mar 14, 2015 at 7:54
  • @ mutbuerger The settings are what I currently use. But Deft itself has no org-related search function Commented Mar 14, 2015 at 17:34

3 Answers 3

11

Org itself provides a facility for filtering/searching information stored in .org files in various ways. It is called the "Agenda". The org-mode manual provides extensive information on how to use it (check the link above or do C-h i m Org Mode RET from within Emacs and navigate to the "Agenda Views" chapter), so it does not make sense to repeat all of this information here. The introductory text from the manual summarizes what you can do with it (emphasis mine):

Org can select items based on various criteria and display them in a separate buffer. Seven different view types are provided:

  • an agenda that is like a calendar and shows information for specific dates,
  • a TODO list that covers all unfinished action items,
  • a match view, showings headlines based on the tags, properties, and TODO state associated with them,
  • a timeline view that shows all events in a single Org file, in time-sorted view,
  • a text search view that shows all entries from multiple files that contain specified keywords,
  • a stuck projects view showing projects that currently don't move along, and
  • custom views that are special searches and combinations of different views.

The only thing you need to do to start using the agenda for searching your notes is add any files you want to search to org-agenda-files:

The information to be shown is normally collected from all agenda files, the files listed in the variable org-agenda-files. If a directory is part of this list, all files with the extension .org in this directory will be part of the list.

So in your case you'll want to add something like this to your init-file:

(setq org-agenda-files '("/path/to/Notes/Linux" "/path/to/Notes/Programming")) 

For lots of examples and practical advice I also recommend you check out the article on Advanced Searching on Worg.

[UPDATE] Limiting search to specific file sets

As mentioned in the comments, OP wants to be able to restrict searches to specific sets of files. In technical terms, this means setting org-agenda-files to different values depending on context. This can be achieved using directory-local variables:

The usual way to define directory-local variables is to put a file named .dir-locals.el in a directory. Whenever Emacs visits any file in that directory or any of its subdirectories, it will apply the directory-local variables specified in .dir-locals.el, [...].

For example, let's say that

  • while visiting files stored in Notes/Linux you want to use the agenda to only search .org files stored in Notes/Linux.

  • while visiting files stored in Notes/Programming, you want to use the agenda to only search .org files stored in Notes/Programming.

Here's what to do to enable this behavior:

  1. Add a .dir-locals.el file with the following content to both Notes/Linux and Notes/Programming:

     ((nil . ((org-agenda-files . ("."))))) 

    This sets the list of org-agenda-files to include all files in the current directory.

  2. Add the following code to your init-file:

     (add-hook 'org-agenda-mode-hook #'hack-dir-local-variables-non-file-buffer) 

    This step is necessary to ensure that *Org Agenda* (a non-file buffer) respects your directory-local settings for org-agenda-files.

Next time you visit a file stored in Notes/Linux or Notes/Programming, Emacs will ask you whether you would like to apply the settings stored in .dir-locals.el. Hit ! to tell Emacs to apply the settings and mark them as safe for future sessions (you will not be prompted again for files in the same directory).

10
  • Maybe I didn't indicate clearly in the question...I have used agenda, but the problem is that I have other org files showing my schedules and TODOs for my agenda, and I don't want my notes to mess up the agenda view. Maybe what I want is something like the search function in Evernote since I want to use Emacs as my note taking application. Do you know Deft? It has a good search interface but is not org-related Commented Mar 14, 2015 at 17:33
  • @cutejumper I'm not sure I follow. What do you mean by "messing up the agenda view"? If the headlines in your Linux/Programming/etc. notes do not have TODO states associated with them, they shouldn't show up in your agenda when you list your TODO items. Commented Mar 14, 2015 at 20:34
  • Well, this way might be clearer: I actually want to have separate sets of agenda files. My understanding for agenda of the current org-mode is that it is more like a "global" set while I want a search interface for a certain "project"(kind of like projectile) so that when I do the search I only see the relevant contents in a "project". Do I overlook any functionality that agenda actually offers? Commented Mar 14, 2015 at 21:53
  • You can locally set org-agenda-files to just the files (or directory) your interested in inside an agenda template or use C-c a < to restrict the agenda search to only the current file. Commented Mar 15, 2015 at 0:08
  • 1
    @cutejumper I don't think the agenda buffer supports this use case out of the box. However, the command @BrianZ mentions in his answer (helm-org-agenda-files-headings) does exactly what you want (for headings). Note that you will need to set up directory-local variables in order to make it possible to run helm-org-agenda-files-headings on specific file sets. Commented Mar 15, 2015 at 9:34
3

There are a few org related functions in helm. I think helm-org-agenda-files-headings is basically what you have in mind. If it's not already available for you in emacs, try getting helm-org.el off Github.

0

The variable org-agenda-text-search-extra-files is similar to org-agenda-files, but is only used for text searches (C-c a s and C-c a //org-occur-in-agenda-files), not for Agenda views.

It requires a list of files as its argument:

(setq org-agenda-text-search-extra-files ("~/.Notes/Notes1.org" "~/.Notes/Notes1.org")) 

However, unlike org-agenda-files, it cannot as its argument take a file that contains a list of files (one per line). This can be circumvented by turning such a file into a list:

(setq org-agenda-text-search-extra-files (s-split "\n" (f-read "~/.emacs.d/orgmode_non-agenda_files.el") t)) 

(nicked here).

This works, but unfortunately I have so many notes (14Mb in 732 files) that C-c a s/S takes forever (whereas grep needs 0.08s). C-c a / is slow the first time, but fast thereafter. The time required to create an Agenda view seems unaffected by this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.