It should be nice to write yasnippets, which ask for filepath interactively using helm, predefining starting directory should be fine too.
Asked
Active
Viewed 479 times
2 Answers
3
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. – atevm Mar 30 '16 at 10:00
0
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))

Timm
- 1,549
- 12
- 23
-
Instead of suppressing the warning, replace ```My file:`(insert ...)` ``` with```My file:`...` ```. See https://emacs.stackexchange.com/questions/24060/yasnippet-truncates-clipboard-contents for more examples. – npostavs Feb 23 '18 at 13:08
-