This article discusses powerful plugins that improve the usability of development in VIM, and also provides valuable guidance on their refinement and use. Including used commands and hotkeys.
Considered setting through the
vimrc configuration file and provides links to articles describing individual points.
Someone VIM alienate, seeming at first too difficult or inconvenient. However, do not underestimate its flexibility and ability to customize yourself, because from VIM you can literally fashion an ideal editor / IDE as you imagine it.
')
I hope that this article will help the reader to see this. Of course, it is primarily aimed at experienced users of Vim, but beginners can learn a lot from it.
I tried to write as compactly as possible, but without losing the content, but the article was still quite voluminous. The material is logically divided into several sections and it is not at all necessary to read it in order. Nevertheless, I highly recommend reading the information on installing plugins and configuring
vimrc .
The article discusses filetype plugins for working with different programming languages and different file types, and common plugins.
I occasionally had to use Vim to edit files over SSH. But Vim has long remained a mystery to me, and I was afraid that I would do something wrong. In the full transition to Vim, the plug-ins described here helped me, which made this process easier and more pleasant, added convenience to the work, and pushed me to further study this editor.
I occasionally spend time exploring Vim's various features, and every time I admire this editor. His huge on-line and online documentation carries a lot of what he can, and what I don't know yet.
Comfortable and pleasant environment contributes to productivity in work. This can be said about the development environment, because it is one of the main tools for the developer.
I noticed one psychological feature. Starting to work with a new IDE for yourself, which seemed very convenient in work, the workflow becomes much more pleasant. But everything becomes boring, becomes commonplace. This does not apply to Vim. It is very extensible thanks to the built-in language, and has the support of different programming languages to increase its capabilities.
A good tool should be an extension of our hands. And only each of us individually knows better than others how an ideal tool should look specifically for him.
Dear readers, you are critics of this article. Everything described in this article works. But if something does not work for you, or you know a better solution, please write in the comments about it. Your constructive criticism is very important.
I hope that you will like the article and it will be useful.
Plugin Overview
In this article I will consider the following common plugins:
- bufexplorer - easy navigation through open buffers
- NERD Commenter - support commenting code for different languages
- NERD Tree - convenient navigation through the tree of files and directories
And the following filetype plugins:
- syntax highlighting support for php and javascript, which make it more efficient than standard ones.
- Autocompletion with omnicomplection for php, javascript, xslt, sql
- auto-tagging for xslt, xsl, xml, xhtml, html.
- Autodiscovery, check php script for errors
I performed all the actions on my laptop with Ubuntu 8.10 with vim-full.
Vimrc settings that make using Vim more enjoyable
My vimrc file is in
/ etc / vim / vimrc . You can have it, for example in
/ etc / vimrc . As a rule, it depends on the distribution.
I originally set up the configuration file for
this good article , so here I want to tell you what I added and why.
I have to work with different projects, some use the utf8 encoding, others cp1251, and when using
One of these encodings when working with a file of another on the screen does not display Russian letters correctly. I decided to correct the situation and automate it by specifying to Vim the possible encodings of the files and the sequence of their definition. I do not remember the features of utf8, but the file can determine whether it is in this encoding or not. If not, Vim will use the second one.
With this setting, Vim does an excellent job with auto-detect encoding:
" , ,
set termencoding=utf-8
" .
set fileencodings=utf8,cp1251
On the advice of
Paul in
this comment, it is advisable to also set the ut8 encoding parameter:
set encoding = utf8
The documentation states that this is also strongly recommended for GTK + 2. This option does not change the file encoding. That is, for example, files in cp1251 will be displayed correctly on the screen, and will be saved in their encoding.
Most often you have to work with mysql dump files. Vim has SQL support, but the DBMS is a lot, and they differ from each other, for example, by the column types, and I decided to configure the MySQL DBMS support by default:
if has ("autocmd")
autocmd BufRead * .sql set filetype = mysql
endif
Plugin Installation Overview
Plugins on the Vim site are usually provided either in the form of archives, or simply a plug-in file with the
.vim extension. On the page of the plug-in usually describes the type of plug-in that prompts which folder to put it. And also usually at the bottom of the page there is an Install details section, which describes the process of installing and configuring the plugin.
Common plugins, as I noted (at least discussed in this article), are designated as utility and are accordingly placed in the
~ / .vim / plugin folder. Often, the very structure of folders in the archive with a plugin tells you where to put the plugin. Plug-ins, for example designated as syntax, are placed in the
.vim / syntax folder. And as the name implies, they provide support for syntax highlighting of a particular language (s).
If I deviate in the installation of the plug-in from the instructions on the page of the plug-in, I will describe the installation process, otherwise it is assumed that the plug-in is installed exactly as described on its page.
If the documentation file comes to the plugin as a txt file, then it is placed in the
~ / .vim / doc directory. After adding the documentation to the plugin, run Vim, and execute the command to generate (update) the tag file for all documentation in the doc folder
: helptags $ HOME / .vim / doc
If the command was silently executed, then everything went well :) Now the documentation for the installed plugin after restarting Vim will be available with the command: help. For example, for the NERD Tree plugin, type:
: help NERDTree
BufExplorer
link to plugin
Very handy plugin to switch between open buffers. There is a documentation file with the plugin file. After installation, the documentation will be available on command.
: help bufexplorer
For the convenience of working with the plugin, I created hot keys to open a window with a list of open buffers and switch between buffers in the
vimrc file:
nmap <C-F5> <Esc>: BufExplorer <cr>
vmap <C-F5> <esc>: BufExplorer <cr>
imap <C-F5> <esc>: BufExplorer <cr>
"F6 - previous buffer
nmap <C-F6>: bp <cr>
vmap <C-F6> <esc>: bp <cr> i
imap <C-F6> <esc>: bp <cr> i
"F7 is the next buffer
nmap <C-F7>: bn <cr>
vmap <C-F7> <esc>: bn <cr> i
imap <C-F7> <esc>: bn <cr> i
nmap ,
vmap ,
imap describe the combination of a key in the normal, visual and insert mode of the editor.
<cr> - enter
i at the end of the command that is hung on the key combination means to go into insert mode.
This is done by me, because on F1..F11 the debugger teams are hung. You can make it as convenient for you.
NERD Commenter
link to pluginThe plugin makes it more convenient commenting code for different types of files. The list of supported file types is on the plugin page.
I use the following commands:
, cc - comment out the line with single-line comments. The team is very effective in visual mode. Because it puts the selected text in the comments.
, cm - the same, only one multi-line comment
, cu - uncomment a line, if a multi-line comment block is selected, it will be uncommented. Also, to uncomment a multiline comment, you can apply it on the first and last line of the comment block. Anywhere line.
, c <space> - inverts the state of the string line. That is, they were uncommented, became uncommented, and vice versa.
, c $ - is useful when you need to comment from the cursor to the end of the line
Nerd tree
link to pluginFrom the plugin page:
“What is this„ NERD tree “??
Check out this demo www.flickr.com/photos/30496122@N07/2862367534/sizes/o „
Yes, the demo does not demonstrate the capabilities of this plugin. On the one hand, it is quite simple, on the other hand, this is what we are all used to in various IDEs - viewing files and directories in the form of a tree. It can easily replace the project browser, such as Zend Studio, as any directory can be selected as the current root. By default, Nerd Tree makes root the directory from which Vim is running.

On the left, the directory tree displayed by NERD Tree. The NERD Tree documentation is open in the upper window. The built-in documentation on it is very informative.
Frequently used commands:
o - Open / close the directory in the tree, open the file and go to the window with the file.
go - Opens a file, but the cursor remains on the NERD Tree.
i - open the file in a separate window, the division goes horizontally.
gi is the same as i, but the cursor remains on the NERD Tree
C - make the current directory root
u - make the root directory higher than the current one.
cd - change
CWD to the directory pointed to by the cursor. That is, relative paths in such commands as: e,: o will now report from this directory.
Easy modification
I decided that it’s convenient when the tree always opens, so
I added a line to the end of the
NERD_tree.vim file:
call s: initNerdTree (getcwd ())
I also assigned hotkeys to open and close NERD Tree in vimrc:
nmap <CN> v: NERDTree <cr>
vmap <CN> v <esc>: NERDTree <cr> i
imap <CN> v <esc>: NERDTree <cr> i
nmap <CN> x: NERDTreeClose <cr>
vmap <CN> x <esc>: NERDTreeClose <cr> i
imap <CN> x <esc>: NERDTreeClose <cr> i
Plugins to improve PHP support
FileType plugin for PHP
Link to plugin
There is no description for the plug-in; I found the link to the plug-in on the page of the list of plug-ins to support various
programming languages -
www.vim.org/vimscriptlinks.php . And apparently this plugin is recommended.
From the useful that I noticed when viewing the plug-in code - it jumps on functions, class methods using
]] and
[[ .
The plugin is installed in the folder with filetype plugins. I assumed that I could have several such plugins for php and therefore I created a separate folder for them -
~ / .vim / ftplugin / php / and put the plugin there.
But of course this is not enough and I decided to modify the plugin, and add the php manual in the form of the built-in VIM documentation, the ability to view the documentation for pressing the
K key for the function or construction of the language above the cursor, the sql backlight, and html for php codes . As well as checking for errors in the script, which is in the current buffer with the command: make. I also added the word for auto-completion of standard PHP functions.
All the lines below I add to the php.vim file before these last lines of the plugin:
"Restore the saved compatibility options.
let & cpo = s: save_cpo
Which, as I understand it, after testing the plug-in, just in case, set the compatibility options back,
provided previously saved in the variable
s: save_cpoFirst of all, I added a PHP script check in the current buffer with the command
: make. "PHP syntax check
set makeprg = php \ -l \%
"PHP error output format
set errorformat =% m \ in \% f \ on \ line \% l
I found a php manual in Vim help format for this link
vim.wikia.com/wiki/PHP_manual_in_Vim_help_formatDirect link to the
planetxml.de/php_manual.txt manual
download . Also on this Vim Wiki page there is a link to the script with which this manual was created.
The manual is put in the daddy
~ / .vim / doc respectively and tags are created for it. After that, you can get help on any php function, like this:
: help func_get_args
Also on the page of the
PHPCollection plugin
, I found two links to dictionaries of the function list and the list of function prototypes for PHP:
www.leon.w-wa.pl/texts/phpfunclistwww.leon.w-wa.pl/texts/phpprotoAnd put them in the folder
~ / .vim / dic /I also downloaded the list of functions from Rasmus Lerdorf’s website -
lerdorf.com/funclist.txtand copied it to the
~ / .vim / dic / php file
And in the plugin below, I wrote the lines already added by me:
setlocal dictionary + = $ HOME / .vim / dic / phpfunclist
setlocal dictionary + = $ HOME / .vim / dic / phpfunclist
setlocal dictionary + = $ HOME / .vim / dic / phpproto

Next, from the
PHPCollection plugin, which is an archive of several scripts, I took the phpmanual.sh script, and placed it in the
~ / .vim / external / directory
gave him the right to perform
chmod + x phpmanual.sh
and changed
MANUAL_DIR in it:
MANUAL_DIR = "$ {HOME} /. Vim / manual / php /"
Then you need to download from the
official site php in the form of "many html files." This is an archive, after it is unpacked, the html directory appears. All its contents need to be copied to the folder
~ / .vim / manual / php /An example of unpacking the archive in tar.gz format:
tar zxvf arhive_name.tar.gz
And now in the same plugin for php under the already added lines, I added another one:
setlocal keywordprg = $ HOME / .vim / external / phpmanual.sh
Great, now when I press the
K key, when the cursor is over the function for which I want to get the documentation, I get into lynx, with the documentation page for this function open :)

Embedded PHP documentation in Vim

After pressing K on array_shift (... I got into lynx on the documentation page for this function
Plus it is worth adding HTML and SQL highlighting in lines in PHP code.
let php_sql_query = 1
let php_htmlInStrings = 1
PHP Indent Plugin
link to pluginWith this plugin, Vim intelligently indents PHP code. I can hardly tell you more about the capabilities of the plugin, its configuration and installation, than its official page on vim.org.
PHP Syntax Plugin
link to pluginThis plugin provides excellent syntax highlighting for php code. Of the nice features worth noting is that
highlighting of standard classes, interfaces, constants, other than highlighting them, but declared by the programmer.
Pairs of curly braces differ in color for functions / methods, classes, and blocks of cycles and conditional blocks. This is necessary, especially when you need to deal with someone else's code, where indents are badly placed, and it is not always clear where the conditional block ends, where the function is. Nice PCRE regex highlighting. All features of the plugin and its installation process are painted on its page.

Highlighting the syntax makes parsing the code, even if it is poorly formatted, easier. And highlighting SQL in rows increases its readability, even if the queries are not split into several lines and not formatted, such as in Drupal /
PDV - phpDocumentor for Vim
link to pluginThis plugin greatly simplifies the work with documenting code in the style of phpDocumentor. And it makes it much more efficient than ZendStudio 5.5, which I used for a long time (I didn’t move to the 6th and didn’t impress) until I switched to Vim :)
The plugin allows you to determine the Package to which the classes, files, the author of the source codes, version, copyrights, and license belong. The following variables in the plugin file are responsible for this:
g: pdv_cfg_Type
g: pdv_cfg_Package
g: pdv_cfg_Version
g: pdv_cfg_Author
g: pdv_cfg_Copyright
g: pdv_cfg_License
Of course, you can override them from the Vim command line:
: g: pdv_cfg_Package = "Core"
And in the end, when commenting on classes, the @package Core tag will be inserted.
The most commonly used command is <CP>, which creates a comment above the line, considering what the line represents. Comments for methods and functions, classes, class attributes are generated differently.
Installation
I installed the plugin not as it is written on the plugin, but in the ftplugin / php / folder, that is, I have it at ~ / .vim / ftplugin / php / php-doc.vim. Thus, the plugin is used only for php files.
Plugin completion
When using the plugin, I noticed that it correctly creates comment blocks for attributes of classes declared as
static and incorrectly for those declared as
protected / public / private static , for example:
protected static $ var = false;
To remedy this situation, and over time it begins to be a little upsetting, it is necessary to work on updating the plug-in. The actions described below are performed on the latest version - 1.0.1. And judging by the release date, the author has ceased to support the plugin. So it is better to modify yourself. I will not explain every line of code, I think you yourself can figure out what's what. In addition, I thoroughly understand the writing of plug-ins and I am preparing material on writing plug-ins on Vim Script, which I will gladly post later :)
Line 126 of the plugin, which is a regular expression, should be replaced by the following:
let g: pdv_re_attribute = '^ \ s * \ ([private \ | public \ | protected \ | var \ | static] \ + \) \ s * \ (static \ | \ s * \) * \ $ \ ([ ^; =] \ + \) [=] * \ (. * \); \? $ '
On line 103 we change the value of the global variable:
let g: pdv_cfg_php4guessval = "public"
Let's make changes to the PhpDocVar () function.
Lines 339 - 342 are replaced by:
let l: modifier = substitute (l: name, g: pdv_re_attribute, '\ 1', "g")
let l: varname = substitute (l: name, g: pdv_re_attribute, '\ 3', "g")
let l: default = substitute (l: name, g: pdv_re_attribute, '\ 4', "g")
Replace with:
let l: modifier = substitute (l: name, g: pdv_re_attribute, '\ 1', "g")
let l: static = substitute (l: name, g: pdv_re_attribute, '\ 2', "g")
let l: varname = substitute (l: name, g: pdv_re_attribute, '\ 3', "g")
let l: default = substitute (l: name, g: pdv_re_attribute, '\ 4', "g")
line 344:
let l: static = g: pdv_cfg_php4always == 1? matchstr (l: modifier, g: pdv_re_static): ""
Replace with:
let l: static = match (l: modifier, "static") == 0? "static": l: static
And after that, we correctly generate the
access and
static tags for variables of the form:
private $ var;
protected $ var;
public $ var;
private static $ var;
protected static $ var;
public static $ var;
static $ var;
var $ var;
static is now always substituted when the
static variable modifier is specified. And for the last two examples, when the access modifier is not specified,
access is set to
public , thanks to the default value
g: pdv_cfg_php4guessval
That's it, now this handy plugin for creating phpDocumentor comments is even better :)

A comment to the blog_block function, which is a framework with a basic description of the parameters was generated using the PDV plugin.
Javascript
Here I wanted to write about plugins to support syntax and javascript autocompletion. But Vim is initially a good support for javascript plugins that are installed along with Vim, with omnicompletion autocompletion, available by default for the <Co> <Cp> combination in edit mode.

XML, XHTML, XSL, XSLT
This all makes
one filetype plugin .
The description on the plugin page is impressive. I do not undertake to retell it here. I will add that described in the installation
I highly recommend creating symbolic links. In this case, the possibilities of the plugin simplify editing XHTML documents. And XHTML is a subset of XML. And there will be support for auto-completion of tags XSL, XSLT.

XSLT autocompletion
Writing an article helped me:
Also a special thank you to all those who write to the Vim blog and take an active part in the discussions, giving good tips, links to useful resources.