Note
Changes in UltiSnips master broke compatibility with DSnips recently (August 2014). Update DSnips (re-download d.snippets) to fix this.
DSnips is a collection of UltiSnips snippets for the D programming language. UltiSnips is a snippets plugin for Vim.
A few examples of what snippets can do:
Get DSnips:
git clone https://github.com/kiith-sa/DSnips.git
Copy the
d.snippets
file to UltiSnips user snippets directory. On Linux this should be~/.vim/UltiSnips
.If you don't know where the user snippets directory is, open a D file and type the
:UltiSnipsEdit
command (after UltiSnips is installed); this will open a userd.snippets
file which you can replace by the DSnips version ofd.snippets
.
As the sheer number of snippets here may be a bit overwhelming, here is a small(ish) list of snippets that should provide a quick improvement for most people:
assert / enforce | as / enf |
(wrap in) if / else if / else | if / elif / else |
for / foreach | for / fore |
writeln | wr |
return | ret |
(wrap in) scope guard | scope |
(wrap in) try/catch / throw / define Exception | try / thr / exc |
module header / import | module / imp |
getter / setter | get / set |
define a function | fun |
constructor | this |
switch / case | sw / case |
string mixin | smix |
DDoc Params: / Returns: | Par / Ret |
Various other snippets may save time depending on which D features you use most, see Reference.
Some snippets (if, try, etc.) can be used to wrap code using the visual placeholder feature of UltiSnips. To wrap code, select the code you want to wrap (using visual mode), and press the UltiSnips expand trigger. This will delete the code to insert it the next time you expand a snippet.
This is a heavily modified version of default UltiSnips D snippets which I made some time ago. Unfortunately, some snippets specific for my projects as well as some indentation bugs ended up in the default snippets. This is an attempt to revamp those snippets and eventually get merged back to the default repo.
Compared to the "old" D snippets, some snippets have been removed (project-specific,
snippets that didn't really save keystrokes) and many new snippets were added, to the
point where almost all D features and some very common Phobos features have a snippet.
There should be less bugs and the snippets have been designed to work together
(try
/catch
, sw
/case
, import
chains etc.), save even more keystrokes,
work with new D features etc.
The number of snippets may seem a bit overkill; a lot of that is due to operator snippets which are similar but always a bit too different to be joined, DDoc and some rather verbose interpolation code which I prefer to write in a readable way instead of clever one-liners.
- Phobos
- Branches
- Loops
- Contracts, asserts, tests
- Functions
- Exception handling
- Type definitions
- Metaprogramming and conditional compilation
- Operators
- DDoc and comments
- Various
al, as, avar, bsl, case, catch, class, cont, cvar, do, doctest, elif, else, enf, enum, Ex, exc, for, fore, forever, forif, format, fsw, fun, get, gpl, if, ife, imp, in, inter, invar, ivar, main, module, new, opApply, opAssign, opB, opCall, opCmp, opDis, opDollar, opIndexAssign, opIndex, opO, opSliceAssign, opSlice, opU, out, pack, Par, priv, prot, pub, Range, ret, Ret, scope, See, set, spn, struct, supe, sw, this, thr, Thr, toStr, todo, try, tryf, tup, union, utest, wh, with, wr, wrf,
std.exception.enforce
Automatically breaks the snippet into 2 lines if line length exceeds Vim textwidth
(can be set in .vimrc
). Can only break the snippet between the 2 parameters to
enforce.
std.string.format

Also in this sample: cvar
std.typecons.tuple

Also in this sample: avar
std.stdio.writeln, std.stdio.writefln
case, elif, else, fsw, if, ife, sw
if
Supports code wrapping.
else if
Supports code wrapping.
else
Supports code wrapping.
if followed by else

Used to create a static if / else pair. Also in this sample: ret
switch and case
sw
generates a switch with two case's and a default branch. When done, places
the cursor after the two cases so case
can be used immediately to add more cases.
final switch
Similar to sw but generates a final switch without a default.
do, for, fore, forever, forif, wh
do while loop
Supports code wrapping.
while loop
Supports code wrapping.

Also in this sample: if
for loop
Supports code wrapping.
foreach loop
Supports code wrapping.

Also in this sample: if
infinite for loop
Supports code wrapping.
foreach loop combined with if to filter elements
Usually, a better way to do this is to use std.algorithm.filter but it is (at least with current Phobos/DMD) sometimes easier/more readable to write performant code with a foreach/if combination.
Supports code wrapping.
as, doctest, in, invar, out, utest
assert
The 2-parameter version of assert is used by default as it's usually good practice to write that description string even if the assert may sem obvious.
Automatically breaks the snippet into 2 lines if line length exceeds Vim textwidth
(can be set in .vimrc
). Can only break the snippet between the 2 parameters to
assert.
in contract

Also in this sample: as
out contract

Also in this sample: as
class/struct invariant

Also in this sample: as
unittest block
a documentation unittest block
A unitest preceded by /// will add an example to the DDoc of the previous function/class/etc.

Also in this sample: as
get, fun, main, Range, set, this, toStr
function/method
fun
is one of the more involved snippets. The parameter list is analyzed to generate
a Params: DDoc section, although the parameter's descriptions need to be filled in
manually. The second-to-last tabstop allows to write the one-line DDoc description of
the function and to specify DDoc comment style; starting the description tabstop with
/// will use /// DDoc comments while starting with /** will use /* /
DDoc comments.

Notice that we change the documentation comment style by typing /** in the "Description" tabstop. Also in this sample: fore
constructor
Like fun, generates DDoc comments.
getter property
@property is not used as it's mostly considered a mistake now and may be deprecated in future.
setter property

Similarly to get, by default a setter sets a field with the setter's name suffixed by '_'. Also in this sample: as
the main() function

Also in this sample: wr
InputRange methods [alias trigger: InputRange
]
Many D types have range-style interfaces, of which InputRange is the most common
subset. Range
generates InputRange API stubs which can be filled in with its
tabstops.
toString() method
try/catch block, catch block, throw statement
Supports code wrapping.

try
is used to wrap 2 lines in a try/catch block, thr
to throw exception
from catch and catch
to add another catch block.
Also in this sample: if
try/catch/finally block
Supports code wrapping.

Also in this sample: wr
al, class, enum, exc, inter, struct, union
class definiton
The default class name is the source file name with uppercased first character.
struct definition
The default name of the enum/interface/union is determined similarly to class.
enum, interface, union definitions
The default name of the enum/interface/union is determined similarly to class.
type alias
Exception class definition
Creates a new exception type with a constructor taking a string, and implicitly taking the caller's source file and line.
debug, mix, smix, template, version
mixin statement
string mixin expression
Creates a 'macro-like' string mixin using std.string.format to insert values into the mixin at compile-time.

Also in this sample: cvar
debug block
Supports code wrapping.
version block
Supports code wrapping.
A plain template (not a template class/function) Like fun, generates DDoc comments.

Also in this sample: al
opApply, opAssign, opB, opCall, opCmp, opDis, opDollar, opIndexAssign, opIndex, opO, opSliceAssign, opSlice, opU
opDispatch
Like fun, generates DDoc comments for parameters (if any).

Also in this sample: ret
opAssign (the = operator) [alias trigger: op=
]

Also in this sample: spn
opDollar (the $ operator) [alias trigger: op$
]

Also in this sample: ret
opSlice (operator to get a slice of a container) [alias trigger: op[..]
]
Checks that the number of parameters is 0 or 2.

op[..]
used to create both a bounded opSlice (a[1 .. 3]) and "entire container"
opSlice (a[]).
Also in this sample: ret, spn
opIndex (operator to get an element of a container) [alias trigger: op[]
]
Checks that there is at least 1 parameter.

op[]
used to create a single-parameter and two-parameter opIndex
Also in this sample: ret
opSliceAssign (operator to assign to a slice of a container) [alias trigger: op[..]=
]
Checks that the number of parameters is 1 or 3.
opIndexAssign (operator to set an element of a container) [alias trigger: op[]=
]
Checks that there are at least 2 parameters.
opCall (function call operator) [alias trigger: op()
]
Like fun, generates DDoc comments for parameters.

Also in this sample: ret
opBinary (binary operators such as +, in and >>)
Generates an opBinary with a static if chain to overload all operators specified in
a string that is the first tabstop. E.g. +-in
will overload operators +, - and
in, while ^^^
will overload ^ and ^^. Checks that the string only contains
valid operators and contains no duplicates.
opUnary (unary operators such as -, ++ and ~)
Same as opB, but for unary operators.
opOpAssign (operator assignments such as +=, %= and >>=)
Same as opB, but for op assignment operators.
opCmp (comparison operator)

Also in this sample: ret
opApply (foreach "operator")
opApply implementations usually contain a loop which passes individual elements to the
foreach. opApply
generates most of the code to pass the elements, which the user
needs to wrap in a loop.

Also in this sample: fore
bsl, Ex, gpl, Par, Ret, See, Thr, todo
TODO comment
Ret
DDoc Params, Returns
Must be preceded with '/// '
or '* '
(i.e. single- or multi-line DDoc comments).
Par
can be suffixed by a count (1 to 5) of parameters; e.g. Par4
will create
a Params section with 4 items.
Thr
DDoc Throws
Must be preceded with '/// '
or '* '
(i.e. single- or multi-line DDoc comments).
Thr
can be suffixed by a count (1 to 2) of exception types thrown; e.g. Thr2
will create a Throws section with 2 items.
DDoc Example
Must be preceded with '/// '
or '* '
(i.e. single- or multi-line DDoc comments).
Supports code wrapping.

Also in this sample: struct
DDoc See_Also
Must be preceded with '/// '
or '* '
(i.e. single- or multi-line DDoc comments).
Boost Software License header
GPL2 header
avar, cont, cvar, imp, ivar, module, new, pack, priv, prot, pub, ret, scope, spn, supe, with
import declaration
module header
Uses the file name to set the module name, but package name must be specified by the user.
Can be combined with gpl to specify license (Boost is the default as it is the most common license for D projects).

Also in this sample: gpl
auto / const / immutable variable definition
continue statement
new (constructor usage) expression

Also in this sample: avar
public / private / protected / package protection attributes
return statement
builtin function attributes
Creates a sequence of function attributes (@safe, pure, nothrow, const and
@nogc). Every attribute is a tabstop, making it possible to pick attributes by jumping
between attributes and deleting the unwanted ones. For example, if both the ultisnips
'expand' and 'jump forward' triggers are <Tab>
, pressing
spn<Tab><Tab><Tab><Tab><Tab><Tab>
(9 keystrokes) will result in @safe pure nothrow
const @nogc while spn<Tab><Tab><BS><Tab><Tab><BS><Tab><BS><Tab>
(12 keystrokes)
will produce @safe nothrow.
Useful in combination with fun, this, operators and other snippets that create functions.

Also in this sample: ret

Also in this sample: ret
parent constructor call
scope guard
Supports code wrapping.
with block
Supports code wrapping.

Also in this sample: if