Emacs Key Macro Example: Eval Emacs Lisp Repeatedly

By Xah Lee. Date:

Here's another example of kmacro use.

I have a function insert-random-uuid [see Emacs Lisp Exercise: insert-random-uuid] I want to call it hundreds of times to see its output. Let's just say i want to call the elisp expression (random 100) one hundred times.

One way is to write a elisp command on the spot, like this:

(defun xx-random-test ()
  (dotimes (ii 100) (insert (format "%d " (random 100))))

But that takes 5 minutes to write, plus you need to have elisp knowledge. You can use a kmacro to do this fast, with the following steps.

  1. Start kmacro 【Ctrl+x (】.
  2. Type 【Ctrl+u】, then Alt+x eval-expression, with this expression (random 100). The 【Ctrl+u】 will make eval-expression insert its result in current buffer.
  3. Type a space.
  4. End kmacro 【Ctrl+x )】.
  5. Type 【Ctrl+u 100】 then call kmacro-end-and-call-macroCtrl+x e】.

Emacs Keyboard Macro Topic

  1. Keyboard Macro
  2. Key Macro Example: Add Title of URL
  3. Key Macro Example: Add HTML Attribute
  4. Key Macro Example: Modify Webfeed Entries
  5. Key Macro Example: Insert All Unicode Bullets
  6. Key Macro Example: Eval Emacs Lisp Repeatedly

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp