EDIT: I have figured out how to do it.
How to configure Emacs to automatically close brackets like in VS code or other text editors?
I switched from Vim to the latest version of Doom Emacs and didn't like the behavior of auto-closing brackets. Specifically, I don't like two things:
Auto erasing closing bracket('|' -> cursor):
int main() { {| }
becomes:
int main() { |
No new line with indentation after pressing Shift+Enter:
Shift+Enter:
int main() { { |} }
Just Enter:
int main() { { | } }
I tried several solutions from the internet, but neither of them worked as I wanted(also I tried hacking on them, but it was really hard as I am a noob in Elisp and Emacs). My requirements are not that high, though:
- Add a closing bracket
- Add new line and indent after I press Enter (and Shift+Enter)
- Don't remove the closing bracket when I erase the opening one (I usually start erasing from the closing bracket and when I reach opening one, it removes the next closing bracket)
In vim, I used really simple bindings:
" brackets
inoremap " ""<left>
inoremap ' ''<left>
inoremap ( ()<left>
inoremap [ []<left>
inoremap { {}<left>
inoremap {<CR> {<CR>}<ESC>O
inoremap {;<CR> {<CR>};<ESC>O
I tried to implement it in Emacs but couldn't. Will appreciate some help.
So is there a good way for auto closing brackets in Emacs?