Snippets vs. Abbreviations in Vim

后端 未结 4 1834
再見小時候
再見小時候 2021-02-15 12:40

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\"

相关标签:
4条回答
  • 2021-02-15 13:08

    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.

    There are two important differences:

    • 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.

    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.

    0 讨论(0)
  • 2021-02-15 13:18

    snippets are like the built-in :abbreviate on steroids, usually with:

    • parameter insertions: You can insert (type or select) text fragments in various places inside the snippet. An abbreviation just expands once.
    • mirroring: Parameters may be repeated (maybe even in transformed fashion) elsewhere in the snippet, usually updated as you type.
    • multiple stops inside: You can jump from one point to another within the snippet, sometimes even recursively expand snippets within one.

    There are three things to evaluate in a snippet plugin: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.

    0 讨论(0)
  • 2021-02-15 13:26

    Snippets are more powerful.

    Depending on the implementation, snippets can let you change (or accept defaults for) multiple placeholders and can even execute code when the snippet is expanded.

    For example with ultisnips, you can have it execute shell commands, vimscript but also Python code.

    An (ultisnips) example:

    snippet hdr "General file header" b
    # file: `!v expand('%:t')`
    # vim:fileencoding=utf-8:ft=`!v &filetype`
    # ${1}
    #
    # Author: ${2:J. Doe} ${3:<jdoe@gmail.com>}
    # Created: `!v strftime("%F %T %z")`
    # Last modified: `!v strftime("%F %T %z")`
    endsnippet
    

    This presents you with three placeholders to fill in (it gives default values for two of them), and sets the filename, filetype and current date and time.

    After the word "snippet", the start line contains three items;

    • the trigger string,
    • a description and
    • options for the snippet.

    Personally I mostly use the b option where the snippet is expanded at the beginning of a line and the w option that expands the snippet if the trigger string starts at the beginning of a word.

    Note that you have to type the trigger string and then input a key or key combination that actually triggers the expansion. So a snippet is not expanded unless you want it to.

    Additionally, snippets can be specialized by filetype. Suppose you want to define four levels of headings, h1 .. h4. You can have the same name expand differently between e.g. an HTML, markdown, LaTeX or restructuredtext file.

    0 讨论(0)
  • 2021-02-15 13:29

    Snippets

    Rough superset of Vim's native abbreviations. Here are the highlights:

    • Only trigger on key press
    • Uses placeholders which a user can jump between
    • Exist only for insert mode
    • Dynamic expansions

    Abbreviations

    Great for common typos and small snippets.

    • Native to Vim so no need for plugins
    • Typically expand on whitespace or <c-]>
    • Some special rules on trigger text (See :h abbreviations)
    • Can be used in command mode via :cabbrev (often used to create command aliases)
    • No placeholders
    • Dynamic expansions

    Conclusion

    For the most part snippets are more powerful and provide many features that other editors enjoy, but you can use both and many people do. Abbreviations enjoy the benefit of being native which can be useful for remote environments. Abbreviations also enjoy another clear advantage which is can be used in command mode.

    0 讨论(0)
提交回复
热议问题