I would like to create a command (C-s C-k or something?) that searches a specific directory every time I call it. It would search files within that directory for a string (may contain spaces) and then present me with a list of results and allow me to jump to said file/result.

I am trying to create a knowledge base of sorts using org files in a directory.

I am currently using a spacemacs setup with a few extra org-mode bits.

  • What's wrong with using Emacs command grep and related commands? – Drew Jun 9 '17 at 19:29
  • The issue is that I do it all the time, so I wanted a short version of it. Deft is doing the job quite nicely. – B0073D Jun 13 '17 at 3:21

The package that you are looking for is called deft.

As a spacemacs user you can just include it as a layer. Just SPC h SPC and type deft to get the instructions on how to use it.

  • This is excellent! Thank you very much! – B0073D Jun 8 '17 at 6:32

The following will ask for a search string and search all files with suffix ".org" in the indicated directory for that string. The resulting grep buffer allows you to click on the filename:lineno part of each matched line and takes you there:

(defun special-grep(s)
  (interactive "MSearch for string: ")
  (lgrep s "*.org" "/path/to/directory/"))

(global-set-key (kbd "C-c C-z") (function special-grep))

I chose a different keybinding that was undefined in my installation. I think "C-s C-k" is a bad idea though, because C-s invokes interactive search and that might cause problems (although the keybinding I chose may be transgressing on the guidelines too - if so, I hope somebody will suggest a better key binding).

  • Hey there, I'm getting an error with this one: grep-expand-template: Wrong type argument: stringp, nil – B0073D Jun 8 '17 at 3:49
  • Strange: it worked fine on one machine but I get the same error you get on another. I'll try to debug it a bit but it may take some time. If you run M-x lgrep and enter the three arguments, do you still get an error? – NickD Jun 8 '17 at 5:01
  • Try adding a slash at the end of the directory path. I added it in the answer and that seems to resolve the problem for me. I hope it works for you too. – NickD Jun 8 '17 at 5:02
  • Hmmm that's not working either but I'm on Windows and some searching seems to indicate various grep/lgrep related issues on that platform... Thank you for trying though. – B0073D Jun 8 '17 at 6:33

It seems you are just looking for a command that always searches in the same directory. All you need to do is bind default-directory around a command, such as grep that searches in the current directory by default.

(defvar my-dir "/THIS/IS/YOUR/DIR/"
  "My directory.")

(defun grep-my-dir ()
  "Grep in `my-dir'."
  (let ((default-directory  my-dir))
    (call-interactively #'grep)))

(global-set-key (kbd "C-o") 'grep-my-dir)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.