How to create my custom vim snippets?
Asked Answered
D

3

12

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 ?

Desecrate answered 22/8, 2015 at 13:45 Comment(2)
Let's start at the beginning: Do you have UltiSnips? Does it work?Outing
Yes, I have UltiSnips, and it work.Desecrate
M
16

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++.
Matrimony answered 24/8, 2015 at 7:30 Comment(2)
Yes, I have. But, maybe i misunderstand its means. anyway, I reference this web, physik.uni-leipzig.de/~marenz/index.cgi/blog , and fixed my problem. And thanks for your answer.Desecrate
@Desecrate I've updated my answer because it was not obvious from your comment if you understood what was wrong in your original configuration. I also provided an alternative and simpler approach to solve your problem. Hope it helps.Matrimony
A
14

If you are using the sirver/ultisnips plugin (UltiSnips) the correct way to do this is simply run the :UltiSnipsEdit command which opens up a custom snippets file for the current language / filetype.

Amii answered 30/12, 2019 at 19:24 Comment(1)
Thank you! This is the most sensible answer.Illailladvised
D
1

I had so much grief with this. Here is an answer for future reference for those of you who do not want to suffer headaches.

I have a shared .vimrc served on a samba share. Both Windows gViM and ViM use this file.

Relevant Part of .vimrc for Windows

I have a samba share mounted under L:. Note that I actually had to use POSIX for the path, not Windows backslashes \ despite being a path for Windows.

if has('win32') || has('win64') "If gVim under Windows"
    let g:UltiSnipsSnippetDirectories=["L:/.vim/custom_snippets"]
endif

Relevant Part of .vimrc for Unix

My terminal opens xterm-256color for more colors, but you could exchange that with xterm. Here the path can expand ~ correctly, since this is the real home directory where my ``.vim` lives.

if $TERM == "xterm-256color"
  let g:UltiSnipsSnippetDirectories=["~/.vim/custom_snippets"]
endif

Finishing Touches to Load custom_snippets

You don't need any! The following changes are NOT necessary:

"let g:UltiSnipsSnippetDirectories=["custom_snippets"]
"let g:UltiSnipsSnippetsDir="~/.vim/snippets_custom/"

However, Putty does not pass the tab key or control key properly it seems, despite all paths working fine. I tested the paths with :UltiSnipsEdit while in a file type environment set ft=tex and it took me to ~/.vim/snippets_custom/tex.snippets as it should (both in gvim on Windows and from my unix console).

Perhaps Useful for enabling in Putty

Deciliter answered 21/6, 2016 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.