3

Upon pressing enter inside a newly created block I would like the Emacs to move the ending curly brace an extra newline down, and then indent the cursor. Is this possible?

Example

Before pressing Enter

for i := range x {<cursor>}

After pressing Enter once I get this

for i := range x {
<cursor>}

However what I would like to get is this

for i := range x {
    <cursor>
}
3

I use smartparens for exactly this. It uses sort of a cryptic syntax in the :post-handlers keyword for doing things after you insert a pair and press a button. Here's a pair config to do what you want:

(sp-pair "{" nil :post-handlers '(("||\n[i]" "RET")))

And the relevant documentation on the Github wiki: Pre and Post Action Hooks

  • This was exactly what I was looking for :) I actually already use smartparens (which is the whole reason my cursor ends up in between the curly braces) but must admit I didn't quite get the action hooks. – jsfr May 13 '15 at 13:05

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.