1

Let's say I want to create a function to search for a string in a set of files (orgmode files). Which of the helm-ag functions should I use and how is the syntax to add the buffers I want to include in the search? Thanks!

Adelita
  • 155
  • 7

1 Answers1

1

You can use helm-do-ag, and provide a list of org files as target, e.g. as follows:

(setq my-search-dirs '("~/.emacs.d" "dir-2"))

(defun my-helm-search-org-files ()
  (interactive)
  (let ((org-files (mapcan (lambda (d)
                             (directory-files d t "\\.org$"))
                           my-search-dirs)))
    (helm-do-ag nil org-files)))

The function searches through org files in all directories in the my-search-dirs variable as set/configured by the first (setq) expression above. If you'd like to also search through subdirectories then use directory-files-recursively instead of directory-files (see its docstring for its usage).

As mentioned already, you could also just call 'helm-do-ag' directory with a list of file as follows:

(helm-do-ag nil list-of-files)
dalanicolai
  • 6,108
  • 7
  • 23
  • Thanks! I tried this out and worked for individual files ((helm-do-ag nil list-of-files). However, for the directory, it prompts me to select the dir even if it is set in the function. – Adelita Dec 08 '22 at 15:21
  • 1
    Did you load the code and do `M-x` `my-helm-search-org-files`? Because then it should ask for a pattern, which is the pattern you'd like to search for. Helm will search already while you are typing some pattern. Of course, you should set/configure the right directories first (in the first line of the example code). – dalanicolai Dec 08 '22 at 15:29
  • 1
    Thanks, I was doing something wrong. I tried again and it works. Then I added the flag --follow to the base command helm-ag and it tracked the symlinks. Thank you! – Adelita Dec 08 '22 at 15:33
  • 1
    The answer [here](https://emacs.stackexchange.com/a/74874/26163) should work similarly, but it is a general answer (not helm/ag specific), and by default it searches only for org headlines (as was the question). I would ask you to try it again (now that you got helm installed). – dalanicolai Dec 08 '22 at 15:42