
Learn Emacs: Keyboard Macros - binarysoul
http://rawsyntax.com/blog/learn-emacs-keyboard-macros/
======
drblast
There have been various times in my programming career that I've wanted an
emacs macro with arguments, so that it would do things like concatenate a
number in each line. Things like this in C:

    
    
      #define a 0
      #define b 1
      ...
      #define l 10
      #define m 11
    

Usually I take that as a sign that I need to write some other program to
generate the lines of the C program, but for some tasks that's overkill and I
want an automated way to create a list of sequential things so I don't have to
type 1 down 1 down 1 down 1 down 2 down 2 down 2 down...

Surely this is a solved problem.

~~~
jfager
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)

~~~
calibraxis
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))

------
ryanwatkins
Each time I consider trying another IDE, I turn on the sometimes available
"use emacs keybindings" feature.

Then I quickly remember I use the macro feature constantly and back I go to
Emacs.

------
hollerith
Long-time Emacs user here. (When I search for a string, read, send or reply to
an email, or set a timer to tell me when my eggs are ready, I use an Emacs
command I wrote.)

I just want to say that if you plan to learn how to write commands in Emacs
Lisp, you do not need to learn how to define or use keyboard macros. In my
experience, it is always easier just to write some lisp.

~~~
ajross
Ditto. I've had emacs in my fingertips for 17 years now, but virtually never
use macros. Repetitive editing stuff in my world is generally easier in perl.

Or more generally: repetitive structure in any text file (especially a
program) is generally a sign of bad design, and should be factored out in
whatever way is appropriate.

------
agumonkey
On the tangent, this may be the 10th article of this kind i've seen, the 1st
time it was nice to discover macro shortcuts, now I feel something is wrong.

Emacs documentation is large and well written, yet we discover features with
google, it's not even centralized, let's avoid duplication of efforts and
reinforce emacswiki. Maybe integrating emacswiki with the standard emacs
distribution in some way ? (if it's not done already.. which would render this
little rant finally funny)

~~~
tikhonj
I'm sure you know this already, but Emacs actually contains its manual in a
convenient form: C-h r opens it. There are other help commands that provide
plenty of information as well.

The issue isn't a lack of information built-in--Emacs is _self-documenting_ ,
after all--but that its difficult to prioritize features. The real value in
blog posts like this is when they describe features the author really likes
coupled with his actual use cases for it.

Ultimately, the author is just a curator in a special Emacs exhibit--he brings
out the real gems (from his point of view) and gives us some background on
them. I think something like that could be integrated into Emacs--but should
it?--by having a "tip of the week" or something like that.

------
binarysoul
disclaimer: I'm the author

Any suggestions for other emacs topics I should blog about?

Thanks for your interest.

~~~
Game_Ender
Something on code navigation & smart code completion.

I am going to echo a reply elsewhere on this thread and say you should
contribute to the emacswiki if you can as well.

~~~
binarysoul
Good idea. I'll be digging into this topic, and making a post on it soon

