How could you get emacs to write line numbers to a file?

How would you write a file from emacs that only contains line numbers like:

1
2
3
4
5

      

Ideally, this would be a command that you would run (how?) That can be told about how many lines to print. Is it possible?

+3


source to share


3 answers


Here's a quick elisp function that does it:

(defun write-line-numbers (n)
  (interactive "nNumber of lines: ")
  (save-excursion
    (with-output-to-temp-buffer "*lines*"
      (dotimes (line n)
        (princ (format "%d\n" (1+ line))))
      (set-buffer "*lines*")
      (write-file "lines.txt"))))

      

You run it with (write-line-numbers 8)

elisp or with M-x write-line-numbers 8interactive mode.



Or you can save the above as a script and run emacs like this:

emacs -Q --script write-line-numbers.el --eval '(write-line-numbers 8)'

      

But as Moritz points out, there are better ways to do this outside of emacs.

+5


source


Why don't you use a shell program for this seq

? For example. seq 20

will print 20 neat lines numbered 1 through 20.



+2


source


M-:    (c-temp-file "foo.txt" (dotimes (i 15) (insert (format "% 2d \ n" (1+ i)))))

If you do this often enough, make it a function:

(defun write-sequence (length output-file)
  (interactive "nLength of sequence: \nFOutput file: ")
  (with-temp-file output-file
    (dotimes (i length) (insert (format "%d\n" (1+ i))))))

      

+1


source







All Articles