简体   繁体   English

Vim和snipMate(插件) - 添加新代码片段无效

[英]Vim and snipMate (plugin) - adding new snippet won't work

I am trying to create a new snippet to my snipMate plugin. 我正在尝试为我的snipMate插件创建一个新代码段。

I work with some files called (ie) myfile.endfile 我使用一些名为(即)myfile.endfile的文件

All .endfile files should have the same "snippet" like .html files. 所有.endfile文件都应该具有与.html文件相同的“代码段”。 So I did 所以我做了

cp html.snippet endfile.snippet

in my ~/.vim/snippets directory. 在我的〜/ .vim / snippets目录中。

SnipMate is working with all present snippets, but not with my new created one. SnipMate正在处理所有现有的片段,但不适用于我新创建的片段。 Any suggestions for this problem? 对此问题的任何建议?

(Btw: after creating the new .snippet file, I ran :helptags ~/.vim/doc command in an vim instance.) (顺便说一下:创建新的.snippet文件后,我在vim实例中运行了:helptags ~/.vim/doc命令。)

It is because Snipmate works with filetype , which is a Vim option set when opening a file of a particular type. 这是因为Snipmate使用filetype ,这是在打开特定类型的文件时设置的Vim选项。

For exemple, if you are opening, "index.html" the filetype is automatically set to html . 例如,如果要打开,“index.html” filetype将自动设置为html

To see how it works, do : 要了解它的工作原理,请:
:e $VIMRUNTIME/filetype.vim

As a preliminary test, you can : 作为初步测试,您可以:
1. open test.endfile 1.打开test.endfile
2. type :set ft=endfile or :set filetype=endfile 2.键入:set ft=endfile:set filetype=endfile
3. Check if your defined snippets now work 3.检查您定义的片段现在是否有效

To do that automatically add the following in your .vimrc : 为此,请在.vimrc中自动添加以下内容:
au BufNewFile,BufRead *.endfile set filetype=endfile

It means that every time you read or create a new file ending in endfile the filetype option is set to endfile. 这意味着每次读取或创建以endfile结尾的新文件时,filetype选项都将设置为endfile。

(The filetype is an arbitrary string it doesn't have to be identical to the file extension) (文件类型是一个任意字符串,它不必与文件扩展名相同)

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. 我相信在保持的snipmate分支中执行此操作的正确方法是设置g:snipMate.scope_aliases。

In your example, assuming you have an 'endfile.snippet' file, I believe adding the following to your .vimrc would work: 在您的示例中,假设您有一个'endfile.snippet'文件,我相信将以下内容添加到.vimrc中会起作用:

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: 如果你想让html和endfile片段都适用于filetype ='html'的文件,那么使用:

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. 我已经向snipmate添加了一个pull请求以更新其文档。 Edit: It has now been merged. 编辑:它现在已合并。

I found it convenient to use global snippets when using snippets that have uncommon name.endfile. 我发现在使用具有罕见name.endfile的片段时使用全局代码段很方便。

When you put your snippets in _.snippets file inside snippets folder they become global and are accessible in every filetype. 当您将片段放在片段文件夹中的_.snippets文件中时,它们将变为全局,并且可以在每种文件类型中访问。

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. 特别是如果他们不需要在各种文件中组织所有内容,并且乐于在一个可随处访问的文件中使用自己的代码段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM