Vim 中的片段与缩写 [英] Snippets vs. Abbreviations in Vim
问题描述
使用片段"插件有什么优点和/或缺点,例如snipmate, ultisnips,用于 VIM 而不是简单地使用内置的 "abbreviations" 功能?
What advantages and/or disadvantages are there to using a "snippets" plugin, e.g. snipmate, ultisnips, for VIM as opposed to simply using the builtin "abbreviations" functionality?
是否有声明iabbr
、cabbr
等的特定用例缺乏一些主要功能片段插件提供?我一直未能找到这两个功能"及其各自实现之间的彻底比较.
Are there specific use-cases where declaring iabbr
, cabbr
, etc. lack some major features that the snippets plugins provide? I've been unsuccessful in finding a thorough comparison between these two "features" and their respective implementations.
正如@peter-rincker 在评论中指出的那样:
As @peter-rincker pointed out in a comment:
需要注意的是,缩写也可以执行代码.通常通过
或通过表达式缩写 (
.将@@ 扩展到当前文件路径的示例::iabbrev @@ <c-r>=expand('%:p')<cr>
It should be noted that abbreviations can execute code as well. Often via
<c-r>=
or via an expression abbreviation(<expr>)
. Example which expands @@ to the current file's path::iabbrev @@ <c-r>=expand('%:p')<cr>
以 Python 为例,让我们比较 Vim 中的 snipmate 代码段和缩写,用于为类声明插入行.
As an example for python, let's compare a snipmate snippet and an abbrev in Vim for inserting lines for class declaration.
# New Class
snippet cl
class ${1:ClassName}(${2:object}):
"""${3:docstring for $1}"""
def __init__(self, ${4:arg}):
${5:super($1, self).__init__()}
self.$4 = $4
${6}
Vimscript
au FileType python :iabbr cl class ClassName(object):<CR><Tab>"""docstring for ClassName"""<CR>def __init__(self, arg):<CR><Tab>super(ClassName, self).__init__()<CR>self.arg = arg
当 Vim 的 abbr
和 :help template
模板是能够做所有大部分的东西片段吗?
Am I missing some fundamental functionality of "snippets" or am I correct in assuming they are overkill for the most part, when Vim's abbr
and :help template
templates are able to do all most of the stuff snippets do?
我认为实现片段更容易,并且它们提供了额外的美学/视觉功能.例如,如果我在 Vim 和其他插件中使用 abbr
来在 vim 中运行/测试 python 代码——例如syntastic, pytest、ropevim、pep8 等——我是否错过了代码片段提供的一些关键功能?
I assume it's easier to implement snippets, and they provide additional aesthetic/visual features. For instance, if I use abbr
in Vim and other plugins for running/testing python code inside vim--e.g. syntastic, pytest, ropevim, pep8, etc--am I missing out on some key features that snippets provide?
推荐答案
可以用片段完成的所有事情都可以用缩写完成,反之亦然.您可以使用(镜像或不镜像)带有缩写的占位符,您可以使用上下文相关的片段.
Everything that can be done with snippets can be done with abbreviations and vice-versa. You can have (mirrored or not) placeholders with abbreviations, you can have context-sensitive snippets.
有两个重要的区别:
- 当输入缩写文本并点击非单词字符(或 esc)时,会触发缩写.代码段按需触发,快捷方式是可能的(无需键入
while
+ tab.w
+ tab 可能就足够了). - 定义新片段(或维护旧片段)比定义缩写要容易得多.用缩写,很多样板代码是当我们想做整洁的事情时需要.
- Abbreviations are triggered when the abbreviation text has been typed, and a non word character (or esc) is hit. Snippets are triggered on demand, and shortcuts are possible (no need to type
while
+ tab.w
+ tab may be enough). - It's much more easier to define new snippets (or to maintain old ones) than to define abbreviations. With abbreviations, a lot of boiler plate code is required when we want to do neat things.
还有一些其他差异.例如,缩写总是在任何地方触发.在注释或字符串上下文中看到 for
扩展为 for(placeholder) {\n}
肯定不是最终用户所期望的.使用代码段,这不再是问题:我们可以期望最终用户在要求扩展代码段时知道他在做什么.不过,我们可以提出上下文感知片段 在评论中将 throw
扩展为 @throw {domain::exception} {explanation}
,或将其扩展为 throw domain::exception({message});
其他地方.
There are a few other differences. For instance, abbreviations are always triggered everywhere. And seeing for
expanded into for(placeholder) {\n}
within a comment or a string context is certainly not what the end-user expects. With snippets, this is not a problem any more: we can expect the end-user to know what's he's doing when he asks to expand a snippet. Still, we can propose context-aware snippets that expand throw
into @throw {domain::exception} {explanation}
within a comment, or into throw domain::exception({message});
elsewhere.
这篇关于Vim 中的片段与缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!