Can we use multiple snippet files for given filetype in vim?
Asked Answered
C

5

10

When using snipmate + vim, is it possible to have many snippet files load for a given language/filetype?

Ex: snipmate comes with javascript.snippets I've also loaded the mootools snippets and added this to my vimrc: autocmd FileType javascript set ft=javascript.mootools

Now I can use

  • the stock js snippets
  • the mootools snippets

I want to add a 3rd set of snippets, javascript.myCustomSnippets, that will also load for the filetype javascript.

When I try to add my custom snippets using something like this autocmd FileType javascript set ft=javascript.myCustomSnippets it overwrites/disables the mootools snippets, however the default javascript snippets continue to work.

How do I accomplish this, or is it possible?

ps: I know I could just add my snippets to the default javascript snippets file, but since I have the snipmate github repo synced inside my .vim/bundle/ folder, I want to keep the personal stuff separate from the live repo.

My Solution

The specific solution that finally got my files working side-by-side was to structure my files like this (by the way, I'm using pathogen to auto-load the bundle dir)

~/.vim/bundles/
    snipmate.vim/snippets/javascript.snippet
    vim-snippets.mootools/snippets/mootools.snippet
    vim-snippets.myCustomSnippets/snippets/javascript.snippets

By naming my file "javascript.snippets" it's auto-loaded along with the defaults.

Chant answered 2/8, 2011 at 17:45 Comment(0)
M
17

Ruy Diaz is right about the personal stuff, you can keep all of your own snippets in "~/.vim/snippets" and you won't have any problems with the github repo. If this is not working for you, the g:snippets_dir variable might have the wrong value -- just set it explicitly in your vimfiles.

As for combining several snippets, you can use the ExtractSnipsFile function. That should do what you want without messing around with dotted filetype syntax.

Let's say you have the following three snippet files, all in your snippet directory:

  • javascript.snippets
  • mootools.snippets
  • myCustomSnippets.snippets

Create a file "after/plugin/snippets.vim" and place the following in it:

call ExtractSnipsFile(g:snippets_dir.'javascript.snippets', 'javascript')
call ExtractSnipsFile(g:snippets_dir.'mootools.snippets', 'javascript')
call ExtractSnipsFile(g:snippets_dir.'myCustomSnippets.snippets', 'javascript')

This will associate all of these snippets with the javascript filetype, no explicit autocommands needed. For more information, you can try :help ExtractSnipsFile.

Mesocarp answered 2/8, 2011 at 19:0 Comment(2)
Wow. I did not know about ExtractSnipsFile -- very useful.Chant
for those coming to this later: if you use the newest version of snipmate (from garbas) and Pathogen, you can have multiple snippets with the same filename and they'll all get read. It'll even prompt you to choose if there are multiple types. Don't need to do anything else :)Ozonide
Z
4

Create a snippets folder inside your .vim directory and place your snippets there. Create a file called javascript.snippets in there and you should have both the snipmate snippets and your custom ones available.

Zanezaneski answered 2/8, 2011 at 18:13 Comment(0)
S
3

Just for further reference, the :h snipMate states: extends: extends the snippets of the given filetypes.

extends c, cpp, objc

this would include all snippets for the given filetypes. And just put your own snippet file in ~/.vim/snippets/objc.snippet

Silvana answered 17/6, 2014 at 18:1 Comment(1)
Soon as I realized there was a fork of the original plugin, this was my preferred solution. I used it for typescript, which extends javascriptPeddada
I
1

Yes of course. First use github.com/MarcWeber/vim-addon-manager so that you don't miss upstreams changes . M. Sander's snipmate has been forked because he never replied to emails about merge requests. That's why I and garbas improved it. The new version can be found at github.com/garbas/vim-snipmate.

You have a global scope_aliases dictionary you can use to associate filetypes with snippet files. This way you can add js snippet support to html files easily based on personal preferences. There are some more changes but I'm tired of repeating them.

Interpretive answered 7/1, 2012 at 22:56 Comment(0)
M
0

Since you are using pathogen than all you need do is add a custom Snippets folder to the bundles directory. For example:

~/.vim/bundles/mySnippets/snippets/php.snippets

Midrash answered 6/2, 2012 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.