9

I want to input 90 number-series starts from 1, here is what I did:

set-mark
type enter 90 times
C-u C-x r N Enter %3d Enter

The output is like this:

1 
2 
3 
4 
5 
6 
7 
....
90

Is there a way to set the amount of output? (I don't want to hit enter / or hold enter for 90 times.)

Nick
  • 4,423
  • 4
  • 24
  • 41

7 Answers7

11

As another answer suggests, you can insert 90 new lines using prefix argument 90 like this: C-u 90 RET. Then insert the number list using C-u C-x r N as you did. However, if you want a more flexible format of numbered list, you can use Keyboard Macro counter.

I included some exercises in my guide. Here is one:

Exercise 1: Creating incremental header prefix

Format specification is useful for making formatted output with macro, combine with the counter, it is useful for appending numbered prefix at beginning of line. For example: We usually write code comment that describes sequential steps in a high level point of view like this:

  • Step 1 of 5: ...
  • Step 2 of 5: ...
  • Step 3 of 5: ...
  • Step 4 of 5: ...
  • Step 5 of 5: ...

Keyboard macro can help us generate such text pattern effortlessly.

  • C-x C-k C-f and enter this format: - Step %d of 5:
  • F3 to start recording.
  • F3 again to insert the first counter value, which is - Step 0 of 5:
  • RET to move to the next line.
  • F4 to stop recording.
  • Now press F4 as many time as you want and see header prefix got inserted with incremental values. You can insert the text any value by simply set the counter with C-x C-k C-c.

Remember to use this the next time you write comments for your code that need an ordered list to describe the steps of your algorithm.

Of course, instead of enter - Step %d of 5:, you can just enter %d. If you want the macro to repeat 90 times, just supply 90 as prefix argument.

To make it easier for you to use macro, remember that all macro commands have C-x C-k as prefix.

Tu Do
  • 6,772
  • 20
  • 39
  • Thank you very much for the explanation on macro counter. Although it requires many steps, it may be useful in other places as well. – Nick Dec 23 '14 at 00:48
8

Tu Do's answer is very good and well worth remembering for custom counter formatting, but potentially (depending on the actual use-case) a little excessive for this particular example, given that %d is the default format for the counter.

The output can therefore be achieved like so:

M-1F3F3RETM-9M-0F4

phils
  • 48,657
  • 3
  • 76
  • 115
  • M-90<\kbd> needs to be C-u 90<\kbd> for it to work on my machine. – user2699 Dec 22 '14 at 06:22
  • @user2699 Are you using Emacs in terminal? – Tu Do Dec 22 '14 at 08:10
  • Ah, interesting. I find that `C-` doesn't function in my terminal, but `M-` *does* work, which is why I've taken to using the meta bindings in answers. `C-u` is always an alternative, of course. Which OS & terminal do you use? – phils Dec 22 '14 at 09:22
  • Note that by `M-90` I mean `M-9 M-0`. I think this is the first time I've tried to condense that sort of sequence into a single kbd tag, and perhaps `M-90` is too easy to misinterpret? – phils Dec 22 '14 at 10:59
  • Actually, M-90 works, what I tried the first time was M-9 M-0, but I've rebound M-0, and that's what caused it to fail. I've only ever seen C-u used for repeats, so this is a great thing to learn. – user2699 Dec 22 '14 at 20:51
8

It's tiny's time to shine:

m\n90

will expand (with tiny-expand) to what you want.

There's also rich formatting and templating that can be applied.

  • m5|%02d:

    00 01 02 03 04 05
    
  • m7|%(expt 2 x):

    1 2 4 8 16 32 64 128
    
  • m1\n5|*** TODO http://emacsrocks.com/e%02d.html:

    *** TODO http://emacsrocks.com/e01.html
    *** TODO http://emacsrocks.com/e02.html
    *** TODO http://emacsrocks.com/e03.html
    *** TODO http://emacsrocks.com/e04.html
    *** TODO http://emacsrocks.com/e05.html
    
  • m\n3|* TODO Wash dog\nDEADLINE: <%(date "Oct 14" (* x 4))>

    * TODO Wash dog
    DEADLINE: <2015-10-14 Wed>
    * TODO Wash dog
    DEADLINE: <2015-10-18 Sun>
    * TODO Wash dog
    DEADLINE: <2015-10-22 Thu>
    * TODO Wash dog
    DEADLINE: <2015-10-26 Mon>
    

tiny is available in MELPA.

abo-abo
  • 13,943
  • 1
  • 29
  • 43
7

You can use a numeric argument to avoid typing RET 90 times. Just do M-9 0 RET to insert 90 newlines.

Alternatively, you can write a simple function that creates a range of numbers and loops over them using number-sequence and dolist:

(defun insert-numbers (start end)
  (interactive "nStart: \nnEnd: ")
  (dolist (ind (number-sequence start end))
    (insert (format "%d\n" ind))))
Pradhan
  • 2,330
  • 14
  • 28
2

Just for the sake of diversity:

  1. C-x * c (Starts the calculator).
  2. 1RETv b90RET creates a vector 90 elements long.
  3. V U + calls to accum Calc function which acts similar to reduce, putting the intermediate results into positions of the elements which generate those. At this point you have a vector which looks like [1, 2, .... 89, 90].
  4. Copy the vector into the file you are editing.
  5. C-x f3 this will set fill-column to be 3 elements wide.
  6. Select the contents of the vector and press M-q to fill paragraph (this will put every number on a separate line.

This is obviously more cumbersome than just using a recorded macro, but it has its perks in that you can create more complicated number sequences :)

wvxvw
  • 11,222
  • 2
  • 30
  • 55
  • By default the keyboard macro counter increases by 1, but you can use any number to add to the current counter with `C-x C-k C-a`. – Tu Do Dec 22 '14 at 08:09
  • @TuDo Well, you can't make a Fibonacci or factorials... :P – wvxvw Dec 22 '14 at 12:15
  • well if it's that complicated then isn't it better to use straight Elisp? But even so, you can bind a command that can inserts arbitrary number (such as the sequence you suggested) into the buffer at point and use it with a macro. – Tu Do Dec 22 '14 at 15:00
  • @TuDo that wasn't a serious comment. I don't think any sane person will want to number lines using Fibonacci numbers :) (However, I would probably use Calc rather than numbers in the macro simply because I never remember the command for it, and I use Calc often enough to perform the above from memory). – wvxvw Dec 22 '14 at 15:42
  • Fair enough :) But it's nice to see something unusual. – Tu Do Dec 22 '14 at 15:51
1

You can set up a sequence of counters starting from START and with step STEP using

C-x C-k C-c START RET F3 C-u STEP F3 F4,

where is anything else you want to include in your macro. This inserts the first counter, with value START. Afterwards, F4 will insert the next value in the sequence and C-u N F4 will insert the next N values from the sequence.

Note,

  • if you don't specify START (i.e. if you do just F3 C-u STEP F3 F4) it'll start from 0.
  • if you don't specify the STEP (i.e. skip C-u STEP), it'll be 1.
  • You can use C-x C-k C-f to specify the format of the counter.

This way you can enter your sequence with

C-x C-k C-c 1 RET F3 F3 RET F4 C-u 8 9 F4


You can also invoke eval-expression (M-:) and evaluate

(dotimes (i 90) (insert (format "%2d.\n" (1+ i))))

or more in general,

(dotimes (i SEQ_LENGTH) (insert (format "%2d\n" (+ START (* i STEP)))))
Arch Stanton
  • 1,525
  • 9
  • 22
0

If you turn on the CUA minor mode (cua-mode), you can select a column of text and then fill it with a sequence of numbers using "cua-sequence-rectangle" (bound to M-n by default).

a_subscriber
  • 3,854
  • 1
  • 17
  • 47