Uma Lição de VIM #15.6: Plugins – Auto-pairs

(Essa é a parte em que o VIM fecha as coisas pra você.)

O sexto plugin da lista de plugins que eu vou falar é Auto-Pairs. Como de costume, para instalar o plugin pelo Vundle:

Bundle ‘jiangmiao/auto-pairs’

Uma vez instalado o plugin, você verá que cada ', ", (, [ e { irá, automagicamente, gerar o seu respectivo ', ", ), ] e }. Simples assim.

Entretanto, Auto-Pairs não funciona bem com o modo visual (se você já usou TextMate ou Sublime Text, deve ter percebido que se você usar uma das combinações acima com uma área selecionada, o editor vai “circular” a área selecionada com o par indicado). O que existe é [Alt]e em modo de inserção que faz a troca rápida entre o caractere de fechamento de pair com a próxima palavra.

Por exemplo, se você tiver

auto

… com o cursor posicionado entre as duas aspas e usar [Alt]e, você irá ficar com

auto

Ainda, o Auto-pairs tem um controle para “pular” sobre os caracteres de fechamento já existentes. Se ele adicionar um “)” (que deve ficar antes do cursor), quando você pressionar “)” no seu teclado, ao invés de adicionar mais um “)”, o cursor irá pular o “)” existente.

Embora isso pareça bom, em certos casos pode ser um problema. Por exemplo, caso você tenha o seguinte trecho de código:

$(function () {
  $(‘#button’).on(‘click’function () {

})

Se você tentar fechar a função interna, o Auto-Pairs irá, na verdade, saltar para o fechamento da função externa, efetivamente “proibindo” que você feche as funções corretamente. Soluções são:

  • Fechar a função externa de novo, forçando o que era para encerrar a função externa em encerramento da função interna (depois você pode alinhar as duas novamente usando o modo visual e = em modo normal);
  • Copiar e colar o fechamento da função externa;
  • Desligar o “salto” de elementos de fechamento com :let g:AutoPairsFlyMode = 0 (e, se você realmente preferir que o salto nunca seja feito, pode adicionar esta linha no seu vimrc);
  • Desligar temporariamente o Auto-Pairs com [Alt]p em modo de inserção (usar [Alt]p novamente irá reativar o Auto-Pairs).

My list of VIM Bundles

Not that anybody asked me, but I recently ditched Sublime Text 2 for VIM… again.

But here is the thing: I just ditched it ’cause I’m using a lot of bundles to change VIM default behaviour, adding more feature and that fuss. The ones I’m using:

Vundle
Vundle is the plugin I’m currenctly using to manage all the other plugins. Vundle is a really simple way to keep your other plugins up-to-date. All the other plugins listed here were installed using Vundle.

vundle

Powerline
Powerline changes the default “last status” line to provide a lot more information. The cool thing about Powerline is that it comes not only with a VIM plugin, but also with a Bash prompt and an iPython prompt, so everything have (almost) the same look.

powerline

Vim-Fugitive
I got Vim-Fugitive to display the current branch in the old Powerline plugin (the current one does that already). One of the things I still try to use is :Gstatus, which display the current changes in the index. The problem here is that the display, although pretty familiar, it’s a mess when you’re dealing with a single file (it shows “Changes not staged for commit” and then, when you add it to the index, it just change that to “Changes staged for commit”, which is pretty easy to miss in the list of things that are shown).

fugitive

Vim-Commentary
Vim-Commentary lets you comment a block of text pretty quickly, which is useful. The only issue with it is that it follows the current file type (as VIM does not have a “block type” or “region type” support), so trying to comment a few lines of JavaScript in an HTML file will produce HTML comments, not JavaScript/C++ comments. Still pretty useful to quickly comment that one line in your Python code.
Tabular
Tabular is a hard plugin to master, but really useful. With it, you can realign your code on custom characters (for example, align your variable definition on the “=” character). Or make all your HTML/XML properties align on “=”. And so on.
Auto Pairs
Auto-pairs does exactly what it says: It complete pairs of characters, adding a second " after your first ", the closing bracket and so on.
Vim-Flake8
On our Python project, we have a strictly “follow PEP8” policy. With Vim-Flake8, PEP8 errors are displayed in the error window for easy access (and fix).

flake8

SnipMate
SnipMate is a TextMate-like snippet plugin, but really easy to use. Type the snippet name, press [Tab] and it will complete the snippet, positioning the cursor on the marker areas.

snipmate

CtrlP
Again, a plugin inspired on TextMate/Sublime Text: Fuzzy file search. It allows faster file switching by doing fuzzy search on filenames and directories, to the point that I barely use the open command/dialog these days. Also, you can chose to open the file in the current buffer, new split window or new tab.

ctrlp

Vim Multiple Cursors
Again, a feature based on TextMate/Sublime Text: Selection of multiple elements and change in those selected. It is a more visual way of doing :s/<original>/<replacement>.

multiple-cursors

After seeing this, you’re probably asking yourself: But with all those features based on Sublime Text, why not use Sublime Text directly? Well, for one, I’m a fan of the VIM movement/selection way of doing things; second, I use “jump to mark” pretty often (including “jump to last changed area in this file” and “jump to the previous jump place”) and Sublime does not have that; third, I prefer the way VIM colorizes the code, even if I try to use the same colorscheme in both editors (the difference here is the syntax file, so…); fourth, even if Sublime have a Vintage mode, most of the things I use (like smart jumps) do not exists.

Also, some bugs in plugins/Sublime let me down from time to time: Ctrl+R, sometimes, start in visual mode when Vintage is on (in other words, as soon as you find the definition you’re looking for, you start selecting text); Flake8 for Sublime manages, from time to time, to move the cursor to focus nothing, so you’re forced to move your hand to the mouse and click on the text again to be able to do anything.