I want to have some specific snippets for each file that I am editing. Like, I want to type "ppp" just for "proper point groups", or "iff" for "if and only if".

To be more specific, I want an additional unique set of snippets for each file I am editing. For example, when I write about group theory, I will want things like above to be shorthanded. But when I write about geometry, I will want "hom" stands for "\operatorname{\mathcal{Hom}}", etc.

So far I succeeded to add a absolute path to UltiSnips search directory, like the following in my VIMRC:

let g:UltiSnipsSnippetDirectories=['UltiSnips','C:\Users\xxx\Drafts\UltiSnips']

I tried to add an relative path like

let g:UltiSnipsSnippetDirectories=['UltiSnips','.\UltiSnips']

But UltiSnips just ignores it. Any ideas?

Or, can I add a modeline to get the current directory, and then set the variable g:UltiSnipsSnippetDirectories?

FYI.: I am on windows 10.


1 Answer 1


First of all :h UltiSnips-snippet-search-path tells us:

Snippet definition files are stored in snippet directories. A snippet directory must be a subdirectory of a directory defined in the 'runtimepath' option.

Which makes me think that you can not create snippets directories anywhere since that would mean constantly add your current path to runtimepath which doesn't sound like a good idea.

Thus I would advise the following solution: Use one directory containing your customs snippets, in this directory create snippets for "custom" filetype and add these files types to the files you want to edit (As both Vim and Ultisnips handle multiple filetypes that shouldn't cause a problem).

For example:

  • Use your current snippets directory 'C:\Users\xxx\Drafts\UltiSnips'
  • In it, put files named custom1.snippets containing the snippets you want to use in the files which will use the ppp snippets.
  • Then you can use :UltiSnipsAddFiletypes (here is the doc) to select the snippets used in your file.

Edit (After the answer was accepted)

Another option could be the following: I didn't test it and it is based on my understanding of the doc so it may not work:

The same doc topic also says:

If you keep your snippets in a .vim subdirectory called "mycoolsnippets" and you want to make use of the default snippets that come with UltiSnips, add the following to your vimrc file.

let g:UltiSnipsSnippetDirectories=["UltiSnips", "mycoolsnippets"]

If you do not want to use the third party snippets that come with plugins, define the variable accordingly:

let g:UltiSnipsSnippetDirectories=["mycoolsnippets"]

You can also redefine the search path on a buffer by buffer basis by setting the variable b:UltiSnipsSnippetDirectories. This variable takes precedence over the global variable.

So maybe (emphasis on maybe) you could:

  • Create several snippets directories project1, project2 at the same level as the UltiSnips directory.
  • Each of these directories would contain differents snippets files for different filetypes in different projects.
  • Then based on the project your file belongs to you could let b:UltiSnipsSnippetDirectories=["project1"]

I think that this way you wouldn't need to play with the filetypes nor the runtimepath You simply would need to modify b:UltiSnipsSnippetDirectories which should be done manually for each buffer because let is prohibited on modelines.

Note That as I said previously I didn't test that so I may be missing something.

  • Er... proper point group is not a filetype, is just a phrase that I want to abbreviate using UltiSnips. Then, can I use modeline to get the current directory and set the value for g:UltiSnipsSnippetDirectories. How do I do that?
    – taper
    Nov 14, 2016 at 11:33
  • @taper From UltiSnips-snippet-search-path Snippet definition files are stored in snippet directories. A snippet directory must be a subdirectory of a directory defined in the 'runtimepath' option.. I don't think it is possible to use any project directory. That's why I suggested that you define filetypes grouping you custom snippets and that you set the desired filetype to the files you edit.
    – statox
    Nov 14, 2016 at 12:13
  • I get your point. That's a clever way to circumvent that restriction. Thanks!
    – taper
    Nov 14, 2016 at 12:24
  • @taper Nice, I'll edit my answer to make it clearer
    – statox
    Nov 14, 2016 at 12:25
  • let is prohibited in modeline. So, to make the second suggestion work one has to extend the built-in modeline by plugins like let-modeline, or use a local .vimrc file. Both are, actually, a bit troublesome. :P
    – taper
    Nov 14, 2016 at 13:36

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.