2

I don't know what the formal name of it is (this being one of the reasons that I can't find how to achieve it). But, what I want is to make Emacs start the second bracket with the same amount of space that is used in the first one and when you press Enter to go in a new line, that line to start with the same amount of spaces if there is a bracket.

I am not looking for a weird customization (unless it is a last resort). I just am looking for a typical feature in most IDE, that I am sure you use if you write code in your Emacs.

What I want to achieve is this:

for(i=0;i<10;i++)
{
    if(i<5)
    {

        /* I want by pressing Enter the cursor to start in here 
        so to write the print */
        print("Text")


    }
    /* I want this bracket to close automatically 
    under the first one (of the if) and not in the beginning. */

}
6

Your question appears to be dealing with two independent issues:

  1. you want the RET character to automatically indent; and
  2. you want an indentation style that is different from the Emacs default.

Electric RET

A character that performs something else than just inserting itself is called electric in Emacs. There are two ways in Emacs to make RET electric: the generic electric-mode, which you enable by doing M-x electric-indent-mode, and works in all modes; and the CC-mode specific minor mode electric mode, which you enable by typing C-c C-l in a CC-mode buffer.

Please try both, and choose whichever your prefer.

Changing the style of indentation

Your preferred indentation style differs from Emacs' default in two ways: it uses 4 spaces indentation, while Emacs uses 2, and it puts curly braces beneath the control operators, rather than on the same line.

The former issue is easily solved:

(setq-default c-basic-offset 4)

The latter is hardly more involved:

(setq c-default-style (cons '(c-mode . "k&r") c-default-style)

You can change the used style for a single buffer by doing M-x c-set-style. Please see the CC-Mode info manual for the available styles.

  • Thank you for your answer! I don't see it as a two different things because anyone writing code sees it as one. The result you want is to be able to have code blocks created automatically, that means brackets aligned. I will check your answer and get back to you! – Adam Nov 3 '14 at 1:44
7

smartparens, a solution for advanced handling of pairs (such as (), [], {}, <>, ...) supports pre- and post-action hooks. With these it's possible to make Emacs indent the content of a {} pair after hitting RET after the beginning {, the } is inserted at the same time as the {. The following snippet is taken from the documentation and sets up the described behaviour for C++:

(sp-local-pair 'c++-mode "{" nil :post-handlers '((my-create-newline-and-enter-sexp "RET")))

(defun my-create-newline-and-enter-sexp (&rest _ignored)
  "Open a new brace or bracket expression, with relevant newlines and indent. "
  (newline)
  (indent-according-to-mode)
  (forward-line -1)
  (indent-according-to-mode))
  • I disabled auto-pairs and installed smartparens and it somehow broke the intended behavior in doing {|}. I was going to disable smartparens and go back to auto-pair before I found this answer. :) – minibuffer Aug 23 '17 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.