sponsor Vim development Vim logo Vim Book Ad

Auto Pairs : Insert or delete brackets, parens, quotes in pair.

 script karma  Rating 411/140, Downloaded by 13031  Comments, bugs, improvements  Vim wiki

created by
Miao Jiang
script type
Repository: https://github.com/jiangmiao/auto-pairs

Auto Pairs
Insert or delete brackets, parens, quotes in pair.

copy plugin/auto-pairs.vim to ~/.vim/plugin

or if you are using `pathogen`:

```git clone git://github.com/jiangmiao/auto-pairs.git ~/.vim/bundle/auto-pairs```

*   Insert in pair

        input: [
        output: [|]

*   Delete in pair

        input: foo[<BS>
        output: foo

*   Insert new indented line after Return

        input: {|} (press <CR> at |)
        output: {

*   Insert spaces before closing characters, only for [], (), {}

        input: {|} (press <SPACE> at |)
        output: { | }

        input: {|} (press <SPACE>foo} at |)
        output: { foo }|

        input: '|' (press <SPACE> at |)
        output: ' |'

*   Skip ' when inside a word

        input: foo| (press ' at |)
        output: foo'

*   Skip closed bracket.

        input: []
        output: []

*   Ignore auto pair when previous character is \

        input: "'
        output: "'"

*   Fast Wrap

        input: |'hello' (press (<M-e> at |)
        output: ('hello')

        wrap string, only support c style string
        input: |'h\\el'lo' (press (<M-e> at |)
        output ('h\\ello'')

        input: |[foo, bar()] (press (<M-e> at |)
        output: ([foo, bar()])

*   Quick jump to closed pair.


        (press } at |)



*   Support ``` ''' and """



*   Delete Repeated Pairs in one time

        input: """|""" (press <BS> at |)
        output: |

        input: {{|}} (press <BS> at |)
        output: |

        input: [[[[[[|]]]]]] (press <BS> at |)
        output: |

*  Fly Mode

        input: if(a[3)
        output: if(a[3])| (In Fly Mode)
        output: if(a[3)]) (Without Fly Mode)


        (press } at |)


        (then press <M-b> at | to do backinsert)

        See Fly Mode section for details

Fly Mode
Fly Mode will always force closed-pair jumping instead of inserting. only for ")", "}", "]"

If jumps in mistake, could use AutoPairsBackInsert(Default Key: `<M-b>`) to jump back and insert closed pair.

the most situation maybe want to insert single closed pair in the string, eg ")"

Fly Mode is DISABLED by default.

add **let g:AutoPairsFlyMode = 1** .vimrc to turn it on

Default Options:

    let g:AutoPairsFlyMode = 0
    let g:AutoPairsShortcutBackInsert = '<M-b>'


    System Shortcuts:
        <CR>  : Insert new indented line after return if cursor in blank brackets or quotes.
        <BS>  : Delete brackets in pair
        <M-p> : Toggle Autopairs (g:AutoPairsShortcutToggle)
        <M-e> : Fast Wrap (g:AutoPairsShortcutFastWrap)
        <M-n> : Jump to next closed pair (g:AutoPairsShortcutJump)
        <M-b> : BackInsert (g:AutoPairsShortcutBackInsert)

    If <M-p> <M-e> or <M-n> conflict with another keys or want to bind to another keys, add

        let g:AutoPairShortcutToggle = '<another key>'

    to .vimrc, if the key is empty string '', then the shortcut will be disabled.

*   g:AutoPairs

        Default: {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'}

*   b:AutoPairs

        Default: g:AutoPairs

        Buffer level pairs set.

*   g:AutoPairsShortcutToggle

        Default: '<M-p>'

        The shortcut to toggle autopairs.

*   g:AutoPairsShortcutFastWrap

        Default: '<M-e>'

        Fast wrap the word. all pairs will be consider as a block (include <>).
        (|)'hello' after fast wrap at |, the word will be ('hello')
        (|)<hello> after fast wrap at |, the word will be (<hello>)

*   g:AutoPairsShortcutJump

        Default: '<M-n>'

        Jump to the next closed pair

*   g:AutoPairsMapBS

        Default : 1

        Map <BS> to delete brackets, quotes in pair
        execute 'inoremap <buffer> <silent> <BS> <C-R>=AutoPairsDelete()<CR>'

*   g:AutoPairsMapCR

        Default : 1

        Map <CR> to insert a new indented line if cursor in (|), {|} [|], '|', "|"
        execute 'inoremap <buffer> <silent> <CR> <C-R>=AutoPairsReturn()<CR>'

*   g:AutoPairsCenterLine

        Default : 1

        When g:AutoPairsMapCR is on, center current line after return if the line is at the bottom 1/3 of the window.

*   g:AutoPairsMapSpace

        Default : 1

        Map <space> to insert a space after the opening character and before the closing one.
        execute 'inoremap <buffer> <silent> <CR> <C-R>=AutoPairsSpace()<CR>'

*   g:AutoPairsFlyMode

        Default : 0

        set it to 1 to enable FlyMode.
        see FlyMode section for details.

*   g:AutoPairsShortcutBackInsert

        Default : <M-b>

        Work with FlyMode, insert the key at the Fly Mode jumped postion

Buffer Level Pairs Setting

Set b:AutoPairs before BufEnter


    " When the filetype is FILETYPE then make AutoPairs only match for parenthesis
    au Filetype FILETYPE let b:AutoPairs = {"(": ")"}

    The script will remap keys ([{'"}]) <BS>,
    If auto pairs cannot work, use :imap ( to check if the map is corrected.
    The correct map should be <C-R>=AutoPairsInsert("\(")<CR>
    Or the plugin conflict with some other plugins.
    use command :call AutoPairsInit() to remap the keys.

* How to insert parens purely

    There are 3 ways

    1. use Ctrl-V ) to insert paren without trigger the plugin.

    2. use Alt-P to turn off the plugin.

    3. use DEL or <C-O>x to delete the character insert by plugin.

Known Issues
There are the issues I cannot fix.

Breaks '.' - [issue #3](https://github.com/jiangmiao/auto-pairs/issues/3)

    Description: After entering insert mode and inputing `[hello` then leave insert
                 mode by `<ESC>`. press '.' will insert 'hello' instead of '[hello]'.
    Reason: `[` actually equals `[]\<LEFT>` and \<LEFT> will break '.'
    Solution: none

* [camthompson](https://github.com/camthompson)
install details
copy plugin/auto-pairs.vim to ~/.vim/plugin

or if you are using `pathogen`:

```git clone git://github.com/jiangmiao/auto-pairs.git ~/.vim/bundle/auto-pairs```

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
auto-pairs-v1.3.2.tar.gz 1.3.2 2013-07-17 7.0 Miao Jiang Try to use abbreviation before using space mapping
<c-h> now behaves the same as <BS>
auto-pairs-v1.3.1.tar.gz 1.3.1 2013-03-31 7.0 Miao Jiang Fix #33: character '|' map failed.
Fix #35: Fast Wrap incorrect.
Improve #36, Keep quotes number is odd in one line.
Improve #37, ignore comment line for vim file.
Improve #43, <CR> mapping compatible.
auto-pairs-v1.3.0.tar.gz 1.3.0 2013-02-16 7.0 Miao Jiang Auto insert triple pairs.
Support deleting repeated pairs in one time.
auto-pairs-v1.2.9.tar.gz 1.2.9 2013-01-15 7.0 Miao Jiang Supports buffer level pairs setting.
auto-pairs-v1.2.8.tar.gz 1.2.8 2012-12-18 7.0 Miao Jiang fixes incorrect AutoPairsDelete.
auto-pairs-v1.2.7.tar.gz 1.2.7 2012-11-02 7.0 Miao Jiang fixes #24 when equalprg is set 'Return' feature cannot work well
auto-pairs-v1.2.6.tar.gz 1.2.6 2012-10-17 7.0 Miao Jiang Support typographer's quote pairs, such as ,
auto-pairs-v1.2.5.tar.gz 1.2.5 2012-09-15 7.0 Miao Jiang Fixes indentation incorrect when returning below 1/3 bottom of window.
auto-pairs-v1.2.4.tar.gz 1.2.4 2012-08-16 7.0 Miao Jiang fixes #20 Errors in pair creation that is split by ENTER
auto-pairs-v1.2.3.tar.gz 1.2.3 2012-07-15 7.0 Miao Jiang compatible with neocomplcache
compatible with clang_complete
Improve Fast Wrap
auto-pairs-v1.2.2.tar.gz 1.2.2 2012-05-16 7.0 Miao Jiang compatible with vim-endwise, supertab.
Disable Fly mode by default.
auto-pairs-v1.2.0.tar.gz 1.2.0 2012-05-14 7.0 Miao Jiang New feature: Fly Mode
auto-pairs-v1.1.6.tar.gz 1.1.6 2012-03-22 7.0 Miao Jiang fixes issue #9 Lowercase 'a' acute accent via opt-e not working
fixes issue #8 having errors with the double quotes when cpoptions is $
avoid overriding imap silent flag
always silent mapping
auto-pairs-v1.1.5.tar.gz 1.1.5 2012-03-03 7.0 Miao Jiang works with vim-endwise
auto-pairs-v1.1.4.tar.gz 1.1.4 2012-01-17 7.0 Miao Jiang Avoid ' in word, eg: John's
Support `
Support triple quote, ''', """, ```
auto-pairs-v1.1.3.tar.gz 1.1.3 2011-12-22 7.0 Miao Jiang NEW: quick jump to the closed pair
NEW: auto insert space
auto-pairs-v1.1.2.tar.gz 1.1.2 2011-11-13 7.0 Miao Jiang Fix autopairs isn't loaded when open new tab by mouse.
Optimize delete in pair
auto-pairs-v1.1.1.tar.gz 1.1.1 2011-09-05 7.0 Miao Jiang Fix auto indent incorrect in CoffeeScript
    bar: [|]
press <CR> at |
auto-pairs-v1.1.0.tar.gz 1.1.0 2011-06-13 7.0 Miao Jiang fix indent incorrect after {\<CR>}
use <expr> instead of <C-R> in map keys
auto-pairs-v1.0.3.tar.gz 1.0.3 2011-06-09 7.0 Miao Jiang Add Fast Wrap feature.
auto-pairs-v1.0.2.tar.gz 1.0.2 2011-06-07 7.0 Miao Jiang Use {<enter> instead of {{ (Thanks to linopolus)

Add Toggle key
Add Options
auto-pairs-1.0.1.tar.gz 1.0.1 2011-05-24 7.0 Miao Jiang Initial upload
ip used for rating:

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.