more emacs keyboard macro examples

Perm url with updates: http://xahlee.org/emacs/emacs_macro_example.html

Kmacro Example: Repeatedly Eval a Emacs Lisp Expression

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 ()
  "test"
  (interactive)
  (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.

  • Start kmacro 【Ctrl+x (】.
  • Type 【Ctrl+u】, then call “eval-expression”, with this expression (random 100). The 【Ctrl+u】 will make “eval-expression” insert its result in current buffer.
  • Type a space.
  • End kmacro 【Ctrl+x )】.
  • Type 【Ctrl+u 100】 then call “kmacro-end-and-call-macro” 【Ctrl+x e】.

Popular posts from this blog

Browser User Agent Strings 2012

11 Years of Writing About Emacs

does md5 creates more randomness?