1

Imagine I want to create a list with 10 items, i. e.

1. 
2. 
3. 
...
10.

In Vim I can use the command :for i in range(1,10) | put =i.'. ' | endfor for this.

How can I do the same thing in Emacs?

Evil mode does not seem to support it because I get the error message Unknown command: 'for'

1

Using eval-expression

You can run arbitrary Emacs Lisp code with eval-expression, by default bound to M-:

M-: runs the command eval-expression (found in global-map), which is an interactive compiled Lisp function in ‘simple.el’.

When called interactively, read an Emacs Lisp expression and evaluate it.

So we need to give an Emacs Lisp expression to it.

(dotimes (i 10) (insert (format "%s.\n" (1+ i))))

We use dotimes to iterate the variable i from 0 to 9. Each time through the loop, because you want to start at 1, insert one more than that number, plus a period and a newline.

Unfortunately, we can't simply use (insert (1+ i) ".\n") because inserting the number directly treats that number as a character, and character #1 is not the number 1.

1

Using a macro counter

We can record a macro that inserts text, and run it multiple times. Each time through the macro, we can use the Emacs feature called the macro counter, which is a number that can be incremented throughout the lifespan of the macro, and inserted into the buffer.

So we want to do these steps:

  1. Because the macro counter normally starts at 0, set it to 1.
  2. Record a macro that inserts the macro counter, a period, and a newline.
  3. Replay that macro to insert all the desired lines.

Set the macro counter to 1

C-x C-k C-c 1 RET

Record the macro

C-x ( C-x C-k C-i . RET C-x )

Replay the macro

C-x e and then keep pressing e to repeat the macro.

Note that you can reset the macro counter either before or after the macro definition. This is useful to run the macro multiple sets of times starting at the same number.

1

Using org mode

You're in luck! Org mode natively supports lists in the format you're looking for! Even if you're not using an org file, you could still copy the list from an org file into your document. To do that:

1. Switch to a new org mode buffer.

C-x temp.org

You might^1 have to switch that buffer to org mode.

M-x org-mode RET

2. Insert the first item

1.

There are multiple ways to do this^2, but just inserting the first one is the simplest I know of.

3. Insert the rest of the items

With point on the first item, pressing M-ret will insert a new item below it. Keep pressing until you have all the items you want.

4. Copy back to your original file

Highlight the entire buffer C-x h, copy it M-w, then switch back to your original buffer C-x b ret and yank it C-y.

[1] I have some part of my config to ensure that buffers not associated with a file open in the major mode suggested by the buffer name (e.g., temp.org opens in org mode; two.el opens in emacs-lisp mode), but I can't find out what does that right now.

[2] For example, one could insert a new heading with C-ret, and then convert it to a plain list with C-c -, and cycle through the types of plain list bullets with C-c - until you come to 1..

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.