Snippets vs. Abbreviations in Vim

后端 未结 4 1825
再見小時候
再見小時候 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: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:}
    # 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.

提交回复
热议问题