Search code examples
vimpluginscode-snippets

How to create my custom vim snippets?


I want to create some snippets when writting c++.
for example:
create a file, cpp.snippets.

priority -1
snippet exam
This is an example!
endsnippet

and put it in ~/.vim/my-snippets/snippets/.
then, add following statement in ~/.vimrc:

set runtimepath+=~/.vim/my-snippets/
let g:UltiSnipsSnippetsDir='~/.vim/my-snippets/'
let g:UltiSnipsSnippetDirectories=["snippets"]

But it not work, how can i fix it ?


Solution

  • UltiSnips plugin includes detailed documentation. Have you read the following help page?

    :help UltiSnips-snippet-search-path
    

    Update:

    One of the things that was obvious when I read that help section was that in UltiSnips the name "snippets" can't be used in g:UltiSnipsSnippetDirectories because it is reserved for snipMate compatible snippets. This does not happen in the link shared in the comment below, where the name "my-snippets" is used instead.

    I do not use UltiSnips, but from the documentation I would suggest the following approach:

    1. Do not set g:UltiSnipsSnippetsDir nor g:UltiSnipsSnippetDirectories.
    2. Keep the runtimepath+= configuration.
    3. Create the following directory: ~/.vim/my-snippets/UltiSnips.
    4. Place the personal snippets under this new directory.

    Reasoning:

    • By default UltiSnips searches for all UltiSnips directories under your runtime paths, so no configuration is required if this name is used.
    • Although the runtime setting is required for personal snippets, this configuration is automatically maintained if a plugin manager is used.
    • The last point allows the installation of vim plugins that contain snippets. For example, this plugin contains various snippets for both snipMate and UltiSnips, including C++.