UltiSnips is a really powerful vim plugin that boosts your productivity by saving you from typing redundant code patterns or repeated configurations. And UltiSnips provides an easy way to create and use snippets:

1. Snippets are organized by file type: cpp.snippets for c++ and pandoc.snippets for pandoc.
2. Any changes made to snippets will take effect after you save them. So you can create and edit snippets on the fly.

For example, writing the following c++ code can be really easy (and quick):

And You will notice a huge difference when you turn off this plugin once you get used to it. There are lots of awesome snippet plugins, such as neosnippet.

UltiSnips is more like a snippet engine while you actually need a separate plugin for your “snippet collection”. And offical document of UltiSnips uses vim-snippets as the “collection plugin”.

## Installation of UltiSnips and vim-snippets

If you use vim-plug, all you need to do is to provide the repo name for UltiSnips and vim-snippets in vimrc like the following. Reload vim and run :PlugInstall.

And here is a screenshot of snippets provided by vim-snippets:

## UltiSnips requirement and how it works

In order to use UltiSnips, vim needs python or python3 support, which can be checked by running

or you could check you vim version and find out if it has python support by running

If you saw either +python or +python3 in the output, you are good to go. My vim is compiled with python2 support, so I have +python and -python3 in the screenshot.

In this snippet, ${1:Class} is a placeholder marked as $1 with default content Class. To define a jump sequence, we use dollar sign followed by numbers like $1, $2, $3 The default key to jump forward and backward is After we update the snippet, expand class, and type BinaryTree, we can jump to the next two placeholders by pressing <c-j> (ctrl + j). ## Visual Placeholder in UltiSnips Visual placeholder is another powerful feature. It further reduces the amount of typing by providing you a way to reuse the existing context in the buffer. Suppose we use the following snippet to define a function in c++: Suppose we have the following line in the buffer and want to use the following content as the function body: This could be done in two steps: 1. expand the keyword fun 2. move the line and indent Using the visual placeholder, this can be done in one step with a simple modification. Any selected content under the visual mode will be put into the visual placeholder, ${VISUAL} when the snippet is expanded. Thus, instead of moving the line and indent it, we could do

1. select the line under visual mode and hit <TAB>
2. expand the keyword fun

This feature is really helpful when you restructure you text a lot. For example, wrap a paragraph in environment such as enumerate, itemize when writing LaTex.

## Python interpolation in UltiSnips

UltiSnips also allows users to leverage python to accomplish complex tasks. Suppose we want to insert all files in the current directory, we can write a simple python function to do this. We the following function in cpp.snippets. Here, !p means python.

Then, we could call this function when we write other snippets.

Suppose we have two files in the folder: ultisnip.cpp and ultisnip.h.

And here is the link to the Chinese version of this post.