zoukankan      html  css  js  c++  java
  • vim中实现括号和引号自动补全

    将下面内容加入.vimrc文件中即可
    inoremap ( ()<Esc>i
    inoremap [ []<Esc>i
    inoremap { {<CR>}<Esc>O
    autocmd 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
     endif
    endf
    
    function CloseBracket()
     if match(getline(line('.') + 1), '\s*}') < 0
     return "\<CR>}"
     else
     return "\<Esc>j0f}a"
     endif
    endf
    
    function 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"
     endif
    endf

      

    其他相关:

    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
        endif
    endfunction
    function! ClosePair(char)
        if getline('.')[col('.')-1] == a:char
            return "\<Right>"
        else
            return a:char
        endif
    endf
    
    inoremap ' <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>"
        endif
    endfunction
    

      

  • 相关阅读:
    【转载】不出国如何练就一口流利的英语?
    【转载】什么能力很重要——4
    【转载】什么能力很重要,但大多数人却没有?——3
    【转载】什么能力很重要,但大多数人却没有?——2
    【转载】什么能力很重要,但大多数人却没有?——1
    【转载】有哪些东西是你读研究生以后才懂的?
    JDBCTemplate基本使用
    Druid数据库连接池基本使用
    C3P0连接池
    简单理解数据库连接池(JDBC)
  • 原文地址:https://www.cnblogs.com/huanlei/p/2430153.html
Copyright © 2011-2022 走看看