This package provides a query language for Org files. It offers two syntax styles: Lisp-like sexps and search engine-like keywords.
It includes three libraries: The
org-ql library is flexible and may be used as a backend for other tools. The libraries
helm-org-ql provide interactive search commands and saved views.
After installation, you can use the commands without additional configuration. Note: The command =helm-org-ql= only works if the package =helm-org= is installed; Helm is not a dependency of this package, so it’s not automatically installed.
To use the functions and macros in your own Elisp code, use libraries
Installing with Quelpa is easy:
- Install quelpa-use-package (which can be installed directly from MELPA).
- Add this form to your init file:
(use-package org-ql :quelpa (org-ql :fetcher github :repo "alphapapa/org-ql"))
These commands and functions are included:
- Showing an agenda-like view:
org-ql-block(agenda block function)
- Showing a tree in a buffer:
- Showing results with Helm:
- Returning a list of matches or acting on them:
Feedback on these APIs is welcome. Eventually, after being tested and polished, they will be considered stable.
Lisp code examples are in examples.org.
Note: This command supports both sexp queries and non-sexp queries.
QUERY and search with
org-ql. Interactively, prompt for these variables:
A list of buffers and/or files to search. Interactively, may also be:
buffer: search the current buffer
all: search all Org buffers
agenda: search buffers returned by the function
- An expression which evaluates to a list of files/buffers
- A space-separated list of file or buffer names
org-super-agenda group set. See variable
NARROW: When non-nil, don’t widen buffers before searching. Interactively, with prefix, leave narrowed.
SORT: One or a list of
org-ql sorting functions, like
Bindings: Keys bound in results buffer.
g: Refresh results. With prefix, prompt to adjust search parameters.
C-x C-s: Save query to variable
org-ql-views(accessible with command
Note: The view buffer is currently put in
org-agenda-mode, which means that some Org Agenda commands work, such as jumping to entries and changing item priorities (without necessarily updating the view). This feature is experimental and not guaranteed to work correctly with all commands. (It works to the extent it does because the appropriate text properties are placed on each item, imitating an Agenda buffer.)
Note: This command uses non-sexp queries.
This command displays matches with Helm. Note: Helm is not a package dependency, so this command only works if the package
helm-org is installed.
C-x C-sin the Helm session to save the results to an
Choose and display a view stored in
Show a sidebar window listing views stored in
org-ql-views for easy access. In the sidebar, press
mouse-1 to show the view at point, and press
c to customize the view at point.
Show items in
FILES from last
DAYS days with timestamps of
TYPE may be
FILES defaults to those returned by the function
(query &key keep-previous (buffer (current-buffer)))
Show a sparse tree for
BUFFER and return number of results. The tree will show the lines where the query matches, and any other context defined in
org-show-context-detail, which see.
QUERY is an
org-ql query sexp (quoted, since this is a function).
BUFFER defaults to the current buffer. When
KEEP-PREVIOUS is non-nil (interactively, with prefix), the outline is not reset to the overview state before finding matches, which allows stacking calls to this command. Runs
org-occur-hook after making the sparse tree.
org-ql query is a lisp form which may contain arbitrary lisp forms, as well as certain built-in predicates. It is byte-compiled into a predicate function which is tested with point on each heading in an Org buffer; when it returns non-nil, the heading matches the query.
- Bare strings like ~”string”~ are automatically converted to
- Standard numeric comparator function symbols (
=) need not be quoted when passed as an argument to predicates which accept them. The resemblance to infix notation is coincidental.
Non-sexp query syntax
org-ql-search also accepts, and the command
helm-org-ql only accepts, an alternative, non-sexp query syntax. The syntax is simple, and a few examples of queries in both syntaxes should suffice. By default, when multiple predicates are used, they are combined with boolean
|Sexp syntax||Non-sexp syntax|
Note that the
priority predicate does not support comparators in the non-sexp syntax, so multiple priorities should be passed instead, as seen in the last example.
Arguments are listed next to predicate names, where applicable.
category (&optional categories)
- Return non-nil if current heading is in one or more of
CATEGORIES(a list of strings).
children (&optional query)
- Return non-nil if current heading has direct child headings. If
QUERY, test it against child headings. This selector may be nested, e.g. to match grandchild headings.
descendants (&optional query)
- Return non-nil if current heading has descendant headings. If
QUERY, test it against descendant headings. This selector may be nested (if you can grok the nesting!).
- Return non-nil if entry’s
TODOkeyword is in
- Return non-nil if entry is a habit.
heading (&rest regexps)
- Return non-nil if current entry’s heading matches all
level (level-or-comparator &optional level)
- Return non-nil if current heading’s outline level matches arguments. The following forms are accepted:
(level NUMBER): Matches if heading level is
(level NUMBER NUMBER): Matches if heading level is equal to or between NUMBERs.
(level COMPARATOR NUMBER): Matches if heading level compares to
outline-path (&rest strings)
- Return non-nil if current node’s outline path matches all of
STRINGS. Each string may appear as a substring in any part of the node’s outline path. For example, the path
(olp "Fruit" "Grape").
outline-path-segment (&rest strings)
- Return non-nil if current node’s outline path matches
STRINGSas a contiguous segment of the outline path. Each string is compared as a substring. For example the path
(olps "Fruit" "Grape")but not
(olps "Food" "Grape").
path (&rest regexps)
- Return non-nil if current heading’s buffer’s filename path matches any of
REGEXPS(regexp strings). Without arguments, return non-nil if buffer is file-backed.
priority (&optional comparator-or-priority priority)
- Return non-nil if current heading has a certain priority.
COMPARATOR-OR-PRIORITYshould be either a comparator function, like
<=, or a priority string, like “A” (in which case (
=will be the comparator). If
COMPARATOR-OR-PRIORITYis a comparator,
PRIORITYshould be a priority string. If both arguments are nil, return non-nil if heading has any defined priority.
property (property &optional value)
- Return non-nil if current entry has
PROPERTY(a string), and optionally
VALUE(a string). Note that property inheritance is currently not enabled for this predicate. If you need to test with inheritance, you could use a custom predicate form, like
(org-entry-get (point) "PROPERTY" 'inherit).
regexp (&rest regexps)
- Return non-nil if current entry matches all of
REGEXPS(regexp strings). Matches against entire entry, from beginning of its heading to the next heading.
src (&key lang regexps)
- Return non-nil if current entry contains an Org Babel source block. If
LANGis non-nil, match blocks of that language. If
REGEXPSis non-nil, require that block’s contents match all regexps.
tags (&optional tags)
- Return non-nil if current heading has one or more of
TAGS(a list of strings). Tests both inherited and local tags.
tags-inherited (&optional tags)
- Return non-nil if current heading’s inherited tags include one or more of
TAGS(a list of strings). If
TAGSis nil, return non-nil if heading has any inherited tags.
tags-local (&optional tags)
- Return non-nil if current heading’s local tags include one or more of
TAGS(a list of strings). If
TAGSis nil, return non-nil if heading has any local tags.
- Return non-nil if current heading includes all of
TAGS. Tests both inherited and local tags.
todo (&optional keywords)
- Return non-nil if current heading is a
KEYWORDS, return non-nil if its keyword is one of
KEYWORDS(a list of strings). When called without arguments, only matches non-done tasks (i.e. does not match keywords in
All of these predicates take optional keyword arguments
:from, return non-nil if entry has a timestamp on or after
:to, return non-nil if entry has a timestamp on or before
:on, return non-nil if entry has a timestamp on date
Argument values should be either a number of days (positive to look forward, or negative to look backward), a
ts struct, or a string parseable by
parse-time-string (the string may omit the time value).
- Return non-nil if current entry has a timestamp in given period. If no arguments are specified, return non-nil if entry has any timestamp.
ts, but only matches active timestamps.
ts, but only matches inactive timestamps.
The following predicates, in addition to the keyword arguments, can also take a single argument, a number, which looks backward or forward a number of days. The number can be negative to invert the direction.
- Return non-nil if current entry was clocked in given period. If no arguments are specified, return non-nil if entry was clocked at any time. Note: Clock entries are expected to be clocked out. Currently clocked entries (i.e. with unclosed timestamp ranges) are ignored.
- Return non-nil if current entry was closed in given period. If no arguments are specified, return non-nil if entry was closed at any time.
- Return non-nil if current entry has deadline in given period. If argument is
auto, return non-nil if entry has deadline within
org-deadline-warning-days. If no arguments are specified, return non-nil if entry has any deadline.
- Return non-nil if current entry has planning timestamp in given period (i.e. its deadline, scheduled, or closed timestamp). If no arguments are specified, return non-nil if entry is scheduled at any time.
- Return non-nil if current entry is scheduled in given period. If no arguments are specified, return non-nil if entry is scheduled at any time.
Functions / Macros
For use as a custom agenda block type in
org-agenda-custom-commands. For example, you could define a custom series command like this, which would list all priority A items tagged
Emacs with to-do keyword
SOMEDAY, followed by the standard agenda view, in a single buffer:
(setq org-agenda-custom-commands '(("ces" "Custom: Agenda and Emacs SOMEDAY [#A] items" ((org-ql-block '(and (todo "SOMEDAY") (tags "Emacs") (priority "A")) ((org-ql-block-header "SOMEDAY :Emacs: High-priority"))) (agenda)))))
Which would be equivalent to a
tags-todo search like this:
(setq org-agenda-custom-commands '(("ces" "Custom: Agenda and Emacs SOMEDAY [#A] items" ((tags-todo "PRIORITY=\"A\"+Emacs/!SOMEDAY") (agenda)))))
org-ql-block version runs in about 1/5th the time.
org-ql-block-header may be bound to a string to use as the block header, otherwise the header is formed automatically.
Listing / acting-on results
(buffers-or-files query &key action narrow sort)
Return items matching
BUFFERS-OR-FILES is a one or a list of files and/or buffers.
QUERY is an
org-ql query sexp (quoted, since this is a function).
ACTION is a function which is called on each matching entry with point at the beginning of its heading. It may be:
elementor nil: Equivalent to
element-with-markers: Equivalent to calling
org-element-headline-parser, with markers added using
org-ql--add-markers. Suitable for formatting with
org-ql-agenda--format-element, allowing insertion into an Org Agenda-like buffer.
- A sexp, which will be byte-compiled into a lambda function.
- A function symbol.
NARROW is non-nil, buffers are not widened (the default is to widen and search the entire buffer).
SORT is either nil, in which case items are not sorted; or one or a list of defined
org-ql sorting methods (
random); or a user-defined comparator function that accepts two items as arguments and returns nil or non-nil.
;; Return list of to-do headings in inbox file with tags and to-do keywords: (org-ql-select "~/org/inbox.org" '(todo) :action #'org-get-heading) ;; => ("TODO Practice leaping tall buildings in a single bound :personal:" ...) ;; Without tags and to-do keywords: (org-ql-select "~/org/inbox.org" '(todo) :action '(org-get-heading t t)) ;; => ("Practice leaping tall buildings in a single bound" ...) ;; Return WAITING heading elements in agenda files: (org-ql-select (org-agenda-files) '(todo "WAITING") :action 'element) ;; => ((headline (:raw-value "Visit the moon" ...) ...) ...) ;; Since `element' is the default for ACTION, it may be omitted: (org-ql-select (org-agenda-files) '(todo "WAITING")) ;; => ((headline (:raw-value "Visit the moon" ...) ...) ...)
(&key (select 'element-with-markers) from where order-by narrow)
org-ql-select, but arguments are named more like a
SELECTcorresponds to the
FROMcorresponds to the
WHEREcorresponds to the
ORDER-BYcorresponds to the
SORT, which see.
NARROWcorresponds to the
;; Return list of to-do headings in inbox file with tags and to-do keywords: (org-ql-query :select #'org-get-heading :from "~/org/inbox.org" :where '(todo)) ;; => ("TODO Practice leaping tall buildings in a single bound :personal:" ...) ;; Without tags and to-do keywords: (org-ql-query :select '(org-get-heading t t) :from "~/org/inbox.org" :where '(todo)) ;; => ("Practice leaping tall buildings in a single bound" ...) ;; Return WAITING heading elements in agenda files: (org-ql-query :select 'element :from (org-agenda-files) :where '(todo "WAITING")) ;; => ((headline (:raw-value "Visit the moon" ...) ...) ...) ;; Since `element' is the default for SELECT, it may be omitted: (org-ql-query :from (org-agenda-files) :where '(todo "WAITING")) ;; => ((headline (:raw-value "Visit the moon" ...) ...) ...)
(buffers-or-files query &key sort narrow markers action)
Expands into a call to
org-ql-select with the same arguments. For convenience, arguments should be unquoted.
Note: Breaking changes may be made before version 1.0, but in the event of major changes, attempts at backward compatibility will be made with obsolescence declarations, translation of arguments, etc. Users who need stability guarantees before 1.0 may choose to use tagged stable releases.
- Negation of terms in plain queries using
!. For example,
tags:space !moonto exclude entries which contain
- Info manual.
org-ql-searchcan search files in
org-directory; customization options are available in the
org-ql-view-refreshcan be called with a prefix argument to adjust search parameters.
helm-org-ql-source, which returns a Helm source that searches given buffers/files with
helm-org-ql. It can be used for custom Helm commands that search certain files.
helm-org-ql-views, which shows one of
org-ql-viewsselected with Helm.
src, which matches Org Babel source blocks.
- Added generic node data cache to speed up recursive, tree-based queries.
org-ql-search, accept symbol as
- In the
org-ql-viewsviews, set timestamps for beginning-of-week to 00:00:00 and end-of-week to 23:59:59.
- Plain quoted-phrases in non-sexp queries.
- Compatibility with Org 9.2. Thanks to Brian Leung.
- Alternative, non-sexp query syntax for commands
helm-org-ql. See documentation.
org-qlqueries. (Thanks to Akira Komamura.)
- Per-buffer, per-heading tag caching, which increases the speed of tags-related queries by 6-7x.
- More tags-related predicates and aliases:
- For inherited tags:
- For heading-local tags:
tags&: Matches all given tags using boolean
AND(rather than boolean
OR, which the
- For inherited tags:
org-ql-block-header, which overrides the default header in
org-ql-viewsmay now be customized in a guided, structured way with the customization UI (e.g.
M-x customize-option RET org-ql-views RET, or press
- Enable more Org Agenda commands in
org-ql-viewbuffers (e.g. setting deadlines and scheduling). (Fixes #35. Thanks to Milan Zamazal and Mikhail Skorzhinskii.)
buffers-filesargument can be a function which returns a list of buffers and/or files.
headingnow accepts multiple regexps, which are matched with boolean
regexpnow matches its regexp arguments with boolean
org-super-agendais now a dependency. This removes the need for awkward code to handle the case where it’s not installed, and makes grouping features always available. Of course, the global minor mode
org-super-agenda-modeis not activated by
org-ql, so no behavior is changed in Org Agenda or
org-ql; it only means that commands like
org-ql-searchwill always provide grouping when called with the appropriate arguments.
org-ql-agenda. Instead, use function
org-ql-search. See also command
headingnow matches only against heading text, i.e. not including tags at the end of the line, to-do keyword, etc.
todonow matches case-sensitively, avoiding non-todo-keyword matches (e.g. a heading which begins
Waiting onwill no longer match for a todo keyword
- Interactive completion in
- Refactored code from file
org-ql-view.el. Function and variable names have been changed accordingly.
- Priority queries could fail to match headings whose to-do keywords had non-alphabetic characters, like
(deadline auto)selector matched entries whose deadlines had a warning period that had not yet been entered (
(descendants)selector matched against parent heading instead of only descendants.
org-ql-selectbut with arguments named more like a SQL query.
- Bare strings like ~”string”~ can be used in queries, which are converted to
(regexp)accepts multiple regexps to test.
org-ql-selectnow also accept a comparator function in their
org-ql-block, which works as an Org Agenda series/composite/block command, usable in custom agenda commands defined in variable
org-agenda-custom-commands. (Inspired by Benson Chu’s config.)
org-ql-agenda--agendaoptionally takes a list of entries as an argument.
ts-i, aliases for
tsnow accepts a
:titleargument, which is displayed in the header.
- Customization group
org-ql-view, which displays views saved to variable
org-ql-views, which can be saved from
org-ql-searchbuffers with command
org-ql-search-save, which is bound to
C-x C-sin view buffers.
org-ql-view-map, active in view buffers displayed by
- Save position when refreshing search buffers.
org-ql-querynow refers to a new function.
org-qlno longer accepts a
:markersargument. Instead, use argument
:action element-with-markers. See function
(todo)no longer matches “done” keywords when used without arguments (i.e. the ones in variable
- Overhauled date/time-based predicates. See documentation for new argument signatures.
(date), replaced by
- Handle date ranges in date-based selectors. (Thanks to Cody Goodman, Samuel W. Flint, and Vikas Rawal.)
- Don’t overwrite bindings in
- Don’t search buffers without headings, and show a message if the user attempts it.
- Don’t search hidden/special buffers.
- Properly accept arbitrary sort functions in
org-ql-select, etc. (Fixes #37. Thanks to Milan Zamazal.)
- Planning-line-related predicates searched too far into entries.
- Add autoloads. (Fixes #36. Thanks to Akira Komamura.)
- Optimizations for some query selectors, e.g.
todo. These can provide a significant improvement for some queries. See benchmarks in notes.org.
- Library ts is now used for parsing and comparing timestamps.
First tagged release.
Comparison with Org Agenda searches
Of course, queries like these can already be written with Org Agenda searches, but the syntax can be complex. For example, this query would be difficult to write in a standard Org Agenda search, because it matches against a to-do keyword and a plain-text search. As described in the advanced searching tutorial, it would require using
org-search-view with a query with specific regular expression syntax, like this:
org-ql-agenda, you would write:
(org-ql-agenda (and (regexp "lisp") (todo "TO-READ")))
This package is used by org-sidebar, which presents a customizable agenda-like view in a sidebar window.