vim

Customizing snipMate to reduce repetitive tasks

Posted in vim on May 2nd, 2010 by jb55 – View Comments

snipMate is a vim plugin that allows you to paste snippets of code based on a keyword. To see what I mean, I put together a video that demonstrates how it works:

Most of the c++ snippets that were shown in the video were customized to my needs. snipMate has a bunch of basic snippets for c, c++, python, etc; but to get the most power out of snipMate you’ll want to start creating your own snippets.

Before we start, we’ll want to know where to put our snippets. Snippets are loaded from

~/.vim/snippets/<syntax>.snippets

where <syntax> is the context in which they are loaded. For example if the current document was loaded as a c++ file, it will automatically load the cpp.snippets, other snippet files will not be loaded (with the exception of the globals snippets file and the c.snippets in the case of the c++ file).

Creating your first snippet, an example

Open up the snippets file for the language you want to add snippets for, for this example I’ll using cpp.snippets.

If you’ve ever written code in C++ before, you may have experienced the pain of iterating over STL containers. Typing the iterator for loop has probably accumulated days of wasted time in my life. Now I can have my revenge:

for (std::vector<int>::const_iterator i = c.begin(); i != c.end(); ++i);
becomes


snippet iter
  for (${1:std::vector}<${2:int}>::${3:const_iterator} ${4:i} = ${5:c}.begin(); $4 != $5.end(); ++$4) {
    ${6}
  }

Taking a look at the original code and the snippet, you should already have some idea how these snippets work.

Placeholder: a tab location, this is where you set the placeholder value

${<placeholder>:<default_value>}
example: ${1:default}

Placeholder Value: This value gets automatically updated when it’s changed
$<placeholder>
example: $1

To test this out, open a c++ file and type:

iter<Tab>

It should expand the entire for iterator, allowing to you change types and tab between things you might need to change. Another advantage of this approach is that it reduces the amount of syntax errors you make, since there is a restricted set of things to change for a given snippet.

Using snippets for makefile templates

When starting new projects, I find myself looking for old projects of mine, copying the makefiles over, and then changing them so it works for my new project. While this works, lets see if we can take advantage of snipMate to remember templates for us! You can apply this idea to anything really.

I use cmake for alot of my projects, so I created a cmake.snippets and added this:

snippet base
	CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
	PROJECT(${1:ProjectName})

	FIND_PACKAGE(${2:LIBRARY})

	INCLUDE_DIRECTORIES(
		${$2_INCLUDE_DIR}
	)

	ADD_SUBDIRECTORY(${3:src})

	ADD_EXECUTABLE($1
	)

	TARGET_LINK_LIBRARIES($1
		${$2_LIBRARIES}
	)

snippet glob
	FILE(GLOB ${1:PROJ_SRCS} *.${2:cpp})

Pretty simple, I plan on adding a lot more cmake directives. The best part of snipMate is now I don’t have to remember every single cmake construct, I just need to remember simple keywords. As I said before, this idea can apply to any language, script, markup, etc.

More advanced snippets

To extend snipMate’s power further, it can execute vim expressions in a snippet using vim’s ‘eval()’.

Date example

snippet date
	`strftime("%Y-%m-%d")`

It would be interesting to see what other snippets people think of, comment and share!