├── .gitignore ├── README.md ├── doc └── AutoPairs.txt └── plugin └── auto-pairs.vim /.gitignore: -------------------------------------------------------------------------------- 1 | doc/tags 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Auto Pairs 2 | ========== 3 | Insert or delete brackets, parens, quotes in pair. 4 | 5 | Installation 6 | ------------ 7 | 8 | * Manual 9 | * Copy `plugin/auto-pairs.vim` to `~/.vim/plugin` 10 | * [Pathogen](https://github.com/tpope/vim-pathogen) 11 | * `git clone git://github.com/jiangmiao/auto-pairs.git ~/.vim/bundle/auto-pairs` 12 | * [Vundle](https://github.com/VundleVim/Vundle.vim) 13 | * `Plugin 'jiangmiao/auto-pairs'` 14 | 15 | Features 16 | -------- 17 | * Insert in pair 18 | 19 | input: [ 20 | output: [|] 21 | 22 | * Delete in pair 23 | 24 | input: foo[] 25 | output: foo 26 | 27 | * Insert new indented line after Return 28 | 29 | input: {|} (press at |) 30 | output: { 31 | | 32 | } (press } to close the pair) 33 | output: { 34 | }| (the inserted blank line will be deleted) 35 | 36 | 37 | * Insert spaces before closing characters, only for [], (), {} 38 | 39 | input: {|} (press at |) 40 | output: { | } 41 | 42 | input: {|} (press foo} at |) 43 | output: { foo }| 44 | 45 | input: '|' (press at |) 46 | output: ' |' 47 | 48 | * Skip ' when inside a word 49 | 50 | input: foo| (press ' at |) 51 | output: foo' 52 | 53 | * Skip closed bracket. 54 | 55 | input: [] 56 | output: [] 57 | 58 | * Ignore auto pair when previous character is \ 59 | 60 | input: "\' 61 | output: "\'" 62 | 63 | * Fast Wrap 64 | 65 | input: |[foo, bar()] (press ( at |) 66 | output: ([foo, bar()]) 67 | 68 | * Quick move char to closed pair 69 | 70 | input: (|){["foo"]} (press at |) 71 | output: ({["foo"]}|) 72 | 73 | input: |[foo, bar()] (press ( at |) 74 | output: ([foo, bar()]|) 75 | 76 | * Quick jump to closed pair. 77 | 78 | input: 79 | { 80 | something;| 81 | } 82 | 83 | (press } at |) 84 | 85 | output: 86 | { 87 | 88 | }| 89 | 90 | * Fly Mode 91 | 92 | input: if(a[3) 93 | output: if(a[3])| (In Fly Mode) 94 | output: if(a[3)]) (Without Fly Mode) 95 | 96 | input: 97 | { 98 | hello();| 99 | world(); 100 | } 101 | 102 | (press } at |) 103 | 104 | output: 105 | { 106 | hello(); 107 | world(); 108 | }| 109 | 110 | (then press at | to do backinsert) 111 | output: 112 | { 113 | hello();}| 114 | world(); 115 | } 116 | 117 | See Fly Mode section for details 118 | 119 | * Multibyte Pairs 120 | 121 | Support any multibyte pairs such as , <% %>, """ """ 122 | See multibyte pairs section for details 123 | 124 | Fly Mode 125 | -------- 126 | Fly Mode will always force closed-pair jumping instead of inserting. only for ")", "}", "]" 127 | 128 | If jumps in mistake, could use AutoPairsBackInsert(Default Key: ``) to jump back and insert closed pair. 129 | 130 | the most situation maybe want to insert single closed pair in the string, eg ")" 131 | 132 | Fly Mode is DISABLED by default. 133 | 134 | add **let g:AutoPairsFlyMode = 1** .vimrc to turn it on 135 | 136 | Default Options: 137 | 138 | let g:AutoPairsFlyMode = 0 139 | let g:AutoPairsShortcutBackInsert = '' 140 | 141 | Shortcuts 142 | --------- 143 | 144 | System Shortcuts: 145 | : Insert new indented line after return if cursor in blank brackets or quotes. 146 | : Delete brackets in pair 147 | : Toggle Autopairs (g:AutoPairsShortcutToggle) 148 | : Fast Wrap (g:AutoPairsShortcutFastWrap) 149 | : Jump to next closed pair (g:AutoPairsShortcutJump) 150 | : BackInsert (g:AutoPairsShortcutBackInsert) 151 | 152 | If or conflict with another keys or want to bind to another keys, add 153 | 154 | let g:AutoPairsShortcutToggle = '' 155 | 156 | to .vimrc, if the key is empty string '', then the shortcut will be disabled. 157 | 158 | Options 159 | ------- 160 | * g:AutoPairs 161 | 162 | Default: {'(':')', '[':']', '{':'}',"'":"'",'"':'"', "`":"`", '```':'```', '"""':'"""', "'''":"'''"} 163 | 164 | * b:AutoPairs 165 | 166 | Default: g:AutoPairs 167 | 168 | Buffer level pairs set. 169 | 170 | * g:AutoPairsShortcutToggle 171 | 172 | Default: '' 173 | 174 | The shortcut to toggle autopairs. 175 | 176 | * g:AutoPairsShortcutFastWrap 177 | 178 | Default: '' 179 | 180 | Fast wrap the word. all pairs will be consider as a block (include <>). 181 | (|)'hello' after fast wrap at |, the word will be ('hello') 182 | (|) after fast wrap at |, the word will be () 183 | 184 | * g:AutoPairsShortcutJump 185 | 186 | Default: '' 187 | 188 | Jump to the next closed pair 189 | 190 | * g:AutoPairsMapBS 191 | 192 | Default : 1 193 | 194 | Map to delete brackets, quotes in pair 195 | execute 'inoremap =AutoPairsDelete()' 196 | 197 | * g:AutoPairsMapCh 198 | 199 | Default : 1 200 | 201 | Map to delete brackets, quotes in pair 202 | 203 | * g:AutoPairsMapCR 204 | 205 | Default : 1 206 | 207 | Map to insert a new indented line if cursor in (|), {|} [|], '|', "|" 208 | execute 'inoremap =AutoPairsReturn()' 209 | 210 | * g:AutoPairsCenterLine 211 | 212 | Default : 1 213 | 214 | When g:AutoPairsMapCR is on, center current line after return if the line is at the bottom 1/3 of the window. 215 | 216 | * g:AutoPairsMapSpace 217 | 218 | Default : 1 219 | 220 | Map to insert a space after the opening character and before the closing one. 221 | execute 'inoremap =AutoPairsSpace()' 222 | 223 | * g:AutoPairsFlyMode 224 | 225 | Default : 0 226 | 227 | set it to 1 to enable FlyMode. 228 | see FlyMode section for details. 229 | 230 | * g:AutoPairsMultilineClose 231 | 232 | Default : 1 233 | 234 | When you press the key for the closing pair (e.g. `)`) it jumps past it. 235 | If set to 1, then it'll jump to the next line, if there is only whitespace. 236 | If set to 0, then it'll only jump to a closing pair on the same line. 237 | 238 | * g:AutoPairsShortcutBackInsert 239 | 240 | Default : 241 | 242 | Work with FlyMode, insert the key at the Fly Mode jumped postion 243 | 244 | * g:AutoPairsMoveCharacter 245 | 246 | Default: "()[]{}\"'" 247 | 248 | Map to 249 | move character under the cursor to the pair. 250 | 251 | Buffer Level Pairs Setting 252 | -------------------------- 253 | 254 | Set b:AutoPairs before BufEnter 255 | 256 | eg: 257 | 258 | " When the filetype is FILETYPE then make AutoPairs only match for parenthesis 259 | au Filetype FILETYPE let b:AutoPairs = {"(": ")"} 260 | au FileType php let b:AutoPairs = AutoPairsDefine({'', ''}) 261 | 262 | Multibyte Pairs 263 | --------------- 264 | 265 | The default pairs is {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'} 266 | You could also define multibyte pairs such as , <% %> and so on 267 | 268 | * Function AutoPairsDefine(addPairs:dict[, removeOpenPairList:list]) 269 | 270 | add or delete pairs base on g:AutoPairs 271 | 272 | eg: 273 | au FileType html let b:AutoPairs = AutoPairsDefine({''}, ['{']) 274 | add pair and remove '{' for html file 275 | 276 | the pair implict start with \V, so if want to match start of line ^ should be write in \^ vim comment {'\^"': ''} 277 | 278 | * General usage 279 | 280 | au FileType php let b:AutoPairs = AutoPairsDefine({'', ''}) 281 | 282 | the first key of closed pair ? will be mapped 283 | 284 | pairs: '', '' 285 | input: 287 | 288 | input: 290 | 291 | input: he (press at|) 292 | output: he| 293 | 294 | input: (press ? at|) 295 | output: | 296 | 297 | pair: '[[':']]' 298 | input: [[|]] (press ) 299 | output: | ([[ and ]] will be deleted the [['s priority is higher than [ for it's longer) 300 | 301 | * Modifier 302 | 303 | The text after // in close pair is modifiers 304 | 305 | n - do not map the first charactor of closed pair to close key 306 | m - close key jumps through multi line 307 | s - close key jumps only in the same line 308 | k[KEY] - map the close key to [KEY] 309 | 310 | by default if open key equals close key the multi line is turn off 311 | 312 | "" ? jumps only in the same line 313 | "//m" force ? jumping through multi line 314 | "" ? will jump through multi line 315 | "//s" force ? only jumping in the same line 316 | "//n" do not jump totally 317 | "//k]" use key ] to jump through ?> 318 | 319 | for 'begin' 'end' pair, e is a charactor, if map e to jump will be annoy, so use modifier 'n' to skip key map 320 | 321 | au FileType ruby let b:AutoPairs = AutoPairsDefine({'begin': 'end//n]'}) 322 | 323 | 324 | input: begin 325 | output: begin|end 326 | 327 | input: begin|end (press on |) 328 | output: | 329 | 330 | input: begin|end (press e on |) 331 | output: begineend (will not jump for e is not mapped) 332 | 333 | * Advanced usage 334 | 335 | au FileType rust let b:AutoPairs = AutoPairsDefine({'\w\zs<': '>'}) 336 | 337 | if press < after a word will generate the pair 338 | 339 | when use regexp MUST use \zs to prevent catching 340 | if use '\w<' without \zs, for text hello<|> press on | will output 'hell', the 'o' has been deleted 341 | 342 | pair: '\w\zs<': '>' 343 | input: h < 344 | output: h < 345 | 346 | input: h< 347 | output: h<|> 348 | 349 | input: h<|> press 350 | output: h| 351 | 352 | pair: '\w<': '>' (WRONG pair which missed \zs) 353 | input: h<|> press 354 | output: | (charactor 'h' is deleted) 355 | 356 | 357 | the 'begin' 'end' pair write in 358 | 359 | au FileType ruby let b:AutoPairs = AutoPairsDefine({'\v(^|\W)\zsbegin': 'end//n'}) 360 | 361 | will be better, only auto pair when at start of line or follow non-word text 362 | 363 | TroubleShooting 364 | --------------- 365 | The script will remap keys ([{'"}]) , 366 | If auto pairs cannot work, use :imap ( to check if the map is corrected. 367 | The correct map should be =AutoPairsInsert("\(") 368 | Or the plugin conflict with some other plugins. 369 | use command :call AutoPairsInit() to remap the keys. 370 | 371 | 372 | * How to insert parens purely 373 | 374 | There are 3 ways 375 | 376 | 1. use Ctrl-V ) to insert paren without trigger the plugin. 377 | 378 | 2. use Alt-P to turn off the plugin. 379 | 380 | 3. use DEL or x to delete the character insert by plugin. 381 | 382 | * Swedish Character Conflict 383 | 384 | Because AutoPairs uses Meta(Alt) key as shortcut, it is conflict with some Swedish character such as å. 385 | To fix the issue, you need remap or disable the related shortcut. 386 | 387 | Known Issues 388 | ------------ 389 | Breaks '.' - [issue #3](https://github.com/jiangmiao/auto-pairs/issues/3) 390 | 391 | Description: After entering insert mode and inputing `[hello` then leave insert 392 | mode by ``. press '.' will insert 'hello' instead of '[hello]'. 393 | Reason: `[` actually equals `[]\` and \ will break '.'. 394 | After version 7.4.849, Vim implements new keyword U to avoid the break 395 | Solution: Update Vim to 7.4.849+ 396 | 397 | Contributors 398 | ------------ 399 | * [camthompson](https://github.com/camthompson) 400 | 401 | 402 | License 403 | ------- 404 | 405 | Copyright (C) 2011-2013 Miao Jiang 406 | 407 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 408 | 409 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 410 | 411 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 412 | -------------------------------------------------------------------------------- /doc/AutoPairs.txt: -------------------------------------------------------------------------------- 1 | *AutoPairs.txt* Insert or delete brackets, parens, quotes in pair 2 | 3 | Author: jiangmiao 4 | License: MIT 5 | URL: https://github.com/jiangmiao/auto-pairs 6 | 7 | ============================================================================== 8 | CONTENTS *autopairs-contents* 9 | 10 | 1. Installation ............................. |autopairs-installation| 11 | 2. Features ..................................... |autopairs-features| 12 | 3. Fly Mode ..................................... |autopairs-fly-mode| 13 | 4. Shortcuts ................................... |autopairs-shortcuts| 14 | 5. Options ....................................... |autopairs-options| 15 | 6. Troubleshooting ...................... |autopairs-troubleshooting| 16 | 17 | ============================================================================== 18 | 1. Introduction *autopairs-installation* 19 | 20 | Copy `plugin/auto-pairs.vim` to `~/.vim/plugin`. 21 | 22 | Or if you are using `pathogen`: > 23 | 24 | git clone git://github.com/jiangmiao/auto-pairs.git ~/.vim/bundle/auto-pairs 25 | 26 | ============================================================================== 27 | 2. Features *autopairs-features* 28 | 29 | Insert in pair: > 30 | 31 | input: [ 32 | output: [|] 33 | 34 | Delete in pair: > 35 | 36 | input: foo[] 37 | output: foo 38 | 39 | Insert new indented line after Return: > 40 | 41 | input: {|} (press at |) 42 | output: { 43 | | 44 | } 45 | 46 | Insert spaces before closing characters, only for [], (), {}: > 47 | 48 | input: {|} (press at |) 49 | output: { | } 50 | 51 | input: {|} (press foo} at |) 52 | output: { foo }| 53 | 54 | input: '|' (press at |) 55 | output: ' |' 56 | 57 | Skip ' when inside a word: > 58 | 59 | input: foo| (press ' at |) 60 | output: foo' 61 | 62 | Skip closed bracket: > 63 | 64 | input: [] 65 | output: [] 66 | 67 | Ignore auto pair when previous character is '\': > 68 | 69 | input: "\' 70 | output: "\'" 71 | 72 | Fast Wrap: > 73 | 74 | input: |'hello' (press ( at |) 75 | output: ('hello') 76 | 77 | Wrap string, only support c style string. 78 | input: |'h\\el\'lo' (press ( at |) 79 | output ('h\\ello\'') 80 | 81 | input: |[foo, bar()] (press ( at |) 82 | output: ([foo, bar()]) 83 | 84 | Quick jump to closed pair: > 85 | 86 | input: 87 | { 88 | something;| 89 | } 90 | 91 | (press } at |) 92 | 93 | output: 94 | { 95 | 96 | }| 97 | 98 | Support ```, ''' and """: > 99 | 100 | input: 101 | ''' 102 | 103 | output: 104 | '''|''' 105 | 106 | Delete Repeated Pairs in one time: > 107 | 108 | input: """|""" (press at |) 109 | output: | 110 | 111 | input: {{|}} (press at |) 112 | output: | 113 | 114 | input: [[[[[[|]]]]]] (press at |) 115 | output: | 116 | 117 | Fly Mode (|autopairs-flymode|): > 118 | 119 | input: if(a[3) 120 | output: if(a[3])| (In Fly Mode) 121 | output: if(a[3)]) (Without Fly Mode) 122 | 123 | input: 124 | { 125 | hello();| 126 | world(); 127 | } 128 | 129 | (press } at |) 130 | 131 | output: 132 | { 133 | hello(); 134 | world(); 135 | }| 136 | 137 | (then press at | to do backinsert) 138 | output: 139 | { 140 | hello();}| 141 | world(); 142 | } 143 | 144 | See |Fly Mode| section for details 145 | 146 | ============================================================================== 147 | 3. Fly Mode *autopairs-flymode* 148 | 149 | Fly Mode will always force closed-pair jumping instead of inserting. Only for 150 | ")", "}", "]". If jumps in mistake, you can use |g:AutoPairsBackInsert| (default 151 | Key: ) to jump back and insert closed pair. 152 | 153 | The most situation maybe you want to insert single closed pair in the string, 154 | eg: > 155 | 156 | ")" 157 | 158 | Fly Mode is DISABLED by default. To enable Fly Mode add following to your 159 | '.vimrc': > 160 | 161 | let g:AutoPairsFlyMode = 1 162 | 163 | Default Options: > 164 | 165 | let g:AutoPairsFlyMode = 0 166 | let g:AutoPairsShortcutBackInsert = '' 167 | 168 | ============================================================================== 169 | 4. Shortcuts *autopairs-shortcuts* 170 | 171 | System Shortcuts: 172 | : Insert new indented line after return if cursor in blank brackets 173 | or quotes. 174 | : Delete brackets in pair 175 | : Toggle Autopairs (|g:AutoPairsShortcutToggle|) 176 | : Fast Wrap (|g:AutoPairsShortcutFastWrap|) 177 | : Jump to next closed pair (|g:AutoPairsShortcutJump|) 178 | : BackInsert (|g:AutoPairsShortcutBackInsert|) 179 | 180 | 181 | To rebind keys , or or in case of conflicts with 182 | another keys: 183 | 184 | let g:AutoPairsShortcutToggle = '' 185 | 186 | If the key is empty string '', then the shortcut will be disabled. 187 | 188 | ============================================================================== 189 | 5. Options *autopairs-options* 190 | 191 | *g:AutoPairs* 192 | |g:AutoPairs| dict 193 | 194 | Default: > 195 | {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'} 196 | 197 | Specifies which symbols should be automatically paired. 198 | 199 | To append new pairs without overwriting defaults, add values in your `.vimrc`.: 200 | 201 | let g:AutoPairs['<']='>' 202 | 203 | This example will enable matching of `<` with `>`. 204 | 205 | 206 | *b:AutoPairs* 207 | |b:AutoPairs| dict 208 | 209 | Default: |g:AutoPairs| 210 | 211 | Buffer level pairs set. 212 | 213 | You can set |b:AutoPairs| before |BufEnter|: > 214 | 215 | au Filetype FILETYPE let b:AutoPairs = {"(": ")"} 216 | 217 | This sets |AutoPairs| to only match for parenthesis for 'FILETYPE'. 218 | 219 | 220 | 221 | *g:AutoPairsShortcutToggle* 222 | |g:AutoPairsShortcutToggle| string 223 | 224 | Default: 225 | 226 | The shortcut to toggle autopairs. 227 | 228 | 229 | 230 | *g:AutoPairsShortcutFastWrap* 231 | |g:AutoPairsShortcutFastWrap| string 232 | 233 | Default: 234 | 235 | Fast wrap the word. All pairs will be considered as a block (including <>). 236 | 237 | (|)'hello' after fast wrap at |, the word will be ('hello') 238 | (|) after fast wrap at |, the word will be () 239 | 240 | 241 | 242 | *g:AutoPairsShortcutJump* 243 | |g:AutoPairsShortcutJump| string 244 | 245 | Default: 246 | 247 | Jump to the next closed pair. 248 | 249 | 250 | *g:AutoPairsShortcutBackInsert* 251 | |g:AutoPairsShortcutBackInsert| string 252 | 253 | Default: 254 | 255 | Work with |autopairs-flymode|, insert the key at the Fly Mode jumped position. 256 | 257 | 258 | 259 | *g:AutoPairsMapBS* 260 | |g:AutoPairsMapBS| int 261 | 262 | Default: 1 263 | 264 | Map to delete brackets and quotes in pair, executes: 265 | 266 | inoremap =AutoPairsDelete() 267 | 268 | 269 | *g:AutoPairsMapCh* 270 | |g:AutoPairsMapCh| int 271 | 272 | Default: 1 273 | 274 | Map to delete brackets and quotes in pair. 275 | 276 | 277 | *g:AutoPairsMapCR* 278 | |g:AutoPairsMapCR| int 279 | 280 | Default: 1 281 | 282 | Map to insert a new indented line if cursor in (|), {|} [|], '|', "|". 283 | Executes: 284 | 285 | inoremap =AutoPairsReturn() 286 | 287 | 288 | *g:AutoPairsCenterLine* 289 | |g:AutoPairsCenterLine| int 290 | 291 | Default: 1 292 | 293 | When |g:AutoPairsMapCR| is on, center current line after return if the line 294 | is at the bottom 1/3 of the window. 295 | 296 | 297 | *g:AutoPairsMapSpace* 298 | |g:AutoPairsMapSpace| int 299 | 300 | Default: 1 301 | 302 | Map to insert a space after the opening character and before the 303 | closing one. 304 | 305 | Executes: 306 | 307 | inoremap =AutoPairsSpace() 308 | 309 | 310 | *g:AutoPairsFlyMode* 311 | |g:AutoPairsFlyMode| int 312 | 313 | Default: 0 314 | 315 | Set it to 1 to enable |autopairs-flymode|. 316 | 317 | 318 | *g:AutoPairsMultilineClose* 319 | |g:AutoPairsMultilineClose| int 320 | 321 | Default: 1 322 | 323 | When you press the key for the closing pair (e.g. `)`) it jumps past it. 324 | If set to 1, then it'll jump to the next line, if there is only 'whitespace'. 325 | If set to 0, then it'll only jump to a closing pair on the same line. 326 | 327 | ============================================================================== 328 | 6. Troubleshooting *autopairs-troubleshooting* 329 | 330 | This plugin remaps keys `([{'"}]) ` 331 | 332 | If auto pairs cannot work, use |:imap| to check if the map is corrected. 333 | 334 | The correct map should be: > 335 | 336 | =AutoPairsInsert("\(") 337 | 338 | Or the plugin conflicts with some other plugins. Use command: > 339 | 340 | :call AutoPairsInit() to remap the keys. 341 | 342 | --- How to insert parens purely? --- 343 | 344 | There are 3 ways: 345 | 346 | 1. Use Ctrl-V ) to insert paren without trigger the plugin. 347 | 348 | 2. Use Alt-P to turn off the plugin. 349 | 350 | 3. Use DEL or x to delete the character insert by plugin. 351 | 352 | --- Swedish Character Conflict --- 353 | 354 | Because AutoPairs uses Meta(Alt) key as a shortcut, it conflicts with some 355 | Swedish character such as å. To fix the issue, you need remap or disable the 356 | related shortcut. 357 | -------------------------------------------------------------------------------- /plugin/auto-pairs.vim: -------------------------------------------------------------------------------- 1 | " Insert or delete brackets, parens, quotes in pairs. 2 | " Maintainer: JiangMiao 3 | " Contributor: camthompson 4 | " Last Change: 2019-02-02 5 | " Version: 2.0.0 6 | " Homepage: http://www.vim.org/scripts/script.php?script_id=3599 7 | " Repository: https://github.com/jiangmiao/auto-pairs 8 | " License: MIT 9 | 10 | if exists('g:AutoPairsLoaded') || &cp 11 | finish 12 | end 13 | let g:AutoPairsLoaded = 1 14 | 15 | if !exists('g:AutoPairs') 16 | let g:AutoPairs = {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '```':'```', '"""':'"""', "'''":"'''", "`":"`"} 17 | end 18 | 19 | " default pairs base on filetype 20 | func! AutoPairsDefaultPairs() 21 | if exists('b:autopairs_defaultpairs') 22 | return b:autopairs_defaultpairs 23 | end 24 | let r = copy(g:AutoPairs) 25 | let allPairs = { 26 | \ 'vim': {'\v^\s*\zs"': ''}, 27 | \ 'rust': {'\w\zs<': '>', '&\zs''': ''}, 28 | \ 'php': {'//k]', '//k]'} 29 | \ } 30 | for [filetype, pairs] in items(allPairs) 31 | if &filetype == filetype 32 | for [open, close] in items(pairs) 33 | let r[open] = close 34 | endfor 35 | end 36 | endfor 37 | let b:autopairs_defaultpairs = r 38 | return r 39 | endf 40 | 41 | if !exists('g:AutoPairsMapBS') 42 | let g:AutoPairsMapBS = 1 43 | end 44 | 45 | " Map as the same BS 46 | if !exists('g:AutoPairsMapCh') 47 | let g:AutoPairsMapCh = 1 48 | end 49 | 50 | if !exists('g:AutoPairsMapCR') 51 | let g:AutoPairsMapCR = 1 52 | end 53 | 54 | if !exists('g:AutoPairsWildClosedPair') 55 | let g:AutoPairsWildClosedPair = '' 56 | end 57 | 58 | if !exists('g:AutoPairsMapSpace') 59 | let g:AutoPairsMapSpace = 1 60 | end 61 | 62 | if !exists('g:AutoPairsCenterLine') 63 | let g:AutoPairsCenterLine = 1 64 | end 65 | 66 | if !exists('g:AutoPairsShortcutToggle') 67 | let g:AutoPairsShortcutToggle = '' 68 | end 69 | 70 | if !exists('g:AutoPairsShortcutFastWrap') 71 | let g:AutoPairsShortcutFastWrap = '' 72 | end 73 | 74 | if !exists('g:AutoPairsMoveCharacter') 75 | let g:AutoPairsMoveCharacter = "()[]{}\"'" 76 | end 77 | 78 | if !exists('g:AutoPairsShortcutJump') 79 | let g:AutoPairsShortcutJump = '' 80 | endif 81 | 82 | " Fly mode will for closed pair to jump to closed pair instead of insert. 83 | " also support AutoPairsBackInsert to insert pairs where jumped. 84 | if !exists('g:AutoPairsFlyMode') 85 | let g:AutoPairsFlyMode = 0 86 | endif 87 | 88 | " When skipping the closed pair, look at the current and 89 | " next line as well. 90 | if !exists('g:AutoPairsMultilineClose') 91 | let g:AutoPairsMultilineClose = 1 92 | endif 93 | 94 | " Work with Fly Mode, insert pair where jumped 95 | if !exists('g:AutoPairsShortcutBackInsert') 96 | let g:AutoPairsShortcutBackInsert = '' 97 | endif 98 | 99 | if !exists('g:AutoPairsSmartQuotes') 100 | let g:AutoPairsSmartQuotes = 1 101 | endif 102 | 103 | " 7.4.849 support U to avoid breaking '.' 104 | " Issue talk: https://github.com/jiangmiao/auto-pairs/issues/3 105 | " Vim note: https://github.com/vim/vim/releases/tag/v7.4.849 106 | if v:version > 704 || v:version == 704 && has("patch849") 107 | let s:Go = "\U" 108 | else 109 | let s:Go = "" 110 | endif 111 | 112 | let s:Left = s:Go."\" 113 | let s:Right = s:Go."\" 114 | 115 | 116 | 117 | 118 | " unicode len 119 | func! s:ulen(s) 120 | return len(split(a:s, '\zs')) 121 | endf 122 | 123 | func! s:left(s) 124 | return repeat(s:Left, s:ulen(a:s)) 125 | endf 126 | 127 | func! s:right(s) 128 | return repeat(s:Right, s:ulen(a:s)) 129 | endf 130 | 131 | func! s:delete(s) 132 | return repeat("\", s:ulen(a:s)) 133 | endf 134 | 135 | func! s:backspace(s) 136 | return repeat("\", s:ulen(a:s)) 137 | endf 138 | 139 | func! s:getline() 140 | let line = getline('.') 141 | let pos = col('.') - 1 142 | let before = strpart(line, 0, pos) 143 | let after = strpart(line, pos) 144 | let afterline = after 145 | if g:AutoPairsMultilineClose 146 | let n = line('$') 147 | let i = line('.')+1 148 | while i <= n 149 | let line = getline(i) 150 | let after = after.' '.line 151 | if !(line =~ '\v^\s*$') 152 | break 153 | end 154 | let i = i+1 155 | endwhile 156 | end 157 | return [before, after, afterline] 158 | endf 159 | 160 | " split text to two part 161 | " returns [orig, text_before_open, open] 162 | func! s:matchend(text, open) 163 | let m = matchstr(a:text, '\V'.a:open.'\v$') 164 | if m == "" 165 | return [] 166 | end 167 | return [a:text, strpart(a:text, 0, len(a:text)-len(m)), m] 168 | endf 169 | 170 | " returns [orig, close, text_after_close] 171 | func! s:matchbegin(text, close) 172 | let m = matchstr(a:text, '^\V'.a:close) 173 | if m == "" 174 | return [] 175 | end 176 | return [a:text, m, strpart(a:text, len(m), len(a:text)-len(m))] 177 | endf 178 | 179 | " add or delete pairs base on g:AutoPairs 180 | " AutoPairsDefine(addPairs:dict[, removeOpenPairList:list]) 181 | " 182 | " eg: 183 | " au FileType html let b:AutoPairs = AutoPairsDefine({''}, ['{']) 184 | " add pair and remove '{' for html file 185 | func! AutoPairsDefine(pairs, ...) 186 | let r = AutoPairsDefaultPairs() 187 | if a:0 > 0 188 | for open in a:1 189 | unlet r[open] 190 | endfor 191 | end 192 | for [open, close] in items(a:pairs) 193 | let r[open] = close 194 | endfor 195 | return r 196 | endf 197 | 198 | func! AutoPairsInsert(key) 199 | if !b:autopairs_enabled 200 | return a:key 201 | end 202 | 203 | let b:autopairs_saved_pair = [a:key, getpos('.')] 204 | 205 | let [before, after, afterline] = s:getline() 206 | 207 | " Ignore auto close if prev character is \ 208 | if before[-1:-1] == '\' 209 | return a:key 210 | end 211 | 212 | " check open pairs 213 | for [open, close, opt] in b:AutoPairsList 214 | let ms = s:matchend(before.a:key, open) 215 | let m = matchstr(afterline, '^\v\s*\zs\V'.close) 216 | if len(ms) > 0 217 | " process the open pair 218 | 219 | " remove inserted pair 220 | " eg: if the pairs include < > and 221 | " when