I have a customized find-file that uses helm sources to help load a file into my buffer. Now, however, I would like to be able to select a file using the same algorithm and store that filename into a variable.

I can use helm-read-file-nameto select a file, but it doesn't use any of the fancy sources or indeed anything very different from standard emacs read-file-name.

How can I get helm-read-file-name to use sources, or alternatively, use the helm function with a different action that will store the match (or matches) into a variable so I can access them?

I've included some code showing what I would like to conceptually have happen (providing a list of sources that helm-read-file-name might use to generate a filename to select).

(setq mhfile
    (helm-read-file-name "Attachment: " :sources '(helm-source-recentf

Obviously I'm going down the wrong path, but I can't figure out a way to override the default action that helm uses either.

  • Helm actions are part of a helm source. It is not clear to me how to use an existing helm source without its actions. Some file related helm sources, such as helm-source-recentf, helm-source-locate and helm-source-findutils, share the same actions, that is helm-type-file-actions, so if you customize it, all these sources will change its actions. Helm also has some generic "actions" via helm-map, such as C-c C-i (helm-copy-to-buffer), it takes effect whenever helm is used, so you can bind your own command on the keymap. – xuchunyang Jul 19 '19 at 1:01
  • It's not clear what you're trying to do, but in case it helps, you can write an action that does what you want and then add that action to SOURCE: (helm-add-action-to-source NAME FN SOURCE) (see also helm-find-files-actions). – jagrg Jul 22 '19 at 15:08
  • @jagrg: Instead of having (helm ....) open a file, I want it to select a file (or put the name of the file in a variable) by /default/, to allow helm-like file completion/discovery/browsing for non-file-opening purposes (such as, attaching a file to an email). – Seth Robertson Jul 23 '19 at 1:03
  • You can select files using helm-find-files so all you would need is to add an action for setting the selected file to some variable. BTW you can also attach files to an email using helm-find-files. – jagrg Jul 23 '19 at 17:33

Your Answer

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

Browse other questions tagged or ask your own question.