Skip to content

Comment on Learn Emacs: Keyboard Macrosparent

Comments

You should check out the EmacsWiki macro tricks page:

http://www.emacswiki.org/emacs/KeyboardMacrosTricks

From there:

    If you want to produce this list:

    test1
    test2
    test3
    ...

    Do like this:

    C-x r n q           (store the number 1 in register q)
    C-x (               (start macro recording)
    test C-x r i q      (insert contents of register q)
    C-x r + q           (increment register q by 1)
    RET                 (new line)
    C-x )               (stop macro recording)

The easy way I do it is to make however many copies of "test0" I want, then make a trivial throwaway macro where I move my cursor to the number I want to increment, and hit M-x rolling-increment-number-at-point. (Of course with autocomplete...)

Here's my definitions:

  (defvar *rolling-counter* 0)
  
  (defun rolling-increment-number-at-point ()
    (interactive)
    (incf *rolling-counter*)
    (skip-chars-backward "0123456789")
    (or (looking-at "[0123456789]+")
        (error "No number at point"))
    (replace-match (number-to-string (+ *rolling-counter*
                                        (string-to-number (match-string 0))))))
  
  (defun clear-rolling-history ()
    (interactive)
    (setf *rolling-counter* 0))

Ow, that hurts my brain more than usual. How would one leverage registers with text instead of incremental numbers? (I will have to RTFW later.)

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.