How to create my custom vim snippets?

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

  • Patch: Creating a ctrl+tab keybinding in PuTTY
  • How to solve the collision of TAB key mapping of `UltiSnips` plugin in the Vim
  • https://unix.stackexchange.com/questions/53581/sending-function-keys-f1-f12-over-ssh

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++.

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.