4

Using UltiSnips, I'd like to add a set of snippets just for one file or for a set of files in the same directory, maybe using a :vim modeline.

I tried to put

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

in my .vimrc and then adding a all.snippets file in the same directory of the file (by the way, it is a LaTeX file — I tried also with tex_local.snippets file) but it doesn't work.

The rational is the following: I am writing a beamer presentation and I have a repeating pattern for several frames --- one that is two diagrams side by side for example, another a figure and a plot, and so on.

So I have defined snippets that create the 3 or 4 patterns I like for this presentation, and happily added them to my .vimtex/UltiSnips/tex_local.snippets file --- and all is fine. But these snippets are really only useful for this file, and at the end of the day I do not want to keep adding very local things to the generic snippets definitions...

  • 1
    I don't think it is possible, but maybe you are missing other ways of solving your problem. Can you provide more context, explain what would be the benefit of restricting the snippets to a set of files? – mMontu Feb 4 '16 at 12:28
  • 2
    Perhaps using anonymous snippets might solve the issue: github.com/SirVer/ultisnips/blob/master/doc/UltiSnips.txt#L446 and define them on an autocmd event that triggers only for your filename. – VanLaser Feb 4 '16 at 12:48
  • @VanLaser yes, possible, but writing the anonymous snippet is less straightforward, and then I should add the autocmd to the .vimrc for every file... and if I change the name? Or copy it to another? I really hoped in something that could be almost transparent, as adding the directory containing the file to the ultisnips search paths... – Rmano Feb 4 '16 at 14:12
  • 1
    You can use a different file that contains the anonymous snippets and source it in your vimrc. The "per-file" autocmd events could be in vimrc or in that file. Otherwise, feel free to add that functionality ;) – VanLaser Feb 4 '16 at 15:21
  • @VanLaser ;-) my vim programming is, well... quite low. But if I have time I will look at it; for the time being I have not been able to find where g:UltiSnipsSnippetDirectories is used in the sources (grep g:UltiSnipsSnippetDirectories **/* gives just the place where it is defined or supplied as default...) – Rmano Feb 4 '16 at 16:12
3

If your template expander plugin doesn't support snippets local to the current buffer, i.e. if b:UltiSnipsSnippetDirectories is not understood in your case, you can

  • ask the maintainers to use this variable if defined, or the global one otherwise -- this is the best course of action
  • or you can use another snippet engine that supports local snippets
  • or you can register an autocommand so that when you enter the window/buffer where your file is, g:UltiSnipsSnippetDirectories is set accordingly, and it's reset when you leave this buffer.

i.e. something like (untested):

function! s:AddSnippets()
   let b:restore = get(g:, 'UltiSnipsSnippetDirectories', [])
   let g:UltiSnipsSnippetDirectories = b:restore + ['yourNewPath']
endfunction

function s:RestoreSnippets()
   let g:UltiSnipsSnippetDirectories = b:restore
endfunction

augroup DedicatedSnippets
  au !
  au BufEnter /path/to/your/file call s:AddSnippets()
  au BufLeave /path/to/your/file call s:RestoreSnippets()
augroup END
  • Thanks and +1, but I really look forward to something I can really add just to the file in question. Something like a modeline :ReadSnippeAlsoFrom(./extra.snippets) or somethign similar. I will see if I can dig into it... – Rmano Feb 4 '16 at 16:15
  • You can adapt my code to have a function that defines the DedicatedSnippets group for the list of files you pass to :ReadSnippeAlsoFrom. BTW, instead of /path/to/your/file, you can simply pass a dirname, and have the two functions compare the current file/buffername to the list of files. – Luc Hermitte Feb 4 '16 at 16:59
  • 1
    Note (bis): if you prefer having only a single function/command that you call on the current buffer, you can define the autocommand with <buffer> (instead of a filename), and don't forget, that calling the ReadSnippeAlsoFrom function should also call s:AddSnippets) – Luc Hermitte Feb 4 '16 at 17:01
  • 1
    I did not test this, but I suspect that the problem would be the same --- if I say let g:UltiSnipsSnippetDirectories=["UltiSnips", "."] it doesn't work, and I should put the full path always... mmhhh. Maybe time for a feature request. – Rmano Feb 6 '16 at 8:50
  • 1
    You can get the full path with expand() or fnamemodify(). I do this all the time with my template expander plugin -- mu-template. But as with my other plugins, it supports project/buffer local settings. – Luc Hermitte Feb 6 '16 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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