Tweaking deft: improving navigation

Following up on my earlier posts on deft, I had another small tweak I wanted to make, that also illustrates a useful technique for customised Emacs.

In the deft buffer, each entry takes two lines so I have to hit the up/down arrow key twice to move between them. To simplify this, I tweaked the keybindings in deft-mode to make the arrow keys jump 2 lines instead of 1, but only when deft-mode is active:

;;override normal settings to jump 2 lines as deft has time stamps on second line
(define-key deft-mode-map (kbd "<down>")
  (lambda () (interactive) (next-line 2)))
(define-key deft-mode-map (kbd "<up>")
  (lambda () (interactive) (next-line -2)))

Here, lambda is the name used for anonymous functions in emacs lisp, which are useful in cases like this where I want to make a simple function that I won’t need to use anywhere else. The function is very simple, it just calls the normal line movement command next-line with an argument of 2 or -2 to skip up or down by 2 lines. We then define the arrow keys to call this function, but only in deft-mode-map, which contains the keybindings for deft-mode.

Touch-typists (which, to my shame I am not) generally prefer to use C-n and C-p to move up and down lines, so those could be used instead here.


Commenter Kaushal Modi pointed out that I should not have been seeing the entries in the deft buffer taking up more than one line. It turns out the problem was that I had global-visual-line-mode switched on on my emacs config file which caused the problem.

This means the above changes are redundant, but I’ll leave them up as an example of adding keybindings to a specific mode.

  • Kaushal Modi

    For me, I always see single line entries as shown here:

    The entries remaining single-lined even when I shrunk down the *Deft* buffer in width. As I reduced the width, the note summary got truncated gradually but the time stamp never jumped to the next line.

    Probably something in your config is doing this?

    • Ben Maughan

      Thanks – you were right – I had global-visual-line-mode on, which caused the problem.

      • Kaushal Modi

        Glad to help! The hard-coded navigation by 2 line jumps did not feel right šŸ™‚

  • DHebbes

    Thanks for the deft tips and ongoing Emacs love.
    Wanted to mention, are you aware of the Deft-turbo package, it supports directory recursion and multiple file types… might have something you see interesting:

    • Kaushal Modi

      I emailed the deft author, Jason Blevins, to put deft on github so that people can collaborate on that project and he was kind enough to do so!

      It should be now easy to contribute to the package and request features/bug fixes in the github Issue Manager.

      • DHebbes

        Great thanks for mailing him, I have some features I wish to add.

  • How did you go about disabling global-visual-line-mode when you want to use deft? Just curious, in the hook, or was it around advice on `global-visual-line-mode’?

    • Ben Maughan

      I just used the deft-mode-hook as you suspected:

      (add-hook ‘deft-mode-hook (lambda () (visual-line-mode 0)))