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
- Downloading the latest version of the extension - here .
- Unpack the archive.
- Copy content to ~ / .vim /
- Plugin installed.
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++) { }
The template has 4 editable zones, between which you can switch using the
tab key
( Shift + Tab for reverse switching) for ( = 0; < ; ) { }
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) {
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 / snippetsIt 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.
- To set the cursor in the right place, you can use the construction "$ {#}" ,
where # is the label number (tab stop), which determines the order of movement when you press the tab . - Default text can be attached to a label with "$ {#: text}" .
- To get the effect of a chain reaction when changing text on a label, you need to use the "$ #" variable construction, where # is the number of the label already used with the text. Changing the text of a label entails changing all variables that have the same number as the label. An example of such a "chain reaction" is a for loop, or rather a change in the name of its counter variable.
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> doubleAs 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-DDCombining 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 pageSnipMate page on vim.orgDemo video