vim中實現括弧和引號自動補全

來源:互聯網
上載者:User
將下面內容加入.vimrc檔案中即可
inoremap ( ()<Esc>iinoremap [ []<Esc>iinoremap { {<CR>}<Esc>Oautocmd Syntax html,vim inoremap < <lt>><Esc>i| inoremap > <c-r>=ClosePair('>')<CR>inoremap ) <c-r>=ClosePair(')')<CR>inoremap ] <c-r>=ClosePair(']')<CR>inoremap } <c-r>=CloseBracket()<CR>inoremap " <c-r>=QuoteDelim('"')<CR>inoremap ' <c-r>=QuoteDelim("'")<CR>function ClosePair(char) if getline('.')[col('.') - 1] == a:char return "\<Right>" else return a:char endifendffunction CloseBracket() if match(getline(line('.') + 1), '\s*}') < 0 return "\<CR>}" else return "\<Esc>j0f}a" endifendffunction QuoteDelim(char) let line = getline('.') let col = col('.') if line[col - 2] == "\\" "Inserting a quoted quotation mark into the string return a:char elseif line[col - 1] == a:char "Escaping out of the string return "\<Right>" else "Starting a string return a:char.a:char."\<Esc>i" endifendf

  

 

 

其他相關:

autocomplete.match.pair.vim

inoremap ( <c-r>=OpenPair('(')<CR>inoremap ) <c-r>=ClosePair(')')<CR>inoremap { <c-r>=OpenPair('{')<CR>inoremap } <c-r>=ClosePair('}')<CR>inoremap [ <c-r>=OpenPair('[')<CR>inoremap ] <c-r>=ClosePair(']')<CR>" just for xml document, but need not for now."inoremap < <c-r>=OpenPair('<')<CR>"inoremap > <c-r>=ClosePair('>')<CR>function! OpenPair(char)    let PAIRs = {                \ '{' : '}',                \ '[' : ']',                \ '(' : ')',                \ '<' : '>'                \}    if line('$')>2000        let line = getline('.')        let txt = strpart(line, col('.')-1)    else        let lines = getline(1,line('$'))        let line=""        for str in lines            let line = line . str . "\n"        endfor        let blines = getline(line('.')-1, line("$"))        let txt = strpart(getline("."), col('.')-1)        for str in blines            let txt = txt . str . "\n"        endfor    endif    let oL = len(split(line, a:char, 1))-1    let cL = len(split(line, PAIRs[a:char], 1))-1    let ol = len(split(txt, a:char, 1))-1    let cl = len(split(txt, PAIRs[a:char], 1))-1    if oL>=cL || (oL<cL && ol>=cl)        return a:char . PAIRs[a:char] . "\<Left>"    else        return a:char    endifendfunctionfunction! ClosePair(char)    if getline('.')[col('.')-1] == a:char        return "\<Right>"    else        return a:char    endifendfinoremap ' <c-r>=CompleteQuote("'")<CR>inoremap " <c-r>=CompleteQuote('"')<CR>function! CompleteQuote(quote)    let ql = len(split(getline('.'), a:quote, 1))-1    let slen = len(split(strpart(getline("."), 0, col(".")-1), a:quote, 1))-1    let elen = len(split(strpart(getline("."), col(".")-1), a:quote, 1))-1    let isBefreQuote = getline('.')[col('.') - 1] == a:quote    if '"'==a:quote && "vim"==&ft && 0==match(strpart(getline('.'), 0, col('.')-1), "^[\t ]*$")        " for vim comment.        return a:quote    elseif "'"==a:quote && 0==match(getline('.')[col('.')-2], "[a-zA-Z0-9]")        " for Name's Blog.        return a:quote    elseif (ql%2)==1        " a:quote length is odd.        return a:quote    elseif ((slen%2)==1 && (elen%2)==1 && !isBefreQuote) || ((slen%2)==0 && (elen%2)==0)        return a:quote . a:quote . "\<Left>"    elseif isBefreQuote        return "\<Right>"    else        return a:quote . a:quote . "\<Left>"    endifendfunction

  

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.