13

I have made a snippet which I want to give different triggers, so instead of say trigger I also want to trigger it with myothertrigger:

snippet trigger "just an example" 
my text ...
endsnippet

Is there an easy way to do this instead of copying the snippet?

Edit: To be clear, I am using UltiSnips, but if there is a better plugin I might look into that.

4
  • 2
    The answer would depend on the snippet engine you're using. With mu-template you could have include one snippet from another, copy the snippet file under a different name or just create a symbolic link. I'm not sure the other engines could permit it. May be if some day they come to support snippet inclusion from "caller" snippets. Perhaps you could define the core of your snippet in a python or viml function and have your snippets call the same function? Commented Feb 28, 2016 at 4:08
  • 3
    The question is tagged as plugin-ultisnips, so I would assume the OP is using ultisnips. Commented Feb 28, 2016 at 11:14
  • My mistake ^^' (µTemplate isn't better, just very different, older, and more suited toward my needs (I'm maintaining it): I've root snippets that behave as functions that I can call with different options to produce different but very close things.Eg in C++ I have snippets for base-class, value-class, exception-class... that all depend on the same set of class-snippet+function-snippet+... But as I told, the syntax is completely different, and the placeholder feature is less ergonomic than the one you're used to. Doing what you're looking for would be easy, but not necessity worth a migration) Commented Feb 28, 2016 at 12:16
  • 2
    If you switch to Shougo's neosnippet plugin, its syntax has an alias keyword for this very purpose.
    – VanLaser
    Commented Feb 29, 2016 at 19:10

3 Answers 3

9

Currently there is no way to do this in UltiSnips. There is an open feature-request on Github for it though. Personally, I would like an alias functionality for triggers as indicated here too.

Let's hope SirVer (the maintainer of UltiSnips) will think so too and include it in the future :)

7

Although there is no specific syntax for aliases you can achieve the same effect by using post_jump:

# c.snippets
global !p
def expand(snip):
    if snip.tabstop != 1:
        return
    vim.eval('feedkeys("\<C-R>=UltiSnips#ExpandSnippet()\<CR>")')
endglobal

snippet incstdlib "#include <stdlib.h>" !b
#include <stdlib.h> /* exit(), malloc(), free() */
$0
endsnippet

post_jump "expand(snip)"
snippet incexit "#include <stdlib.h>" !b
incstdlib$1
endsnippet

In this snippets file the trigger incexit is an "alias" of trigger incstdlib. In reality incexit expands to incstdlib and the expand(snip) is called when UltiSnips moves to the tabstop $1. The expand(snip) just executes the VimScript function UltiSnips#ExpandSnippet which will expand incstdlib into it's final expanded form #include <stdlib.h>.

Another options is to use UltiSnips regular expression support instead:

snippet "inc(true|false|bool)" "#include <stdbool.h>" r
#include <stdbool.h> /* true, false */
endsnippet

Here you will have three triggers inctrue, incfalse and incbool that are effectively the same. The only drawback is that autocompletion plugins like YouCompleteMe will not autocomplete those.

1

Plugin neosnippet.vim support multiple trigger by keyword 'alias',
e.g. there have three triggers if myif myif2:

snippet     tcl::if
options     head
alias       if myif myif2
abbr tcl::if
    if { ${0:#:TARGET} } {
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.