Skip to content

Instantly share code, notes, and snippets.

@veirus
Created May 28, 2018 18:47
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 veirus/575b80c5c1728c952e4908f9b0f05c3d to your computer and use it in GitHub Desktop.
Save veirus/575b80c5c1728c952e4908f9b0f05c3d to your computer and use it in GitHub Desktop.
Simple function and mappings to close brackets automagically. In practice this shit is more irritating than helpful.
" Auto-insert closing parenthesis/brace {{{2
" TODO: make bracket insertion a little smarter
" right now it gets in the way more than it helps
inoremap ( ()<Left>
inoremap { {}<Left>
inoremap [ []<Left>
" Auto-delete closing parenthesis/brace
function! BetterBackSpace() abort
let cur_line = getline('.')
let before_char = cur_line[col('.')-2]
let after_char = cur_line[col('.')-1]
if (before_char == '(' && after_char == ')') || (before_char == '{' && after_char == '}') || (before_char == '[' && after_char == ']')
return "\<Del>\<BS>"
else
return "\<BS>"
endfunction
inoremap <silent> <BS> <C-r>=BetterBackSpace()<CR>
" Skip over closing parenthesis/brace
inoremap <expr> ) getline('.')[col('.')-1] == ")" ? "\<Right>" : ")"
inoremap <expr> } getline('.')[col('.')-1] == "}" ? "\<Right>" : "}"
inoremap <expr> ] getline('.')[col('.')-1] == "]" ? "\<Right>" : "]"
" }}}2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment