📜 ⬆️ ⬇️

Productive work in vim using snipMate

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

snipMate - allows you to quickly insert a text template into your document using the + <tb> keyword and provides easy navigation through the inserted template.


Installation

For the extension to work, you must add the following lines to the configuration file (vimrc) :
set nocompatible filetype on filetype plugin on 

Snippets


Text patterns that snipMate operates on are called snippets.
There is a separate directory for them 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 the template for a si-like for loop, to do this in any file with the extension .c or .cpp you need to enter in insert mode for <tb> (<tb> means 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 a new text completely erases the old one.
We see 3 copies of the zone / * 2 * / . This means that when editing, changes are applied to all three instances.

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


Here's 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.

We write 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.
We wrote our own snippet , but it hurts too much and resembles a vim-abbreviation. So let's take a closer look at the body of the template.

Expanded text


By default, after pressing the tab, the cursor is placed at the end of the template.
All of these designs can be invested in each other to achieve the desired result. Let's write a simple function template that returns the square of the argument received.
 snippet sq ${1:int} $1_sqr($1 x){ return x*x; } ${2} 

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

Templates with options


Defining a template with variations can be very useful. You can implement this construction as follows:
 snippet < > <  1> <  1> snippet < > <  2> <  2> ... snippet < > <  N> <  N> 

All template descriptions 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 you enter sq <tb> in edit mode, we get the following dialog:
 1. int_sqr 2. double_sqr 3. someType_sqr Type number and <Enter> or click with mouse (empty cancels): 

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

Using Vim Script


A template can contain a fragment of a wim script, which is interpreted when inserting a snippet into a document. The system () function, which executes an external command and sends 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")` 

When inserting we get: Today is YYYY-MM-DD

Combining Template Sets


To activate snippets from several files when editing a single document, it is enough to use the instruction:
 :set ft=<currentFiletype>.<otherFiletype> 
It should be noted that first you need to specify the type of the current edited file, in order not to lose the syntax highlighting. For example, to activate HTML templates when editing * .c you need to do:
 :set ft=c.html 


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

Links


Official vim page
SnipMate page on vim.org
Demo video

Source: https://habr.com/ru/post/125355/


All Articles