標籤:標籤 vim 自動補全 vimrc
問題:怎樣在vim中實現花括弧引號自動補全,包括html標籤?解決辦法:只要把下面兩段代碼粘貼到~/.vimrc中,就可以實現括弧超強補全<!--括弧引號補全代碼{{{-->" 括弧引號補全inoremap ( ()<Esc>iinoremap [ []<Esc>iinoremap { {<CR>}<Esc>Oinoremap ) <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:charreturn "\<Right>"elsereturn a:charendifendffunction CloseBracket()if match(getline(line('.') + 1), '\s*}') < 0return "\<CR>}"elsereturn "\<Esc>j0f}a"endifendffunction QuoteDelim(char)let line = getline('.')let col = col('.')if line[col - 2] == "\\""Inserting a quoted quotation mark into the stringreturn a:charelseif line[col - 1] == a:char"Escaping out of the stringreturn "\<Right>"else"Starting a stringreturn a:char.a:char."\<Esc>i"endifendf<!--}}}--><!--html標籤自動補全{{{-->" html自動補全autocmd BufNewFile * setlocal filetype=htmlfunction! InsertHtmlTag()let pat = '\c<\w\+\s*\(\s\+\w\+\s*=\s*[''#$;,()."a-z0-9]\+\)*\s*>'normal! a>let save_cursor = getpos('.')let result = matchstr(getline(save_cursor[1]), pat)"if (search(pat, 'b', save_cursor[1]) && searchpair('<','','>','bn',0, getline('.')) > 0)if (search(pat, 'b', save_cursor[1]))normal! lyiwf>normal! a</normal! pnormal! a>endif:call cursor(save_cursor[1], save_cursor[2], save_cursor[3])endfunctioninoremap > <ESC>:call InsertHtmlTag()<CR>a<CR><Esc>O<!--}}}-->之所以這裡的括弧補全代碼中的函數實現反匹配當打入(輸入內容),再按)系統會自動檢查前面是否已經有匹配的括弧如果有就不再鍵入),而是直接跳出或許你得加:set autoindentset cindent參考:http://www.cnblogs.com/huanlei/archive/2012/04/02/2430153.htmlhttp://blog.sina.com.cn/s/blog_01ea59580101hvth.html
0008 vim括弧引號html標籤自動補全