Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(re)number numbered lists in emacs (muse)

Tags:

emacs

elisp

suppose I have a text list in emacs like this:

a
b
c
...
d

Is there a way to assign numbers to those items in Emacs, by selecting the region? End results should look like:

1. a
2. b
3. c
j. ...
n. d

Thanks.

like image 896
Dervin Thunk Avatar asked Apr 09 '10 13:04

Dervin Thunk


2 Answers

The way I do this, which may not be optimal, is to use regex search and replace. This, of course, requires that you be able to define a regex to match the start of the lines you want numbers on. Taking your example, I'd use a search regex like this:

\([a-z]\)

note the capturing brackets, we'll need that first letter soon. And a replace regex like this:

\#. \1

where:

\# is a special form which is replaced, by Emacs, by the right number (though see the warning below);

. writes a stop; and

\1 writes a space and the captured group.

WARNING: Emacs will number your items 0, 1, 2, .... Until someone posts to tell us how to start at 1, I always insert a dummy 0th element before the edit, then delete it.

like image 80
High Performance Mark Avatar answered Oct 20 '22 03:10

High Performance Mark


You can use the Emacs Keyboard Macro Counter.

  • Put the cursor one line ABOVE your list.

  • Start a macro: F3

  • Insert the counter value: C-x C-k C-i. A 0 will appear

  • Insert the DOT and a space: .

  • Move the cursor to the next line

  • Stop the macro: F4

  • Select your list

  • M-x apply-macro-to-region-lines

  • You can delete the 0 you added on the top and enjoy :)

NOTE: This will create a numbered list. It will not use letters.

like image 29
Roberto Aloi Avatar answered Oct 20 '22 04:10

Roberto Aloi