UltiSnips/doc/UltiSnips.txt

505 lines
20 KiB
Plaintext
Raw Normal View History

2009-07-13 04:46:14 -04:00
*UltiSnips.txt* The Ultimate Plugin for snippets in Vim.
UltiSnips *snippet* *snippets* *UltiSnips*
1. Description |UltiSnips-description|
2. Installation and Updating |UltiSnips-installnupgrade|
2.1 Installation |UltiSnips-installation|
2.2 Deinstallation |UltiSnips-deinstallation|
2.3 Updating |UltiSnips-updating|
3. Settings |UltiSnips-settings|
4. Syntax |UltiSnips-syntax|
2009-07-17 17:00:59 -04:00
4.1 Adding Snippets |UltiSnips-adding-snippets|
2009-07-15 14:11:47 -04:00
4.2 Plaintext snippets |UltiSnips-plaintext-snippets|
4.3 Interpolation |UltiSnips-integration|
4.3.1 Shellcode |UltiSnips-shellcode|
4.3.2 VimScript |UltiSnips-vimscript|
4.3.3 Python |UltiSnips-python|
4.4 Tab Stops and Placeholders |UltiSnips-tabstops|
4.6 Mirrors |UltiSnips-mirrors|
4.7 Transformations |UltiSnips-transformations|
2009-07-15 15:06:11 -04:00
4.7.1 Replacement String |UltiSnips-replacement-string|
4.7.2 Demos |UltiSnips-demos|
5. Roadmap |UltiSnips-roadmap|
6. Helping out |UltiSnips-helping|
7. Contact |UltiSnips-contact|
2009-07-13 04:46:14 -04:00
For Vim version 7.0 or later.
This plugin only works if 'compatible' is not set.
{Vi does not have any of these features}
This plugin needs Python support compiled into Vim.
=============================================================================
DESCRIPTION *UltiSnips-description*
UltiSnips aims to provide a snippets solution that users came to expect from
editors. A Snippet is a short piece of text which is either typed very often
or which contains a lot of redundant text. Such snippets are very often
encountered in structured text like Source Code but I also use them for email
signatures and to insert the current date or time into the text while typing.
UltiSnips is an implementation that is developed with in the philosophy of TDD
(Test driven development). This guarantees that features do not disappear and
bugs do not reappear after they have been fixed once.
2009-07-15 15:06:11 -04:00
Acknowledgments: *UltiSnips-acknowledgments*
UltiSnips is inspired by the snippets solution implemented in TextMate
(http://macromates.com/). The idea of implementing snippets for vim is not new
and I want to thank Michael Sander (Author of snipMate) for some
implementation details I stole from him and for the permission to use some of
his snippets.
2009-07-13 04:46:14 -04:00
=============================================================================
2. INSTALLATION AND UPDATING *UltiSnips-installnupdate*
2.1 Installation *UltiSnips-installation*
----------------
2009-07-13 04:46:14 -04:00
UltiSnips has only been tested on Mac OS X and Linux. Like TextMates Snippets
it also relies heavily on shell integration, therefore Windows users will have
only a part of the functionality.
To use UltiSnips, you need a python enabled Vim 7. You have python if
:echo has("python")
yields '1'.
If you have Python, you only need to install UltiSnips. The recommended way to
do so is by using bzr (http://bazaar-vcs.org/). It is in all major linux
distribution (either package bzr or bazaar) and can be easily installed under
Mac OS X: >
2009-07-13 04:46:14 -04:00
$ easy_install bzr
To get UltiSnips, check it out into a directory of your choice. Then add this
directory to your Vim runtime path: >
2009-07-13 04:46:14 -04:00
$ cd ~/.vim/
$ bzr get lp:ultisnips ultisnips_rep
$ vim ~/.vimrc
add the line: >
2009-07-13 04:46:14 -04:00
set runtimepath+=~/.vim/ultisnips_rep
Restart vim. UltiSnips should work now. To access the help, use >
:helptags ~/.vim/ultisnips_rep/doc
:help UltiSnips
2009-07-13 04:46:14 -04:00
2.2 Deinstallation *UltiSnips-deinstallation*
------------------
2009-07-13 04:46:14 -04:00
To Uninstall UltiSnips, remove the directory you installed it to and remove
the path from your vim runtimepath: >
2009-07-13 04:46:14 -04:00
$ rm -rf ~/.vim/ultisnips_rep
$ vim ~/.vimrc
remove the line: >
2009-07-13 04:46:14 -04:00
set runtimepath+=~/.vim/ultisnips_rep
2.3 Updating *UltiSnips-updating*
------------
To Update an installation, simply pull the latest revision: >
$ cd ~/.vim/ultisnips_rep
$ bzr pull
2009-07-15 14:11:47 -04:00
=============================================================================
3. SETTINGS *UltiSnips-settings*
You can freely define the keys used to expand a snippet and jump forward and
backwards in it. The variables with the default values are: >
g:UltiSnipsExpandTrigger <tab>
g:UltiSnipsJumpForwardTrigger <c-j>
g:UltiSnipsJumpBackwardTrigger <c-k>
It is possible to set the Expand and Forward trigger to the same value. You
can get a more TextMate like configuration by adding the next three lines to
you vimrc: >
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<tab>"
let g:UltiSnipsJumpBackwardTrigger="<s-tab>"
2009-07-15 14:11:47 -04:00
=============================================================================
4. SYNTAX *UltiSnips-syntax*
This chapter describes where to add new snippets and how to write them. I'll
provide many examples to make things clearer.
4.1 Adding Snippets *UltiSnips-adding-snippets*
-------------------
Snippets are searched in *runtimepath* . In each directory, a directory called
"UltiSnips" (case matters) is looked for. Inside these directories, files are
looked for called ft.snippets, for example: >
ruby.snippets
c.snippets
perl.snippets
These files contain the snippet definitions for the various file types. A
special file is >
all.snippets
which contains snippets that are always expanded, no matter what file type is
defined. For example, I keep mail signatures and date insertion snippets here.
2009-07-17 17:00:59 -04:00
The dotted file type syntax of vim is supported. For example, for my cpp or
CUDA files, i keep the file type set to ":set ft=cpp.c" or ":set
ft=cuda.cpp.c". This activates all snippets for each file type in the order
specified.
As an alternative, the first line of each snippet file can look like this: >
extends ft1, ft2, ft3
For example, the first line in cpp.snippets looks like this: >
extends c
This means, first check all triggers for c, then add the triggers from this
file. This is a convenient way to add more special cases to more general ones.
2009-07-15 14:11:47 -04:00
The snippets file format is simple. A line starting with # is a comment, each
snippet starts with a line in the form of: >
snippet tab_trigger [ "description" [ options ] ]
2009-07-15 14:11:47 -04:00
The following lines are the snippets definition; a line starting with >
endsnippet
marks the end of the snippet. The trailing newline is chopped from the
definition. In the starting line, the description and options are optional.
The description is only needed when more than one snippet is defined with the
same tab trigger. The user is then given a choice and the description is
displayed for the user as helper.
The options is a word of characters, each turning a specific option for this
snippet on. The options currently supported are >
! Overwrite - This snippet will overwrite all previously defined
snippets with this tab trigger. Default is to let the user choose at
expansion which snippet to expand. This option is useful to overwrite
bundled tab stops with user defined ones.
b Beginning of line - This snippet will only be expanded if you are at
the beginning of a line (that is if there are only whitespaces in front
of the cursor). Default is to expand snippets at every position, even
mitten in the line. Most of my snippets have this option set, it keeps
UltiSnips out of the way.
2009-07-15 14:11:47 -04:00
4.2 Plaintext snippets *UltiSnips-plaintext-snippets*
----------------------
Lets start with a simple example. Add this to your all snippets file: >
~/.vim/UltiSnips/all.snippets
------------------- SNIP -------------------
snippet bye "My mail signature"
Good bye, Sir. Hope to talk to you soon.
- Arthur, King of Britain
endsnippet
------------------- SNAP -------------------
Now, restart vim or reset UltiSnips by typing >
:py UltiSnips_Manager.reset()
now try it in insert mode:
bye<tab> -->
Good bye, Sir. Hope to talk to you soon.
- Arthur, King of Britain
4.3 Interpolation *UltiSnips-interpolation*
-----------------
4.3.1 Shellcode: *UltiSnips-shellcode*
You can enter shellcode in your snippets in nearly all places. Shell code is
written to a script and then executed. The output is inserted into the snippet
instead of the shell code itself. Since the code is executed as a shell
script, you can use #!/bin/ notation to feed the input of your code to every
program you like. Let's consider an example that inserts the current date.
------------------- SNIP -------------------
snippet today
Today is the `date +%d.%m.%y`.
endsnippet
------------------- SNAP -------------------
today<tab> ->
Today is the 15.07.09.
Another example doing the same using perl
------------------- SNIP -------------------
snippet today
Today is `#!/usr/bin/perl
@a = localtime(); print $a[3] . '.' . $a[4] . '.' . ($a[5]+1900);`.
endsnippet
------------------- SNAP -------------------
today<tab> ->
Today is 15.6.2009.
4.3.2 VimScript: *UltiSnips-vimscript*
You can also use VimScript (also sometimes called VimL) in interpolation. This
works very much the same as interpolation of shellcode, expect that it must be
started by using `!v `. Let's consider a simple example, that counts the
indent of the current line:
------------------- SNIP -------------------
snippet indent
Indent is: `!v indent(".")`.
endsnippet
------------------- SNAP -------------------
(note the 4 spaces in front): indent<tab> ->
(note the 4 spaces in front): Indent is: 4.
4.3.3 Python: *UltiSnips-python*
By far the most powerful interpolation is by using python code. The syntax is
similar to the one for Vimscript, but in python code the value of the python
variable "res" is inserting at the position of the code. Also, the code is
inside a `!p ` block. Python code gets some special variables defined which
can be used: >
fn - The current filename
path - The complete path to the current file
t - The values of the placeholders, t[1] -> current text of ${1} and so on
2009-07-19 10:44:29 -04:00
cur - The current text of the placeholder. You can check if this is != ""
to make sure that the interpolation is only done once
2009-07-15 14:11:47 -04:00
2009-07-19 10:44:29 -04:00
Also, the vim, re, os, string and random modules are already imported inside
the snippet code. This allows for very flexible snippets. For example, the
following snippet mirrors the first Tab Stops value on the same line in
uppercase and right aligned:
2009-07-15 14:11:47 -04:00
------------------- SNIP -------------------
snippet wow
${1:Text}`!p res = (75-2*len(t[1]))*' '+t[1].upper()`
endsnippet
------------------- SNAP -------------------
wow<tab>Hello World ->
Hello World HELLO WORLD
4.4 Tab Stops and Placeholders *UltiSnips-tabstops* *UltiSnips-placeholders*
------------------------------
Very often, it is convenient to quickly add some text snippet and jump on to
the next point of interest. This is were tabstops come in:
------------------- SNIP -------------------
snippet jump
I $1 I was a $2.
$0
endsnippet
------------------- SNAP -------------------
jump<tab>wish<c-j>hunter<c-j>more text ->
I wish I was a hunter.
more text
You can use <c-j> and <c-k> to jump to the next tab or the previous. <Tab> was
not used for jumping forward because many people (like myself) use <tab> also
for completion. $0 is a special tab: This is the last tab in the snippet, no
matter how many tabs were defined before.
Most of the time it is more useful to have some default text for a tabstop and
sometimes it is also useful to have a tab stop in a tab stop. Consider the
next example which could be a real life scenario from a HTML snippet:
------------------- SNIP -------------------
snippet a
<a href="$1"${2: class="${3:link}"}>
$0
</a>
endsnippet
------------------- SNAP -------------------
Here, $1 marks the first tabstop; it is assumed that you always want to add a
url as href. After entering it you jump to $2 which has the default text '
class="link"'. Now, you can either hit backspace, because you do not want to
enter a class attribute for this tag or you hit <c-j> to change the class name
(which is tab stop $3). When you are satisfied with the first line you hit
<c-j> again to finish this snippet and contiue inside the anchor.
a<tab>http://www.google.com<c-j><BS><c-j>hi ->
<a href="http://www.google.com">
hi
</a>
a<tab>http://www.google.com<c-j><c-j>visited<c-j>hi ->
<a href="http://www.google.com" class="visited">
hi
</a>
Default tab stop text can also contain mirrors, transformations or
interpolation.
4.6 Mirrors *UltiSnips-mirrors*
-----------
2009-07-15 15:06:11 -04:00
Mirrors simply repeat the content of a tabstop. A classical example would be a
TeX Environment which needs a \begin{} and an \end{} tag:
------------------- SNIP -------------------
snippet env
\begin{${1:enumerate}}
$0
\end{$1}
endsnippet
------------------- SNAP -------------------
env<tab>itemize ->
\begin{itemize}
\end{itemize}
Another typical example is #ifndef block in C code:
------------------- SNIP -------------------
snippet ifndef
#ifndef ${1:SOME_DEFINE}
#define $1
$0
#endif /* $1 */
endsnippet
------------------- SNAP -------------------
ifndef<tab>WIN32 ->
#ifndef WIN32
#define WIN32
#endif /* WIN32 */
2009-07-15 14:11:47 -04:00
4.7 Transformations *UltiSnips-transformations*
-------------------
2009-07-15 15:06:11 -04:00
A complete description of the features follows, the chapter closes with some
demos, because this feature is rather mighty and hard to understand.
Transformations are like mirrors. But instead of just verbatim copying text
from the original tab stop, a regular expression is matched to the content of
the referenced tab stop and a transformation is then applied to the matched
pattern. UltiSnips follows the syntax and features of TextMate very closely
here.
A Transformation has the syntax >
${<tab stop no/regular expression/replacement/options}
with >
tab stop no - The number of the tab stop to reference
regular expression - The regular expression to match to the value of the
tab stop
replacement - The replacement string, see below
options - options for the regular expression
The options can be any combination of >
g - global replace, replaces all matches of the regular expression, not
just the first
Regular expressions are not handled here. Python regular expressions are used
internally, so the reference for matching is the re module of python:
http://docs.python.org/library/re.html
The replacement string is using a own syntax and is handled in the next paragraph.
4.7.1 Replacement String: *UltiSnips-replacement-string*
The replacement string can contain $no variables to reference matched groups
in the regular expression, $0 is special and yields the whole match. The
replacement string can also contain special escape sequences: >
\u - Uppercase next letter;
\l - Lowercase next letter
\U - Uppercase everything till the next \E
\L - Lowercase everything till the next \E
\E - End upper or lowercase started with \L or \U
\n - A newline
\t - A literal tab
Last, but not least the replacement string might contain conditional
replacements with the following syntax (?no:text:other text). This reads as
follows: if the group $no has matched, insert "text", otherwise insert "other
text". "other text" could be skipped and would default to "", that is the
empty string. This is a very powerful feature to add optional text into
snippets.
4.7.2 Demos: *UltiSnips-demos*
The transformations are very powerful but yield often a convoluted snippet
syntax. Therefore I offer for each feature a simple example below.
This shows uppercasing one character
------------------- SNIP -------------------
snippet title "Titelize in the Transformation"
${1:a text}
${1/\w+\s*/\u$0/}
endsnippet
------------------- SNAP -------------------
title<tab>big small ->
big small
Big small
This shows uppercasing one character and global replace:
------------------- SNIP -------------------
snippet title "Titelize in the Transformation"
${1:a text}
${1/\w+\s*/\u$0/g}
endsnippet
------------------- SNAP -------------------
title<tab>this is a title ->
this is a title
This Is A Title
This is a clever c-like printf snippet, the second tabstop is only shown
when there is a format (%) character in the first tab stop.
------------------- SNIP -------------------
snippet printf
printf("${1:%s}\n"${1/([^%]|%%)*(%.)?.*/(?2:, :\);)/}$2${1/([^%]|%%)*(%.)?.*/(?2:\);)/}
endsnippet
------------------- SNAP -------------------
printf<tab>Hello<c-j> // End of line ->
printf("Hello\n"); // End of line
But
printf<tab>A is: %s<c-j>A<c-j> // End of line ->
printf("A is: %s\n", A); // End of line
There are many more examples of what can be done with transformations in the
bundled snippets.
2009-07-15 14:11:47 -04:00
=============================================================================
5. ROADMAP *UltiSnips-roadmap*
2009-07-15 15:06:11 -04:00
UltiSnips is useful as it is now; but I plan to add more features. I use this
section as a brain storming of what should come in the future to provide some
insight into which direction development is headed. Please feel free to
comment on any feature here or if you have any good ideas what is missing
contact me.
Cursor key movement: *UltiSnips-cursor-key-movement*
Currently, cursor key movement is not possible in snippets. When you use the
cursors, you need to use <c-j> or <c-k> to jump to the next tab stop again
before anything gets updated. This is a bit annoying and I want to make cursor
keys work.
Escaping: *UltiSnips-escaping*
Leaving to normal mode leaves snippet expansion currently. This shouldn't
happen. This is related to Cursor key movement.
Jumping back from TS zero: *UltiSnips-jumping-back-from-ts-zero*
Currently, jumping back from $0 with <c-k> doesn't work. This is really not
necessary and should be fixed.
2009-07-15 14:11:47 -04:00
=============================================================================
6. HELPING OUT *UltiSnips-helping*
2009-07-15 15:06:11 -04:00
UltiSnips needs the help of a vibrant vim community to make it more useful
tomorrow than it is today. Please consider joining this effort by providing
new snippets, new features or bug reports.
If you like this plugin, please also vote for it on its vim script page. It is
life changing for me, maybe it is for you too.
You can find the page here:
http://www.vim.org/scripts/script.php?script_id=2715
2009-07-13 04:46:14 -04:00
=============================================================================
7. CONTACT *UltiSnips-contact*
2009-07-13 04:46:14 -04:00
You can reach me at SirVer -AT- gmx -ADOT- de. You can find the launchpad
project for UltiSnips at https://launchpad.net/ultisnips/, there is also the
bug tracker.
2009-07-15 14:11:47 -04:00
This project aims to be the one-for-all solution for Snippets for Vim. If you
miss a feature or find a bug, please contact me or file a support ticket.
vim:tw=78:ts=8:ft=help:norl: