我的vim 自动实例括号函数

不废话,直接上代码:

 1 """""""""""""""""""""""""""""""""""""""""""
 2 "自动实例括号
 3 """""""""""""""""""""""""""""""""""""""""""
 4 ""inoremap ( ()<ESC>i
 5 inoremap ( <c-r>=OpenPair('(',')')<CR>
 6 inoremap ) <c-r>=ClosePair(')')<CR>
 7 inoremap { {}<ESC>i
 8 inoremap } <c-r>=ClosePair('}')<CR>
 9 inoremap [ []<ESC>i
10 inoremap ] <c-r>=ClosePair(']')<CR>
11 inoremap " <c-r>=QuoteDelim('"')<CR>
12 inoremap ' <c-r>=QuoteDelim("'")<CR>
13 
14 function! OpenPair(char1,char2)
15     let line = getline('.')
16     let col = col('.')
17     if col('$') == col
18         return a:char1 . a:char2. "<Left>"
19     else
20         return a:char1 
21     endif
22 endfunction
23 
24 function! ClosePair(char)
25     if getline('.')[col('.') - 1] == a:char
26         return "<Right>"
27     else
28         return a:char
29     endif
30 endfunction
31 
32 function! QuoteDelim(char)
33     let line = getline('.')
34     let col = col('.')
35     if line[col - 2] == "\"
36         "Inserting a quoted quotation mark into the string
37         return a:char
38     elseif line[col - 1] == a:char
39         "Escaping out of the string
40         return "<Right>"
41     else
42         "Starting a string
43         return a:char.a:char."<Esc>i"
44     endif
45 endf
原文地址:https://www.cnblogs.com/sixbeauty/p/4285654.html