It should be nice to write yasnippets, which ask for filepath interactively using helm, predefining starting directory should be fine too.
2 Answers
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!
EDIT:
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)))))
-
Thanks :) . I extended the answer with a small function based on your code with the capability to predefine a directory inside the snippets.– atevmCommented Mar 30, 2016 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))
-
Instead of suppressing the warning, replace
My file:`(insert ...)`
withMy file:`...`
. See emacs.stackexchange.com/questions/24060/… for more examples.– npostavsCommented Feb 23, 2018 at 13:08 -