Productive work in vim using snipMate

In this article I want to talk about one great plugin for vim, which significantly speeds up code writing, layout and text editing.

snipMate - allows you to quickly insert a text template into the document using the keyword + and provides convenient navigation on the inserted template.


Installation

  • Download the latest version of the extension - here .
  • Unpack the archive.
  • Copy the contents to ~ / .vim /
  • The plugin is installed.
For the extension to work, add the following lines to the configuration file (vimrc) :
set nocompatible
filetype on
filetype plugin on

Snippets


The text patterns used by snipMate are called snippets.
They have a separate directory in the ~ / .vim folder with the same name.
The archive we downloaded has a basic set of templates for various programming languages.

Let's use a template for a C-like for loop , for this, in any file with the extension .c or .cpp you need to enter in the insert mode for( denotes pressing the tab key) and immediately get the following construction:
	for (i = 0; i < count; i++) {
		/* code */				
	}

The template has 4 editable zones, between which you can switch using the tab key ( Shift + tab for reverse switching)
	for (/*2*/ = 0; /*2*/ < /*1*/;  /*2*//*3*/) {
		/*4*/			
	}

When the focus switches to the next zone, this zone is highlighted, while we are in edit mode, that is, entering new text completely erases the old one.
We see 3 instances of the zone / * 2 * / . This means that when editing changes are applied to all three instances.

An example of writing a simple loop:
" for 100 myVar + = 10 // some"will create the following code fragment:
	for (myVar = 0; myVar < 100; myVar+=10) {
		//some
	}


This is how it looks in practice - video .

Conveniently? You decide.

The most important thing about this plugin is its extensibility. We can develop our own templates and use them to the full.

Writing your own snippet


As we already know, all snippets are in ~ / .vim / snippets.
It should be noted that in the archive we downloaded there is a file “syntax / snippet.vim” , which means that we will be pleased with the syntax highlighting when editing snippet files in our favorite editor.
  • Open the snippets file for the language we need.
    In our case, the language is C , therefore the file is ~ / .vim / snippets / c.snippets
  • Add your snippet following the syntax:
    snippet <ключевое слово>
    	<тело шаблона>

    Where <keyword> is the word after which you will need to press tab to get the <template body> , and you must indent one tab in the first line of the template. To describe our actions, we can use lowercase comments that begin with the "#" character . The use of comments is allowed only outside the body of the template. So add:
    snippet hello
    	printf("Hello, world!");
    
  • Save the file. Open "foo.c" and check our snippet - enter hello in edit mode- we get printf ('' Hello, world! ''); .
We wrote our own snippet , but it hurts too simple and resembles the vim abbreviation. Therefore, let's take a closer look at the body of the template.

Expanded text


By default, after clicking the tab, the cursor is positioned at the end of the template.
  • To place the cursor in the right place, you can use the construction "$ {#}" ,
    where # is the number of the label (tab stop), which determines the order of movement when you press tab .
  • Default text can be attached to the label using "$ {#: text}" .
  • To get the effect of a chain reaction when changing text on a certain label, you need to use the variable structure "$ #" , where # is the number of the label with text already in use. Changing the text of a certain label entails a change in all variables that have the same number as the label. An example of such a “chain reaction” is the for loop , or rather, changing the name of its counter variable.
All of these structures can be nested in each other to achieve the desired result. Let's write a template for a simple function that returns the square of the accepted argument.
snippet sq
	${1:int} $1_sqr($1 x){
		return x*x;
	} ${2}

Now apply it. Open “foo.c” and enter in edit mode: sq double
As a result, we get:
double double_sqr(double x){
	return x*x;
}
Just two words and the function is ready!

Options Templates


It may be very useful to define a template with options. You can implement this design as follows:
snippet <ключевое слово> <описание шаблона 1>
	<тело шаблона 1>
snippet <ключевое слово> <описание шаблона 2>
	<тело шаблона 2>
	...
snippet <ключевое слово> <описание шаблона N>
	<тело шаблона N>

All descriptions of templates have the same keyword, but different implementation. When you try to use such a template, a dialog appears in which you can see descriptions of all suitable templates and make a choice by entering the desired number.
Let's redefine the template for the function from the previous example:
snippet sq int_sqr
	int int_sqr(int x){
		return x*x;
	}
snippet sq double_sqr
	double double_sqr(double x){
		return x*x;
	}
snippet sq someType_sqr
	someType someType_sqr(someType x){
		return x*x;
	}

When entering sq in edit mode we get the following dialog:
1. int_sqr
2. double_sqr
3. someType_sqr
Type number and  or click with mouse (empty cancels): 

Now to get a function, just enter one word and make a choice.

Using Vim Script


The template may contain an excerpt of a wim script that is interpreted when a snippet is inserted into a document. The system () function, which executes an external command and directs the output to the insertion point, can be very useful. The script must be taken in `` (often confused with '' ).
Let's look at an example using system ():
snippet today
	Today is `system("date +%Y-%m-%d")`

Upon insertion, we get: Today is YYYY-MM-DD

Combining Pattern Sets


To activate snippets from several files when editing a single document, just use the instruction:
:set ft=.
It should be noted that the type of the current edited file must be indicated first so as not to lose syntax highlighting. For example, to activate HTML templates when editing * .c, you need to do:
:set ft=c.html


I hope this becomes useful to someone. More information about snipMate can be found in the file ~ / .vim / doc / snipMate.txt .

References


The official vim
page The snipMate page at vim.org
Demo video

Also popular now: