3

I very much {like, use} the kmacro-*-counter family of commands. However I sometimes find myself needing to generate alphabetic sequences (e.g., A, B, C, ...) instead of integer sequences (e.g., 1, 2, 3 ...). I can sorta see how to do this in 2 passes using query-replace-regexp and something like make-string, but that means 1st generating the integer sequence and then replacing it.

Is there an easier way? Particularly, is there some {command, function, package} that will support a usecase like

  1. M-x foo-set-alpha-counter A
  2. (loop) M-x foo-insert-counter

?

  • 1
    I don't use macros, but you may find it helpful to know that Emacs uses numbers for letters such as (char-to-string 65) is the capital letter "A". And, this increments sequentially, e.g., (char-to-string 66) is the capital letter "B". Lowercase letters begin at 97; e.g., (char-to-string 97) is the lowercase letter "a"; and, it continues sequentially; e.g., (char-to-string 98) is the lowercase letter "b". To check the number, you can do the reverse -- (string-to-char "A") returns the number 65. – lawlist Aug 29 '19 at 23:42
  • 1
    Coding is according to the ASCII standard. See 'man ascii' – Heikki Aug 30 '19 at 5:26
5

You can use the function kmacro-set-format, normally bound to C-x C-k C-f, to change the (printf-style) format used to insert the macro counter. You can set it to %c to treat the value of the macro counter as an ASCII code.

For example to produce a letter sequence like ABCDEF: set the format to %c, start the macro counter at 65 which is the ASCII code for A, then record a macro that inserts the counter and run it 6 times, say:

C-x C-k C-f %c RET C-u 65 <f3> <f3> C-6 <f4>

As a neat extra touch, if you change the format inside the keyboard macro, the change only affects that particular macro. So, after running the above, the format will still be %c for the next keyboard macro, but if instead you do:

C-u 65 <f3> C-x C-k C-f %c RET <f3> C-6 <f4>

then the %c is local to that macro.

%d to go back to numbers.

| improve this answer | |
  • 2
    Thanks! Works great, plus this validates my otherwise-bizarre notion that my laminated ASCII-code card from 1982 would eventually prove handy :-) – TomRoche Sep 1 '19 at 17:02
  • 1
    @TomRoche :) If you ever loose that laminated card and you use GNU/Linux, you can run man ascii to see a nice chart (M-x man or M-x woman from within Emacs). – Omar Sep 1 '19 at 17:57
  • @Omar Awesome tip. Is there any way to be able to just insert the char and have emacs convert it? – young_souvlaki Sep 18 at 22:48
  • Not as part of that key sequence; but you can certainly ask Emacs what the number is beforehand, by evaluating the read syntax for a character. E.g. M-: ?A tells you 65 (or vice versa, evaluating 65 tells you ?A). – phils Sep 23 at 0:42
  • If it helps, in elisp code you would use the read-char function to ask the user to type a character, and obtain that value. – phils Sep 23 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.