I'm experiencing with ways to configure Emacs to enable me to type less, but achieving the same thing, by exploiting the fact that in programming, you do (type) more or less the same thing over and over. I need this for medical reasons, but also because it makes sense in general.

The Emacs-interface I envisage is basically mixing commands with arguments. I'll just show an example and you'll understand immediately.

OK, you don't have to be that sharp to see, that in C, the for loop, "less than something, increment by one", is very common. So I thought I'd write something like this:

(defun C-for-loop-new-int-less-inc (var start stop)
  "Shortcut for putting a C for loop at point.
   Note: There are some specifics to i."
  (interactive "sVariable: \nnStarts at: \nnLess than: ")
   (format "for (int %s = %d; %s < %d; %s++) {\n\n}"
           var start var stop var) )
  (c-indent-line-or-region) )

And to reduce typing:

(defalias 'fnili 'C-for-loop-new-int-less-inc)

Now, M-x fnili RET i RET 0 RET 10 RET should get you

for (int i = 0; i < 10; i++) {


with the cursor at the second row, with indentation.

While that's an improvement, how can I fire off the command like this:


That way, all the RET-wait-reorient cycles would vanish, instead you'd write the entire command, once, and it would be visible all the time.

Now, I now I could write a function that takes a string, and then parses it, but I won't do that if Emacs already has support for such an interface that I seek.


So, "template facility" is the designation?

I don't think it is that hard to do. If you want this for the entire C language, and for all possible twists and turns, then I agree. But to identify just a few time consuming cases (for, switch-case, the guard in header files) and implement those, that would be a helpful feature.

Of course, if work has been done, I'm not doing it from scratch (as I said :), so I'll check out your links for sure.

But... I think my example was too complicated so it overshadowed the actual question, which is about the interface: instead of hitting RET and getting a blank line for each argument, putting it all on a single command line.

A much more simple example: Instead of M-x man RET irssi RET, how can I do M-x man"irssi" RET? In this example, it is not really beneficial (slightly perhaps) because it is only a single argument, and there is nothing complicated to it (that you need to keep in your brain), and you will seldom make a mistake that you have to "backtrack" to correct. But, in the for loop example command in the original post (fictitious, as I've never seen it, if it exists) - in that case, you would really benefit from it.

  • Look at keyboard macros. There is also a way of defining shortcuts/abbreviations. Rarely use the first, never learned about the later (in some 20 years of intensive emacs usage).
    – vonbrand
    Commented Apr 7, 2013 at 19:18
  • @vonbrand: Check out the edit. Actually, I intuitively dislike stuff like that myself. I think it is much cooler (and relaxing) just to type. But my fingers and eyes do not agree, so I have to adjust just a bit. Commented Apr 8, 2013 at 14:55
  • 1
    What you're describing for is extremely similar to yasnippet. It seems to come with a for snippet for cc-mode, which works out of the box in c-mode. Take a look at the default snippets for other examples.
    – Inaimathi
    Commented Apr 8, 2013 at 17:07

1 Answer 1


A template facility that actually helps is hard to design and requires a nontrivial amount of code. I recommend looking for something existing and possibly tuning it to your needs, rather than building it from scratch.

The Emacs Wiki lists a few packages. The main candidates are:

  • Made an edit that clarified (hopefully) what I seek. Commented Apr 8, 2013 at 14:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .