Felix D.
Felix D.

Reputation: 2220

Indentation inside brackets and not beside closing bracket in emacs

I'm finally making the big jump, leaving Vim for Emacs, and I can say I'm pretty delighted already (not in term of speed for now... but that will catch up).

However, there is this thing I'm trying to figure out...

When I code a for loop in C, for example, and add an opening braces, this is what I get

code1

Then I expand braces by pressing C-j and this is where I end.

code2

The issue is, I'm used to end there (this is how my vim was configured)

code3

But I can't figure out how to do this... I'm just starting with emacs and lisp and seem overwhelmed already by that tiny thing. I've already mapped a chord so when I press kk I insert an empty line above the current line and indent it properly. So for now, what I do is RET kk but i'd like my return key to do that by itself.

Upvotes: 3

Views: 509

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74058

You can configure where newlines are inserted with c-hanging-braces-alist

Hanging Braces
To specify which kinds of braces you want auto-newlines put around, you set the style variable c-hanging-braces-alist.

E.g., to keep the brace on the same line as for and insert a newline after it

(custom-set-variables
 '(c-hanging-braces-alist (quote ((substatement-open . (after))))))

Another approach could be to use Yasnippet and define an appropriate template

# -*- mode: snippet -*-
# name: for
# --
for ($1; $2; $3) {
$0
}

Now you can enter for and tab and Yasnippet will automatically insert

for (; ; ) {

}

and wait for you to insert the fields leaving the cursor between the braces when finished.

Upvotes: 3

Related Questions