(我现在已经不用下面方法了,可使用 xptemplate 插件 http://blog.youkuaiyun.com/zcube/article/details/42525973)
问题:怎样在vim中实现花括号引号自动补全,包括html标签?
解决办法:只要把下面两段代码粘贴到~/.vimrc中,就可以实现括号超强补全
<!--括号引号补全代码{{{-->
" 括号引号补全
inoremap ( ()<Esc>i
inoremap [ []<Esc>i
inoremap { {<CR>}<Esc>O
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
<!--}}}-->
<!--html标签自动补全{{{-->
" html自动补全
autocmd BufNewFile * setlocal filetype=html
function! 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! p
normal! a>
endif
:call cursor(save_cursor[1], save_cursor[2], save_cursor[3])
endfunction
inoremap > <ESC>:call InsertHtmlTag()<CR>a<CR><Esc>O
<!--}}}-->
之所以这里的括号补全代码中的函数实现反匹配
当打入(输入内容),再按)系统会自动检查前面是否已经有匹配的括号
如果有就不再键入),而是直接跳出
或许你得加:
set autoindent
set cindent
参考:
http://www.cnblogs.com/huanlei/archive/2012/04/02/2430153.html
http://blog.sina.com.cn/s/blog_01ea59580101hvth.html