It should be nice to write yasnippets, which ask for filepath interactively using helm, predefining starting directory should be fine too.


If the list is always the same you should be able to use the yasnippet-choose-value function as described in the manual.

You could also do a snippet like this if you want the search through all of the files like you would when opening any file normally

# -*- mode: snippet -*-
# name: test
# key: test
# --
My File: ${1:$$(unless yas-modified-p (completing-read "File: " 'read-file-name-internal))}

If you have any questions or it doesn't work perfectly just let me know!


A function to use a predefined directory:

(defun my-yas-insert-file-name (dir)
  "Insert absolute path inside yassnipets from given directory"
  (let ((default-directory dir))
    (unless yas-modified-p
      (expand-file-name (completing-read "File: " 'read-file-name-internal)))))
| improve this answer | |
  • Thanks :) . I extended the answer with a small function based on your code with the capability to predefine a directory inside the snippets. – atevm Mar 30 '16 at 10:00

If you want to be able to browse through your directories, you should rather define the snippet like this:

# -*- mode: snippet -*-
# name: test
# key: test
# --
My File: `(insert (file-relative-name (read-file-name "") (file-name-directory (buffer-file-name))))`

In this example, the path is chosen relative to the buffer where the snippet was invoked.

Note that yasnippet will throw a warning because insert is being used. To suppress it, add the following to your init.el:

(add-to-list 'warning-suppress-types '(yasnippet backquote-change))
| improve this answer | |

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.