emacsagemacs-helm

Interactive Elisp Function to Directly Search File Names with `emacs-helm-ag` (-g option)


I use emacs-helm-ag to search file content and file names in a project. My emacs-helm-ag configuration is:

; Helm Ag mode
(add-to-list 'load-path "~/.emacs.d/emacs-helm-ag")
(require 'helm-ag)
(custom-set-variables
    '(helm-ag-base-command "ag --nocolor --nogroup")
    '(helm-ag-command-option "--hidden --ignore *~ --ignore .git --ignore .idea"))
(global-set-key (kbd "M-s s") 'helm-do-ag-project-root)

To search file content (a word in all project files) I press M-s s and type the word to search.

To search file name (a file in a list of all files of a project) I press M-s s then I have to type -g (I would like to automate this step) and then I type the file name to search.

Question. Please could you help me to come up with an elisp interactive function that directly searches for file names in a project by automatically inserting -g command line option (and preferably reuses helm-do-ag-project-root from emacs-helm-ag package) and only requires the file name to be typed by the end user. I would like to have an interactive function to search file names in a project and be able to call the function with a different key binding. Something similar to:

(global-set-key (kbd "M-s f") 'helm-do-ag-project-root-file-names)

Solution

  • Based on a suggestion from DasEwigeLicht on Reddit I've created a function which searches for file names in a project using ag -g <pattern> as required in my question. The function is:

    (defun helm-do-ag-project-root-search-file-names ()
        "Searches file names in a project using ag -g <pattern> command line tool"
        (interactive)
        (let ((helm-ag-command-option (concat helm-ag-command-option " -g")))
            (helm-do-ag-project-root)))
    
    (global-set-key (kbd "M-s f") 'helm-do-ag-project-root-search-file-names)
    

    Thank you very much DasEwigeLicht for a helpful suggestion!