xdg-open https://filename.sh

How to Autocomplete Parentheses, Brackets, Braces, etc., in Vim?

The code on this page may only works on GNU/Linux system only.

			
function! GetChar(...)
	return matchstr(getline('.'), '\%' . (col('.')-a:1) . 'c.')
endfunction

inoremap <expr> ' (GetChar(0) ==# "'" ? "<Right>" : "''<Left>")
inoremap <expr> " (GetChar(0) ==# '"' ? '<Right>' : '""<Left>')
inoremap ( ()<Left>
inoremap <expr> ) (GetChar(0) ==# ")" ? "<Right>" : ")")
inoremap [ []<Left>
inoremap <expr> ] (GetChar(0) ==# "]" ? "<Right>" : "]")
inoremap <expr> { ((GetChar(1) ==# " " && GetChar(2) ==# ")") \|\| GetChar(1) ==# ")" ? "{<CR><CR>}<Up><Tab>" : "{}<Left>")
inoremap <expr> } (GetChar(0) ==# "}" ? "<Right>" : "}")
			
		

Let me talk about the meaning of the code above!

  1.  I creat a function GetChar(). The argument of the function should be a number. If the argument equals 0, GetChar() return the charactor under the cursor; if the argument equals 1, GetChar() return 1 charactor before the cursor, etc. The 3 dots in the parentheses mean the function is able to receive argument, we can see that I call the 1st argument as "a:1"
  2. 				
    function! GetChar(...)
    	return matchstr(getline('.'), '\%' . (col('.')-a:1) . 'c.')
    endfunction
    				
    			
  3.  Use inoremap to remap single quotation marks in insert mode, and prevent calling other mappings. The statement after the first single quotation mark of this line means: When user press the single quotation button, if the charactor under the cursor is a single quotation mark (# means case-sensitive), mock pressing right button; if not, type two single quotation marks and mock pressing left button.
  4. 				
    inoremap <expr> ' (GetChar(0) ==# "'" ? "<Right>" : "''<Left>")
    				
    			
  5.  Same as the 2nd point, just change single quotation marks to double quotation marks.
  6. 				
    inoremap <expr> " (GetChar(0) ==# '"' ? '<Right>' : '""<Left>')
    				
    			
  7.  Whenever press the left parenthesis button, type a left parenthesis and a right parenthesis and mock pressing the left button.
  8. 				
    inoremap ( ()<Left>
    				
    			
  9.  When user press the right parenthesis button, check if the charactor under the cursor is right parenthesis or not. If yes, mock pressing the right button; If no, just type a right parenthesis mark.
  10. 				
    inoremap <expr> ) (GetChar(0) ==# ")" ? "<Right>" : ")")
    				
    			
  11.  Same as the 4th point, just change left parentheses to left brackets; right parentheses to right brackets.
  12. 				
    inoremap [ []<Left>
    				
    			
  13.  Same as the 5th point, just change right parentheses to right brackets.
  14. 				
    inoremap <expr> ] (GetChar(0) ==# "]" ? "<Right>" : "]")
    				
    			
  15.  The conditional statement here is the most complicated, because I want to let it work more like a modern IDE. Be careful that you should use backslashes before pipe symbols, or it will not work.
  16. 				
    inoremap <expr> { ((GetChar(1) ==# " " && GetChar(2) ==# ")") \|\| GetChar(1) ==# ")" ? "{<CR><CR>}<Up><Tab>" : "{}<Left>")
    				
    			
  17.  Same as the 5th point, just change right parentheses to right braces.
  18. 				
    inoremap <expr> } (GetChar(0) ==# "}" ? "<Right>" : "}")
    				
    			

Lastest update: Sep 7th 2025