1

In the answer to a question on Quora, Gilles Castel wrote an answer highlighting the power of vim. The Question was:

How would you write a C program to print 1 to 100 without loop, recursion, or goto?

Here is the link to his Answer

How would one go about implementing the same in Emacs?

  • 4
    Why? What are you trying to do? What's the real question or use case? How will this question and its answer help Emacs users, including Emacs-Lisp programmers? – Drew Dec 6 '16 at 14:41
3

Emacs has keyboard macros with counter support. The gist of the solution would be (assuming default key bindings):

  • F3 to start recording a macro
  • Type printf("%d", "
  • F3 to insert the macro counter
  • Type the rest of the line, ");
  • F4 to end recording the macro
  • Use F4 with a prefix to run the macro n times, e.g. C-u 1 0 0 F4.

If you want to play editor golf and compare with that vim solution, I think the equivalent sequence would be this:

#include <stdio.h><RET>
int main(){<RET>
<F3>printf("%d ", <F3>);<RET>
<F4><C-p><C-S-backspace><C-u>100<F4>return 0;}
0
#include <stdio.h>
void main() { puts ("1 to 100"); }
0

You can use the command tiny-expand from the package tiny on ELPA.

Enter the following text and call the command (I like to bind it to C-o):

m1\n10|printf ("%%d ", %d);

The resulting buffer contents:

printf ("%d ", 1);
printf ("%d ", 2);
printf ("%d ", 3);
printf ("%d ", 4);
printf ("%d ", 5);
printf ("%d ", 6);
printf ("%d ", 7);
printf ("%d ", 8);
printf ("%d ", 9);
printf ("%d ", 10);

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.