If you have queries that you use often, you may want to store them as bookmarks. Bookmark searches are available in the main view (see The main view), header view (see The headers view), and message view (see The message view), using (by default) the key b (M-x mu4e-search-bookmark), or B (M-x mu4e-search-bookmark-edit) which lets you edit the bookmark first.
mu4e provides a number of default bookmarks. Their definition may
be instructive:
(defcustom mu4e-bookmarks
'(( :name "Unread messages"
:query "flag:unread AND NOT flag:trashed"
:key ?u)
( :name "Today's messages"
:query "date:today..now"
:key ?t)
( :name "Last 7 days"
:query "date:7d..now"
:hide-unread t
:key ?w)
( :name "Messages with images"
:query "mime:image/*"
:key ?p))
"List of pre-defined queries that are shown on the main screen.
Each of the list elements is a plist with at least:
`:name' - the name of the query
`:query' - the query expression string or function
`:key' - the shortcut key (single character)
Optionally, you can add the following:
- `:favorite' - if t, monitor the results of this query, and make
it eligible for showing its status in the modeline. At most
one bookmark should have this set to t (otherwise the _first_
bookmark is the implicit favorite). The query for the `:favorite'
item must be unique among `mu4e-bookmarks' and
`mu4e-maildir-shortcuts'.
- `:hide' - if t, the bookmark is hidden from the main-view and
speedbar.
- `:hide-if-no-unread' - if t, the shortcut is hidden from
the main-view if it contains are no unread messages.
You can also use:
- `:hide-unread' - do not show the counts of
unread/total number of matches for the query in the main-view.
This can be useful if a bookmark uses a very slow query.
`:hide-unread' is implied from `:hide'.
Note: for efficiency, queries used to determine the unread/all
counts do not discard duplicate or unreadable messages. Thus, the
numbers shown may differ from the number you get from a normal
query."
:type '(repeat (plist))
:group 'mu4e-bookmarks)
You can replace these or add your own items, by putting in your configuration (~/.emacs) something like:
(add-to-list 'mu4e-bookmarks
'( :name "Big messages"
:query "size:5M..500M"
:key ?b))
This prepends your bookmark to the list, and assigns the key b to it. If
you want to append your bookmark, you can use t as the third
argument to add-to-list.
In the various mu4e views, pressing b lists all the bookmarks
defined in the echo area, with the shortcut key highlighted. So, to invoke the
bookmark we just defined (to get the list of "Big Messages"), all you need to
type is bb.
Instead of using strings, it is also possible to use Lisp expressions as bookmarks. Either the expression evaluates to a query string or the expression is a function taking no argument that returns a query string.
For example, to get all the messages that are at most a week old in your inbox:
(add-to-list 'mu4e-bookmarks
'( :name "Inbox messages in the last 7 days"
:query (lambda () (concat "maildir:/inbox AND date:"
(format-time-string "%Y%m%d.."
(subtract-time (current-time) (days-to-time 7)))))
:key ?w) t)
Another example where the user is prompted how many days old messages should be shown:
(defun my/mu4e-bookmark-num-days-old-query (days-old)
(interactive (list (read-number "Show days old messages: " 7)))
(let ((start-date (subtract-time (current-time) (days-to-time days-old))))
(concat "maildir:/inbox AND date:"
(format-time-string "%Y%m%d.." start-date))))
(add-to-list 'mu4e-bookmarks
`(:name "Inbox messages in the last 7 days"
:query ,(lambda () (call-interactively 'my/mu4e-bookmark-num-days-old-query))
:key ?o) t)
It is defining a function to make the code more readable.
There is also M-x mu4e-search-bookmark-edit (key B), which lets you edit the bookmarked query before invoking it. This can be useful if you have many similar queries, but need to change some parameter. For example, you could have a bookmark ‘"date:today..now AND "’13, which limits any result to today’s messages.