Write `var0, var1, var2, …, var10` in a buffer

Write `var0, var1, var2, …, var10` in a buffer



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-@)").


(insert-last-message)



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




4 Answers
4



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





Cool! Is it possible to make the macro counter start from another number?
– Nisba
Aug 30 at 15:24





If you start recording your macro with F3, you can give it a numeric argument with the number to start from.
– DoMiNeLa10
Aug 30 at 15:36


F3





Don't forget to delete the extra comma at the end. :)
– Omar
Aug 30 at 22:38



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


print


insert


dotimes


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



(alternatively you can use keyboard macros for this)



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:


F3


C-x (


F4


C-x e


m0, 9|var%d


tiny-expand


m0, 9|var%d


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.


0


9



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


(s-join ", " (loop for i below 10 collect (format "var%s" i)))



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.





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 at 21:10


string-join


s





@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 at 21:37


string-join


(eval-when-compile (require 'subr-x))


loop


(eval-when-compile (require 'cl))


cl-loop


cl-lib.el


loop


cl.el


(cl) Organization



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)