Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new snippets to snipMate in VIM

Tags:

vim

snipmate

Ive just started using the sniptMate plugin for VIM and love it, however, since my collection of snippets isn't huge, I'd like to be able to add new snippets dynamically rather than opening the snippets file and creating a new snippet as I am in the middle of development.

As I am coding something I realize that some specific piece of code can be saved as a snippet to save me trouble of typing the bloat code again, at this time I want to be able to add a snippet without opening the snippet file for the language I am using at the time.

like image 411
Omnipresent Avatar asked Aug 15 '11 13:08

Omnipresent


4 Answers

Just place your own snippets (given you want to use them in all files you edit) here:

~/.vim/after/snippets/_.snippets

For example:

snippet test
  {"foo": "${1}", "bar": "${2}"}
like image 72
installero Avatar answered Sep 28 '22 17:09

installero


You can put your own snippets in ~/.vim/after/snippets/ or whatever the equivalent on Windows is... read :h snipMate for the filename syntax.

like image 45
bpj Avatar answered Oct 18 '22 02:10

bpj


If you are using pathogen, you can write your own snippets without polluting the original ones. To do this, add your snippets to ~/.vim/bundle/mysnippets/snippets/*.snippets. FYI, mysnippets can be any name.

Moreover, it's a convention to add a _.snippets file in that directory where you add snippets which should be available everywhere irrespective of the filetype.

like image 10
vikred Avatar answered Oct 18 '22 01:10

vikred


I'm not sure it's meant to be done like this but you can try calling the MakeSnip function from within file you're currently working on. For example:

:call MakeSnip(&ft, "foo", "<foo>${1}</foo>")

&ft will pass the filetype of the file you're currently editing, "foo" is the trigger and "<foo>${1}</foo>" is the replacement text.

Of course, snippets created like this won't persist. So why not have the snippets file open in another buffer, define new snippets there as necessary, then do:

:call ReloadSnippets(&ft)

and your new snippet will be available. You could even define an autocmd to call the ReloadSnippets function when you write the snippets file.

like image 6
david Avatar answered Oct 18 '22 02:10

david