Make quick notes with deft

I tend to organise my notes into files related to a specific topic or project, so I find org-mode with org-capture very useful to add quick notes to a specific file, like adding a TODO item or an idea for a blog post (I will post lots more about org-mode in due course, but there is an overview on my tutorial page). However, if I want to make a more general note that doesn’t fit into a category (say notes from a meeting), then deft is a nice alternative.

This post is a short introduction to deft, and I will describe some tweaks I have made in subsequent posts.

Deft is an emacs package (available through package-list-packages) that lets you quickly create, search and add to files containing notes. It is inspired by the Mac programme Notational Velocity.

My use case is that I launch deft, start typing a search string to very quickly find the file I want to add notes to, or create a new file on the fly, and then add my notes, quit deft and be back to where I was before.

Deft works simply by having all of your note files in a single directory, and the files themselves are simple text (org-mode if you like) files that can be viewed and edited anywhere else as well as via deft. Deft is really just a nice quick interface for finding/creating and opening the right file for editing.

To use deft, launch it with M-x deft and you will see a list of the files in your deft directory with short summaries. Start typing a search string and the list will dynamically filter down to files that match the string in their file name or body text. Use arrow keys to move up and down through the list of files and hit return to open that file for editing.

If no files match the search string then hitting enter creates a new file with a name taken from the search string. If you want to create a new file with a specific name, use C-c C-n.

You can also rename C-c C-r and delete C-c C-d files from the deft buffer.

Use C-c C-q to quit deft.

This is best illustrated with a couple of examples. In the first example I launch deft and type a search string to find an existing file and then open that file for editing. In this case I am looking for the file about an open day in 2014.

deft1.gif

In the second example I launch deft and type a search string that doesn’t match any files and so create a new file with a name based on the search string. In this case, my search string “open day 2015” doesn’t match anything so deft creates a new file for me.

deft2.gif

Once you have installed deft, add the following to your emacs config file to get the behaviour described above

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; deft                                                                   ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'deft)
(setq deft-directory "~/docs/deft")
(setq deft-extensions '("org"))
(setq deft-default-extension "org")
(setq deft-text-mode 'org-mode)
(setq deft-use-filename-as-title t)
(setq deft-use-filter-string-for-filename t)
(setq deft-auto-save-interval 0)
;;key to launch deft
(global-set-key (kbd "C-c d") 'deft)

With this setup, my deft files are all stored in ~/docs/deft/ and have .org file extensions, and deft will open them for editing in org-mode.

The option (setq deft-use-filename-as-title t) tells deft to use the search string to generate the filename for a new file if the string does not match an existing file.

Those are the basics of deft, but stay tuned for some tweaks that (for me at least) make the experience even smoother.

Update

An update to deft introduced a new way to tell deft to use the filter to create the filename

(setq deft-use-filter-string-for-filename t)

I have added that to the deft setup code above. This solves an issue raised in the comments below.

  • Bill Raynor

    I am using GNU Emacs 24.4.1 on OS X, and your

    (require ‘deft)

    does not work. I get the message ” Cannot open load file, no such file or directory, deft”

    However M-x deft works fine. What have I misconfigured?

    • Ben Maughan

      I’m afraid I’m not sure! Did you try restarting emacs after installing the deft package? If you comment out the (require ‘deft) line, does the rest of the code work?

      • Bill Raynor

        1. Yes, I habitually restart emacs after modifying my .emacs file
        2. If I comment out the (require `deft) statement, the rest of the statements work, EXCEPT for the deft-use-filename-as-title. Deft insists on calling them deft-1, deft-2 etc. the C-c C-r sequence does work, though.

        Thanks,
        Bill

        • Ben Maughan

          I’m not sure why you are seeing this behaviour. The only things I can think to suggest are to try comment out your whole .emacs file except for the deft stuff and see if that works and also look at the value of load path (using C-h v load-path) which lists the places emacs looks for packages. If you load-path has multiple directories for packages this could cause odd behaviour – e.g. the fact that you are not picking up the new version of auctex implies the directory with the old version is also in your load-path.

          • Bill Raynor

            The update worked fine, thank you.

            Am I supposed to use .emacs or a file in ~/.emacs.d/?
            I’ve been looking through the book “Mastering Emacs” and it mentions that things get defined in different orders. I am wondering if .emacs gets processed before the load-path is (completely) set. That would explain why M-x deft works but not require.

          • Ben Maughan

            I would suggest using a file in ~/.emacs.d/ – in fact, if you use prelude, you would put it in ~/.emacs.d/personal/

            See
            http://pragmaticemacs.com/installing-and-setting-up-emacs/
            for my recommended setup

          • Bill Raynor

            Thanks Ben,
            1. if I stick a (message “%S” load-path) in my .emacs file, deft is not yet on the load-path.
            2. After everything finishes, the same command in *scratch* shows deft in the load-path.

            Where can I find the process that emacs goes through in building the load-path? That would be an interesting blog post (to me, at least)

            I’m using Vincent Goulet’s “Emacs for OS X Modified” build. It includes ESS, AUCTEX, and ORG preconfigured.

        • Ben Maughan

          I just found my deft setup was behaving the same way as yours after I updated it. The fix is in the update above. I hope that works for you too.

      • Bill Raynor

        I should also note that package-user-dir is set to ~/.emacs.d/elpa. I update AUCTex at the same time I installed deft, and I see that emacs is loading the older version (11.87 versus 11.88.6 in the elpa directory)

  • Bob Uhl

    Looks like deft-extension is now deft-extensions, with the first being the default, and deft-text-mode is no longer necessary.