Skip to content

Instantly share code, notes, and snippets.

Created August 13, 2013 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d784e16d9a2eec79414d to your computer and use it in GitHub Desktop.
Save anonymous/d784e16d9a2eec79414d to your computer and use it in GitHub Desktop.
inoremap " "<esc>:call <SID>ScopeComplete()<cr>a
inoremap ' '<esc>:call <SID>ScopeComplete()<cr>a
inoremap ( (<esc>:call <SID>ScopeComplete()<cr>a
inoremap ) )<esc>:call <SID>ScopeComplete()<cr>a
inoremap { {<esc>:call <SID>ScopeComplete()<cr>a
inoremap } }<esc>:call <SID>ScopeComplete()<cr>a
inoremap [ [<esc>:call <SID>ScopeComplete()<cr>a
inoremap ] ]<esc>:call <SID>ScopeComplete()<cr>a
inoremap > ><esc>:call <SID>ScopeComplete()<cr>a
inoremap < <<esc>:call <SID>ScopeComplete()<cr>a
" Toggle scope completion by using either <leader>sc or :ScopeCompleteToggle
nnoremap <leader>sc :call <SID>ScopeCompleteToggle()<cr>
command! ScopeCompleteToggle :call <SID>ScopeCompleteToggle()
" Default to off. To turn on by default add "let g:scope_complete=1"
if !exists("g:scope_complete")
let g:scope_complete = 0
endif
" When an opening character is typed, automatically insert a closing character
" after it and place the cursor between the opening and closing characters.
" If the user types a closing character before the automatically inserted
" closing character then remove the automatically inserted character.
function! s:ScopeComplete()
if g:scope_complete
" opening chars as keys and closing chars as values
let scope_chs = {'(' : ')', '{' : '}', '[' : ']', '"' : '"', '''' : '''', '<' : '>',}
let line = getline('.')
let pos = getpos('.')
let ch = line[pos[2] - 1]
let next_ch = line[pos[2]]
echom "ScopeComplete() called for: " . ch
" if the ch is a closing ch and is the same as the next ch"
if index(values(scope_chs), ch) >= 0 && ch ==# next_ch
" delete the automatically inserted closing ch
execute "normal! x"
" if the ch is an opening ch
elseif has_key(scope_chs, ch)
" insert the corresponding closing ch and move cursor
" between the opening and closing ch's
execute "normal! a" . get(scope_chs, ch, '') . "\<esc>h"
endif
endif
endfunction
" Toggle on and off
function! s:ScopeCompleteToggle()
if g:scope_complete
let g:scope_complete = 0
else
let g:scope_complete = 1
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment