I'd like to pretty-print an Elisp form with some arbitrary prefix string inserted at the beginning of each output line.

For example, given the following variable:

(setq var '((abc . 11)
            (def . 22)
            (ghi . 33)))

I'd like something like (pp-with-prefix var " ---> ") (or something like it) to be able to add the " ---> " prefix string (or anything) so I could get something like the following printed:

 ---> ((abc . 11)
 --->  (def . 22)
 --->  (ghi . 33))

instead of what (pp var) would normally print:

((abc . 11)
 (def . 22)
 (ghi . 33))
  • 1
    Use pp-to-string to produce the string and then do string manipulation to do anything you want. The s string library will probably be useful. See github.com/magnar4s/s.el
    – NickD
    Sep 24 at 19:38
  • @NickD That's exactly what I was looking for! Thanks. Please add this as the answer and I'll accept it as the answer.
    – PRouleau
    Sep 24 at 19:41

All that pp does is call pp-to-string on the object and then it calls princ to print the string. So you can use pp-to-string in your own function and then manipulate the resulting string any way you want, before printing it out.

The s string library might be useful for such manipulation. See https://github.com/magnars/s.el

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.