Skip to content

Instantly share code, notes, and snippets.

@lnsoso
Forked from hotoo/autocomplete.match.pair.vim
Created June 23, 2010 17:14
Show Gist options
  • Save lnsoso/450218 to your computer and use it in GitHub Desktop.
Save lnsoso/450218 to your computer and use it in GitHub Desktop.
"inoremap ( ()<ESC>i
inoremap ( <c-r>=OpenPair('(')<CR>
inoremap ) <c-r>=ClosePair(')')<CR>
"inoremap { {}<ESC>i
inoremap { <c-r>=OpenPair('{')<CR>
inoremap } <c-r>=ClosePair('}')<CR>
"inoremap [ []<ESC>i
inoremap [ <c-r>=OpenPair('[')<CR>
inoremap ] <c-r>=ClosePair(']')<CR>
"inoremap < <><ESC>i
inoremap < <c-r>=OpenPair('<')<CR>
inoremap > <c-r>=ClosePair('>')<CR>
function! OpenPair(char)
let PAIRs = {
\ '{' : '}',
\ '[' : ']',
\ '(' : ')',
\ '<' : '>'
\}
let ol = len(split(getline('.'), a:char, 1))-1
let cl = len(split(getline('.'), PAIRs[a:char], 1))-1
if 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
" a:quote length is odd.
if (ql%2)==1
return a:quote
elseif getline('.')[col('.') - 1] == a:quote
return "\<Right>"
else
return a:quote . a:quote . "\<Left>"
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment