2

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

atevm
  • 928
  • 7
  • 16

2 Answers2

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)))))
atevm
  • 928
  • 7
  • 16
Jules
  • 1,275
  • 7
  • 12
  • 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
  • @npostavs It's true. Thanks for pointing this out! – Timm Mar 03 '18 at 19:32