sponsor Vim development Vim logo Vim Book Ad

AutoClose : Auto close pair of characters

 script karma  Rating 447/145, Downloaded by 9726  Comments, bugs, improvements  Vim wiki

created by
Thiago Alves
 
script type
utility
 
description
*NOTE*
======
The header of the plugin says it is on version 1.1, but this is wrong. The 1.4 version is indeed the 1.4 version only with a wrong header. Next version I'll fix it.

Overview
=======
This plugin was  born  as  a  personal  need  to  have some little special features from other editors into my preferred one. The Eclipse IDE editor has an auto-complete for  open-close  pair  of  characters  feature  that I always wanted into Vim.

As soon  as you type  a character that  could have a  close pair,  Eclipse automatically puts its  pair in front of your cursor,  and  if you continue to type and type  the close character,  Eclipse is smart enough  to not insert it again (it just move your cursor one character forward).

But the Eclipse editor don't stop  there.  If you type an "open" character on any part of your code that  should not have a pair completion,  it will not insert the close character for you.

The AutoClose plugin is an attempt to reproduce this behavior on Vim.

Configuration
==========
1. Definning characters to auto close
--------------------------------------------------
If you  are not sattisfied  with the default  characters to close  you can define your own by setting the global variable g:AutoClosePairs.

The only thing to be careful is that this variable must be a dictionary on the following format:

    {"<open_char>": "<close_char>"}

Otherwise the  plugin will  not recognize  the settings  and will  use the default values.

Following is the default declaration for auto close chars. If this configuration is enought, you don't need to configure nothing:

    let g:AutoClosePairs = {'(': ')', '{': '}', '[': ']', '"': '"', "'": "'"}


2. Definning protected regions
-----------------------------------------
Inside some special  regions  the  auto  close  feature  is far from util, actually we  can say  that on those  regions,  this feature  is worst  than no feature at all. This is the case for "Comments" and "Strings".

If you edit files with this characteristic and there is more than comments and strings where you would like  to protect from auto close,  just define the global variable g:AutoCloseProtectedRegions.

This variable must be  a list of strings or the  plugin will not recognize it and will  use the defaults parameters.  The format  of this variable should be:

    ["<protected_region1>", "<protected_region2>", ...]

Following is the default declaration to the protected regions.  If you are satisfied with this, you don't have to set this setting:

    let g:AutoCloseProtectedRegions. = ["Comment", "String", "Character"]


3. Turning AutoComplete on and off
------------------------------------------------
As AutoComplete change the buffer you are editing, sometimes you just want to turn this feature off for a while and than turn it on again.

AutoComplete provide three  commands that can be mapped to  any key as you want to do that task:

    1. AutoCloseOn
    2. AutoCloseOff
    3. AutoCloseToggle

And if you don't want to map anything, just use them on the command mode.
 
install details
Put the script into your $VIM/plugin directory and the documentation into $VIM/doc and type on your vim:

   :helptags $VIM/doc

You can change $VIM to whatever place your vim can automatically read plugins and documentations like in your home vim directory.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
autoclose.vim 1.4 2008-09-04 7.0 Thiago Alves Fix a problem when use AutoClose on Visual Mode Blockwise. Unfortunatelly Backspace will not work as expected on this mode due Vim behavior. E.g.:
Select a column of text and press <Backspace> than <Delete> keys. You'll see that only the first line will be changed.
autoclose.zip 1.3 2008-08-25 7.0 Thiago Alves Fix a bug that prevent to automatically close pair of equal chars that the first one is entered on the very first column.
autoclose.zip 1.2 2008-08-24 7.0 Thiago Alves Fixed the not closing bug to string on some languages. Perhaps other strange behaviors also happens because of that!
autoclose.zip 1.1 2008-08-11 7.0 Thiago Alves Complete refactor of the plugin.
Remove all autocommands and drastically simplify the whole plugin.
This version was refactor thanks for the simple_pairs plugin that was launched on 08/10/2008 that point me to a much simpler way!
townkAutoCompleteChar.vim 0.9 2007-09-04 7.0 Thiago Alves Initial upload
ip used for rating: 44.206.248.122

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github