4

Most of the times there is no need to write manually many variables with the same name and a different index, like

var0, var1, var2, var3, ..., var9

because one can use arrays, vectors or whatever the language offers.

Anyway, I just needed to write manually ten variables as shown above. What is the fastest way to do it with emacs?

I guess that the fastest way is to write some elisp code. I tried doing it with this code

(setq i 0)
    (while (< i 10) (print (format "var%d, " i)) (setq i (+ 1 i))

followed by (insert-last-message) defined here. It does not work (I get "(#o0, #x0, ?\C-@)").

What's wrong here? Is there any faster way?

4 Answers 4

7

The fastest way is to use the macro counter:

C-x( starts recording a macro
varF3,space inserts 0 as the initial value of the counter
M-1M-0 repeat the following 10 times
C-xe execute the macro

4
  • Cool! Is it possible to make the macro counter start from another number?
    – Nisba
    Aug 30, 2018 at 15:24
  • 2
    If you start recording your macro with F3, you can give it a numeric argument with the number to start from.
    – user12563
    Aug 30, 2018 at 15:36
  • Don't forget to delete the extra comma at the end. :)
    – Omar
    Aug 30, 2018 at 22:38
  • A numeric prefix argument means the same thing to C-x ( as it does to <f3>, so you don't need to use the function key for that (but it's shorter, so you may as well). If for any reason you can't, note that C-x C-k TAB also inserts-and-increments the counter.
    – phils
    Oct 3, 2019 at 1:07
3

print will output to the echo area rather than putting stuff in your buffer. You're looking for insert and dotimes:

(dotimes (i 10) (insert (format "var%d, " i)))

(alternatively you can use keyboard macros for this)

2

I'd use a macro just as @choroba did (with F3 in place of C-x ( and F4 instead of C-x e, for brevity and to be able to easily specifying a non-zero starting number, as @DoMiNeLa10 mentioned), but if you want a possibly more ergonomic solution, there is abo-abo's tiny package. You'd type m0, 9|var%d into your buffer, and then execute tiny-expand and it would replace m0, 9|var%d with:

var0, var1, var2, var3, var4, var5, var6, var7, var8, var9

for you. (I used it just now to type that.)

Notice that the comma and space between 0 and 9 are used as a separator, that is, you don´t get an extra one at the end like you would with the most straightforward macro.

1

If you use lispy (https://github.com/abo-abo/lispy), you can do this:

  1. Enter (s-join ", " (loop for i below 10 collect (format "var%s" i))) into your elisp buffer. At the end of the sexp, type xr which will evaluate and replace the sexp with a string in quotes. Type C-b to get the cursor on the last quote, then type C-u " to unquote the string.

alternatively, if you type the code @rpluim suggested, and type xr, it will also replace it with about the same thing, you just have to delete a nil and the last comma.

I am not sure it is faster than a macro counter, but since I like lispy, it is a nice solution for me.

2
  • There is also the builtin string-join (which takes its arguments in the opposite order), if you don't have s installed.
    – Omar
    Aug 31, 2018 at 21:10
  • @Omar string-join is built-in, but it requires (eval-when-compile (require 'subr-x)). Just like loop requires (eval-when-compile (require 'cl)) (though cl-loop from cl-lib.el is generally preferred to loop from cl.el; see (cl) Organization).
    – Basil
    Aug 31, 2018 at 21:37

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.