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!
1 Answer
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)
-
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.– AdelitaCommented Dec 8, 2022 at 15:21
-
1Did 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). Commented Dec 8, 2022 at 15:29 -
1Thanks, 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!– AdelitaCommented Dec 8, 2022 at 15:33
-
1The answer here 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). Commented Dec 8, 2022 at 15:42