From 9ffe088c5a1dcdb096e1a5eca8d5540680970a1a Mon Sep 17 00:00:00 2001 From: Lifepillar Date: Wed, 12 Jul 2017 11:56:10 +0200 Subject: [PATCH] Add function to auto-close quotes. The rule is as follows: if a quote is typed and it is immediately followed or preceded by another quote of the same type, it is inserted as usual. Otherwise, a pair of quotes is inserted, and the cursor is positioned in between. --- plugin/AutoPair.vim | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/plugin/AutoPair.vim b/plugin/AutoPair.vim index ccbb317..2faa7e5 100644 --- a/plugin/AutoPair.vim +++ b/plugin/AutoPair.vim @@ -13,6 +13,9 @@ inoremap { {}U inoremap ) :call AutoPair#Close(')') inoremap ] :call AutoPair#Close(']') inoremap } :call AutoPair#Close('}') +inoremap " :call AutoPair#Quote('"') +inoremap ' :call AutoPair#Quote("'") +inoremap ` :call AutoPair#Quote('`') "}}} " New line between brace pair {{{ @@ -20,6 +23,13 @@ inoremap } :call AutoPair#Close('}') inoremap :call AutoPair#InsertBlankLine(@.) "}}} +fun! AutoPair#Quote(mapChar) + let l:nextChar = strpart(getline('.'), col('.') - 1, 1) + let l:prevChar = strpart(getline('.'), col('.') - 2, 1) + let l:feedkeys = (l:nextChar ==# a:mapChar || l:prevChar ==# a:mapChar) ? a:mapChar : a:mapChar . a:mapChar . "\" + call feedkeys(l:feedkeys, 'n') +endf + function! AutoPair#Close(mapChar) "{{{ let l:nextChar = strpart(getline('.'), col('.') - 1, 1)