Last active
August 8, 2022 08:28
-
-
Save bfrg/11ddfaa04bb7fd4813a15c3612bfeb36 to your computer and use it in GitHub Desktop.
Automatically close, skip and remove matching pairs in insert-mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vim9script | |
# Auto-close parentheses | |
inoremap ( ()<c-g>U<left> | |
inoremap [ []<c-g>U<left> | |
inoremap { {}<c-g>U<left> | |
inoremap < <><c-g>U<left> | |
inoremap (<cr> (<cr>)<esc>O | |
inoremap {<cr> {<cr>}<esc>O | |
inoremap [<cr> [<cr>]<esc>O | |
inoremap {; {<cr>};<esc>O | |
# Remove matching pairs | |
inoremap <expr> <c-h> col('.') > 1 && getline('.')[charcol('.') - 2 : charcol('.') - 1] =~ '^\%(()\\|{}\\|\[]\\|<>\\|""\\|''''\\|``\)$' ? '<bs><del>' : '<bs>' | |
inoremap <expr> <bs> col('.') > 1 && getline('.')[charcol('.') - 2 : charcol('.') - 1] =~ '^\%(()\\|{}\\|\[]\\|<>\\|""\\|''''\\|``\)$' ? '<bs><del>' : '<bs>' | |
# Remove auto-inserted closing pair | |
inoremap <expr> <c-j> col('.') > 1 && getline('.')[charcol('.') - 2 : charcol('.') - 1] =~ '^\%(()\\|{}\\|\[]\\|<>\\|""\\|''''\\|``\)$' ? '<del>' : '<c-j>' | |
# Skip closing parenthesis | |
inoremap <expr> ) getline('.')[charcol('.') - 1] == ')' ? '<c-g>U<right>' : ')' | |
inoremap <expr> ] getline('.')[charcol('.') - 1] == ']' ? '<c-g>U<right>' : ']' | |
inoremap <expr> } getline('.')[charcol('.') - 1] == '}' ? '<c-g>U<right>' : '}' | |
inoremap <expr> > getline('.')[charcol('.') - 1] == '>' ? '<c-g>U<right>' : '>' | |
inoremap <expr> ` getline('.')[charcol('.') - 1] == '`' ? '<c-g>U<right>' : '``<c-g>U<left>' | |
inoremap <expr> " getline('.')[charcol('.') - 1] == '"' ? '<c-g>U<right>' : '""<c-g>U<left>' | |
# Single-quote auto-pair, but not when it's an apostrophe | |
inoremap <expr> ' | |
\ getline('.')[charcol('.') - 2] =~ '\w' | |
\ ? getline('.')[charcol('.') - 1] == "'" ? '<c-g>U<right>' : "'" | |
\ : getline('.')[charcol('.') - 1] == "'" ? '<c-g>U<right>' : "''<c-g>U<left>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment