Last active
May 13, 2025 06:47
-
-
Save wa008/4da70e1970b590497bf057e4358a6248 to your computer and use it in GitHub Desktop.
[vimrc] Bracket and quote matching and automatic shifting
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
" How to use: append below code into your ~/.vimrc file | |
inoremap ( ()<LEFT> " ( -> () | |
inoremap [ []<LEFT> " [ -> [] | |
inoremap { {}<LEFT> " { -> {} | |
" shift right when input ) and current char is ) | |
function! RemoveNextDoubleChar(char) | |
let next_char = getline(".")[col(".")] " get current char | |
if a:char == next_char | |
execute "normal! l" | |
else | |
execute "normal! a" . a:char | |
end | |
endfunction | |
inoremap ) <ESC>:call RemoveNextDoubleChar(')')<CR>a | |
inoremap ] <ESC>:call RemoveNextDoubleChar(']')<CR>a | |
inoremap } <ESC>:call RemoveNextDoubleChar('}')<CR>a | |
" 1. ' -> '', " -> "" and move cursor in the middle of these two char | |
" 2. shift right when you input ' and currect char is ' | |
function! RemoveOrDoubleChar(char) | |
let next_char = getline(".")[col(".")] " get current char | |
if a:char == next_char | |
execute "normal! l" | |
else | |
let current_line = getline(".") | |
let first_char_col_index = match(current_line, '\S') | |
if first_char_col_index == -1 || (col(".") != first_char_col_index + 1) | |
execute "normal! a" . a:char . a:char . "\<Esc>h" | |
else | |
execute "normal! i" . a:char . a:char . "\<Esc>h" | |
endif | |
endif | |
endfunction | |
inoremap ' <ESC>:call RemoveOrDoubleChar("'")<CR>a | |
inoremap " <ESC>:call RemoveOrDoubleChar('"')<CR>a | |
" all basic vimrc config I used: https://gist.github.com/wa008/f82559aa3354afe16c06ac5527f4612c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment