Snippets versus acronyms in Vim

What are the advantages and / or disadvantages of using the "snippets" plugin for example. snipmate , ultisnips , for VIM as opposed to just using the built-in abbreviations functionality

Are there specific use cases where the declaration iabbr

, cabbr

etc. lacking some of the core functionality that fragment plugins provide? I have not been able to find a complete comparison between these two "functions" and their respective implementations.

As @ peter-rincker pointed out in a comment:

It should be noted that abbreviations can also execute code. Often through <c-r>=

or through an expression abbreviation (<expr>)

. An example that expands @@ to the current file path::iabbrev @@ <c-r>=expand('%:p')<cr>

As an example for python one can compare snipmate snippet and abbreviation in Vim to insert strings for class declaration.

Snipmate

# 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

      

I missed some of the fundamental functionality of the "fragments", or am I right in thinking that they are excessive for the most part, when Vim templates abbr

and :help template

can perform all the majority of the fragments of material?

I guess it's easier to implement fragments and provide additional aesthetic / visual functionality. For example, if I use abbr

in Vim and other plugins to run / test python code inside vim - eg. syntastic , pytest , ropevim , pep8 , etc. Am I missing some key features that snippets provide?

+4


source to share


4 answers


Anything that can be done with slices can be done with abbreviations and vice versa. You can have (mirrored or not) abbreviated placeholders, you can have context sensitive snippets.

There are two important differences:



  • Abbreviations are triggered when you type the text of the abbreviation and the no-word character (or esc) is removed. Snippets don't fire on demand and shortcuts are possible (no need to type while

    + tab. w

    + Tab might be enough).
  • It is much easier to define new fragments (or keep old ones) than to define abbreviations. With cuts , a lot of boiler stove code is required when we want to do neat things .

There are a few more differences. For example, acronyms always work everywhere. And seeing for

laid out for(placeholder) {\n}

in a comment or inline context is certainly not what the end user expects. With fragments, this is no longer a problem: we can expect the end user to know what he is doing when he asks to expand the fragment. However, we can offer context sensitive snippets that expand throw

in @throw {domain::exception} {explanation}

a comment or throw domain::exception({message});

elsewhere.

+2


source


Fragments are more powerful.

Depending on the implementation, fragments can allow you to modify (or accept the default) for multiple placeholders and even execute code as the fragment expands.

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

Example (ultisnips):

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 gives you three placeholders to fill (it gives you defaults for two of them) and sets the file name, file type, and current date and time.



After the word "fragment", the start line contains three elements:

  • trigger string,
  • description and
  • for a fragment.

Personally, I mostly use a parameter b

where the slice expands at the beginning of the line and a parameter w

that expands the slice if the trigger line starts at the beginning of a word.

Note that you need to enter the trigger string and then enter the key or key combination that actually initiates the expansion. This way the snippet doesn't expand if you don't want to.

In addition, fragments can be file-type specialized. Suppose you want to define four levels of headings h1

.. h4

. You can have the same name expanding differently between, for example, HTML, markdown, LaTeX, or a restructured text file.

+4


source


snippets are similar to those embedded :abbreviate

in steroids, usually with:

  • inserting parameters . You can insert (type or select) text snippets in different places within the snippet. The abbreviation is simply expanded once.
  • mirroring . Parameters can be repeated (possibly even transformed) elsewhere in the fragment, usually updated as you type.
  • multiple stops inside: you can jump from one point to another within a fragment, sometimes even recursively expanding fragments within one.

There are three things in the snippet plugin: first, the specifics 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 fragments.

+3


source


Excerpts

A rough superset of Vim's own shortcuts. Here are the highlights:

  • Only key press when key is pressed
  • Uses placeholders that the user can jump between
  • Insert mode only
  • Dynamic extensions

Abbreviations

Great for common typos and small snippets.

  • Native to Vim, so no plugins needed
  • Usually expands in space or <c-]>

  • Some special rules for trigger text (see :h abbreviations

    )
  • Can be used in command mode via :cabbrev

    (often used to create command aliases)
  • No placeholders
  • Dynamic extensions

Conclusion

For the most part, snippets are more powerful and provide many of the features that other editors use, but you can use them for many or many people. Abbreviations are taken advantage of as native, which can be useful for remote environments. Abbreviations also have another distinct advantage that can be used in command mode.

+2


source







All Articles