Vim and snipMate (plugin) - adding new snippet won

2019-07-16 12:53发布

问题:

I am trying to create a new snippet to my snipMate plugin.

I work with some files called (i.e.) myfile.endfile

All .endfile files should have the same "snippet" like .html files. So I did

cp html.snippet endfile.snippet

in my ~/.vim/snippets directory.

SnipMate is working with all present snippets, but not with my new created one. Any suggestions for this problem?

(Btw: after creating the new .snippet file, I ran :helptags ~/.vim/doc command in an vim instance.)

回答1:

It is because Snipmate works with filetype, which is a Vim option set when opening a file of a particular type.

For exemple, if you are opening, "index.html" the filetype is automatically set to html.

To see how it works, do :
:e $VIMRUNTIME/filetype.vim

As a preliminary test, you can :
1. open test.endfile
2. type :set ft=endfile or :set filetype=endfile
3. Check if your defined snippets now work

To do that automatically add the following in your .vimrc :
au BufNewFile,BufRead *.endfile set filetype=endfile

It means that every time you read or create a new file ending in endfilethe filetype option is set to endfile.

(The filetype is an arbitrary string it doesn't have to be identical to the file extension)



回答2:

You can assign snippets without altering the filetype (which is desirable, because altering the filetype breaks syntax highlighting).

I believe the proper way to do this in the maintained fork of snipmate is to set g:snipMate.scope_aliases.

In your example, assuming you have an 'endfile.snippet' file, I believe adding the following to your .vimrc would work:

let g:snipMate = {}
let g:snipMate.scope_aliases = {}
let g:snipMate.scope_aliases['html'] = 'endfile'

If you want both html and endfile snippets to work for files of filetype='html', then use:

let g:snipMate = {}
let g:snipMate.scope_aliases = {}
let g:snipMate.scope_aliases['html'] = 'html,endfile'

I've added a pull request to snipmate to have their documentation updated. Edit: It has now been merged.



回答3:

I found it convenient to use global snippets when using snippets that have uncommon name.endfile.

When you put your snippets in _.snippets file inside snippets folder they become global and are accessible in every filetype.

Maybe this is not directly answer to the question but a lot of users with similar problem can find this convenient. Specially if they don't have need to have everything organised in various files and are happy to have their own snippets in one file that is accessible everywhere.



标签: vim snipmate