0

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?

RinRi
  • 1
  • 1
  • What are the modules toggled on in your init.el and/or the packages installed in packages.el? Since Doom Emacs has some default but we are encouraged to change them when needed My first impression is the usage of [smartparen](https://github.com/Fuco1/smartparens) which is toggled by a `+smartparen` flag as argument like this `:config ( default +smartparen )` But other packages could create a similar behavior. :) Also, what are the language you mostly use other than C? You may need specific settings depending on the chosen solution. Have a nice day. ^^ – Raphaël Duchaîne Jul 26 '21 at 18:34
  • 1
    @RaphaëlDuchaîne, thank you for your reply! I have figured out how to do it. You can check what I did on [my reddit post](https://www.reddit.com/r/emacs/comments/o6c33n/how_can_i_automatically_close_brackets_in_emacs/?utm_source=share&utm_medium=web2x&context=3). Have a nice day! – RinRi Jul 27 '21 at 11:13
  • Some apparent duplicates: https://stackoverflow.com/questions/3801147/how-can-can-i-get-emacs-to-insert-closing-braces-automatically https://stackoverflow.com/questions/1023770/automatically-closing-braces-in-emacs https://stackoverflow.com/questions/7718975/intelligent-auto-closing-matching-characters https://stackoverflow.com/questions/30018851/does-there-exist-an-emacs-intelligent-parenthesis-matching-plugin-configuration. – phils Aug 01 '21 at 08:05
  • Note that S.O. protocol is to post an Answer to your own Question if you figure it out by yourself. (You may then Accept your own answer.) Your reddit link seemed to be more about indentation than auto-closing brackets, but from what I can tell you've gone with electric-pair-mode so I've closed this as a duplicate. – phils Aug 01 '21 at 08:08

0 Answers0