2

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

0

2 Answers 2

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)))))
1
  • Thanks :) . I extended the answer with a small function based on your code with the capability to predefine a directory inside the snippets.
    – atevm
    Commented Mar 30, 2016 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))
2
  • Instead of suppressing the warning, replace My file:`(insert ...)` withMy file:`...` . See emacs.stackexchange.com/questions/24060/… for more examples.
    – npostavs
    Commented Feb 23, 2018 at 13:08
  • @npostavs It's true. Thanks for pointing this out!
    – Timm
    Commented Mar 3, 2018 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.