3

How would one write a function that one could bind to, say, SUPER-f, which inserts the code:

for () {
}

in the buffer with my cursor being moved into the () parens for filling out the loop guard?

4
  • 1
    Have you already heard about yasnippet?
    – Nsukami _
    Feb 28, 2016 at 17:28
  • What did you try?
    – asjo
    Feb 28, 2016 at 17:52
  • Didn't try much of anything, since my knowledge of emacs is limited to what's builtin. However, I had not heard of yasnippet and it seems to do the trick.
    – eof
    Feb 28, 2016 at 18:35
  • @eof: if yasnippet solved your problem, please post an answer to your own question that details the solution, and accept it.
    – Dan
    Feb 29, 2016 at 0:08

1 Answer 1

3

I think I'd do it like this:

(define-skeleton my-for-statement
  "Insert a for () {...} skeleton."
  nil
  \n "for () {" \n > _ "}" \n)

The \n are for "newlines" (and the ones at beg/end are only added if needed). The _ basically says that if you have selected a region before running the command, then this region will end up right where _ is (IOW, the region will be wrapped by the "for" statement). And the > in front causes this region (if any) to be re-indented.

You can then do M-x my-for-statement and that will insert the statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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