问题:怎样在vim中实现花括号引号自动补全,包括html标签?
解决办法:只要把下面两段代码粘贴到~/.vimrc中,就可以实现括号超强补全
" 括号引号补全
inoremap ( ()i
inoremap [ []i
inoremap { {}O
inoremap ) =ClosePair(')')
inoremap ] =ClosePair(']')
inoremap } =CloseBracket()
inoremap " =QuoteDelim('"')
inoremap ' =QuoteDelim("'")
function ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\"
else
return a:char
endif
endf
function CloseBracket()
if match(getline(line('.') + 1), '\s*}') < 0
return "\}"
else
return "\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 "\"
else
"Starting a string
return a:char.a:char."\i"
endif
endf
" html自动补全
autocmd BufNewFile * setlocal filetype=html
function! InsertHtmlTag()
let pat = '\c'
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 > :call InsertHtmlTag()aO
之所以这里的括号补全代码中的函数实现反匹配
当打入(输入内容),再按)系统会自动检查前面是否已经有匹配的括号
如果有就不再键入),而是直接跳出
或许你得加:
set autoindent
set cindent
参考:
http://www.cnblogs.com/huanlei/archive/2012/04/02/2430153.html
http://blog.sina.com.cn/s/blog_01ea59580101hvth.html
原文:http://blog.csdn.net/zcube/article/details/42325815