Wednesday, June 20, 2012

Numbering Lines and lists in Emacs

I'm not talking about Line Numbering as we usually understand it but more along the lines of numbering lists or text inside the file buffer and which are part of the file. err...best to use the wiki definition again;NumberLines

In Emacs 24.1, the NEWS file had this new addition to the rectangle commands.


** New command `C-x r N' (`rectangle-number-lines') numbers the lines in the current rectangle.  With a prefix argument, this prompts for a number to count from and for a format string.



This allows you to now number lines or lists easily instead of using a macro or additional elisp functions.  Mark a vertical region of text(using transient mark mode may help) 1 character wide and then hit C-x r N and that's it.  It numbers the line.

If you use the prefix command C-u and do C-u C-x r N, it will prompt you for a starting digit and the format string, so you can start from 15, say.

Things do get better with Emacs!

4 comments:

Theldoria said...

Thanks for this post, that function is really helpful :)

Steve said...

THANK YOU!!! I have been looking for this for the last couple years. Every time I looked for 10-15 mins then gave up. This time I found it! Thank you!

Steve said...
This comment has been removed by the author.
Steve said...

Is there a way to make it insert 0's rather than a white space character for those numbers with less digits than the rest, e.g. 01, 02, 03, ..., 10, 11, etc? It's only a few steps to do this with other rectangle commands so not a huge deal if not possible.