Thursday, May 14, 2015

Emacs Script to Fold Text

Quite often I need to fold the contents of large text files at certain character boundaries. On my Mac, I can drop to the CLI and execute something like:
fold -w 80 my_text_file.txt > formatted_text_file.txt
This works well, but not on my Windows VM. Since I use Emacs everywhere, I thought it would make sense to have an Elisp function available in Emacs to do the exact same thing. Here is the source for that function:
(defun fold-text (char-count)
  (interactive "n")
  (setq counter 0)
  (while (< counter (point-max))
    (forward-char char-count)
    (insert "\n")
    (setq counter (+ counter char-count))
  )
)
(This script is also on Github here.)

Right now, I have this function listed in my .emacs file so it is available to me when I open Emacs.