5

I have installed the vim-ultisnips plugin and added some files containing snippets to the ~/.vim/snippets/ directory. (note: these files are in the snipmate format as I am in the process of switching to ultisnips from snipmate)

% ls -1 ~/.vim/snippets/                          
apache.snippets
awk.snippets
css.snippets
html.snippets
javascript
markdown.snippets
php.snippets
sh.snippets
_.snippets
snippets.snippets
sql.snippets
vim.snippets

say I am in a vim buffer and I type

fun<TAB>

Ultisnips will ask me to choose between the fun snippets from sh.snippets or the fun snippet from vim.snippets.

My question is how does ultisnips decide which snippet files to load for a given filetype, and is it possible to get a list of which snippet files ultisnips has currently loaded?

1 Answer 1

6

From :help UltiSnips-adding-snippets (section 4.1):

UltiSnips iterates over the snippet definition directories looking for files with names of the following patterns: ft.snippets, ft_*.snippets, or ft/*, where "ft" is the 'filetype' of the current document and "*" is a shell-like wildcard matching any string including the empty string. The following table shows some typical snippet filenames and their associated filetype.

snippet filename         filetype ~
ruby.snippets            ruby
perl.snippets            perl
c.snippets               c
c_my.snippets            c
c/a                      c
c/b.snippets             c
all.snippets             *all
all/a.snippets           *all

So, if your current buffer's filetype is vim, you should only be seeing snippets from vim.snippets (and an all.snippets, if you had one).

There are a few reasons why UltiSnips would load snippets for multiple filetypes at the same time for a single buffer:

  • UltiSnips supports Vim's dotted filetypes, so if your buffer's filetype is set to bash.sh, UltiSnips will load both bash snippets and sh snippets.
  • Snippet files can also extend each other using the extends ft1, ft2, ft3 syntax. C++ snippet files usually start off with extends c, so cpp files will load both c.snippets, and cpp.snippets.
  • Bugs. If your buffer's filetype does not include both vim and sh, and neither vim.snippets, nor sh.snippets extend the other (either directly, or indirectly through yet more filetypes), then it's quite possible you've found a bug.

Regarding listing all loaded files... I'm not sure if that's possible, but you might be able to get something similar. The description for the UltiSnips#SnippetsInCurrentScope function says this (section 3.5.3 from above link):

This function simply returns a vim dictionary with the snippets whose trigger matches the current word. If you need all snippets information of current buffer, you can simply pass 1 (which means all) as first argument of this function, and use a global variable g:current_ulti_dict_info to get the result

There's also a specific example of the latter usage further down the help page.

1
  • 3
    thanks, this is a solid answer. In regard to getting a list of what UltiSnips considers to be "matching" snippet files, I found issuing the command :UltiSnipsEdit! will generate a list of files. This list seems to correspond to what ultisnips considers "matching" snippet files May 5, 2017 at 2:28

Your Answer

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

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